简体   繁体   English

如何在android中调用soap web-service?

[英]how to call soap web-service in android?

when i try to call soap webservices in android after below line 当我尝试在线下之后在android中调用soap webservices时

androidHttpTransport.call(SOAP_ACTION, envelope);

program direct jump in to catch program should be call result = envelope.getResponse(); 程序直接跳转到catch程序应该调用result = envelope.getResponse(); but i haven't revived a response what is the possible solution any one help in this? 但我没有恢复一个回应什么是可能的解决方案任何一个帮助在这?

try {
    System.out.println("Token ===sssTTTTTT " );

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    request.addProperty("encAppName", "dsakjsfj");
    request.addProperty("sessionInfo", "sadsadsdf");

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    androidHttpTransport.call(SOAP_ACTION, envelope);

      result = envelope.getResponse();
       Toast.makeText(getBaseContext(), "  Result " + "\n" + result.toString(), Toast.LENGTH_SHORT).show();
       System.out.println("response === " + result.toString());

} catch (Exception e) {
    // txtprint.setText(e.getMessage());
}

Please try this this my working code. 请尝试这个我的工作代码。 just do your necessary changes. 只是做你必要的改变。 And if you are saying it is going direct to catch block, it means it is throwing some exception. 如果你说它直接捕获阻塞,那就意味着它会抛出一些异常。 please try to see what is that. 请试着看看那是什么。 use asynctask for background thread(request response) 使用asynctask作为后台线程(请求响应)

// put here your url's..
    private final String URL = "http://192.192.192.192/DemoService/Demo.asmx";
        private final String SOAP_ACTION = "http://tempuri.org/AndroidTestRequest";
        private final String METHOD_NAME = "AndroidTestRequest";


SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("User", "abcd@gmail.com");
        request.addProperty("Password", "test@123");
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.headerOut = new Element[1];
        envelope.headerOut[0] = buildAuthHeader();
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

// you can add your properties here if you want to.
        /*
         * PropertyInfo cityProp = new PropertyInfo();
         * 
         * cityProp.setType(String.class); request.addProperty(cityProp);
         */

        Log.e("value of request", request.toString());
        Log.e("Value of envolope ", envelope.toString());

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try {

            androidHttpTransport.call(SOAP_ACTION, envelope);

                    Log.i("myAppEnvelope", envelope.toString());

            SoapObject response = (SoapObject) envelope.getResponse();

            SoapObject object = (SoapObject) response.getProperty("value");


        } catch (Exception e) {
            e.printStackTrace();
        }

Is your soap service is running properly ?if yes then you must call your webservice thr asynctask in android. 你的soap服务是否正常运行 ?如果是,那么你必须在android中调用你的webservice thr asynctask what Exception you get in catch block ? 你在catch块中获得了什么异常 check it that will help you get out of it.Hope this helps you.check below code call this class method thr asynctask 检查它会帮助你摆脱它。希望这可以帮助你。 检查下面的代码调用这个类方法thr asynctask

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;


public class WebCommunication {

    public static String SOAP_ACTION = "";
    public static String METHOD_NAME = "";
    public static String NAMESPACE = "http://tempuri.org/";
    public static String URL = "http://192.168.1.108:8085/wsTrinfin/Service.asmx";//your webservice 

    public String CALLSOAP(String Data)
    {
        try {
            String Result="";

            METHOD_NAME = "ManageLicense";
            SOAP_ACTION = "http://tempuri.org/ManageLicense";

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("Data",Data);
            request.addProperty("ActivationType","Online");
            request.addProperty("ValidationParameter","TABLET");
            request.addProperty("SenderPhoneNumber","NA");

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;         
            envelope.setOutputSoapObject(request);
            HttpTransportSE aht = new HttpTransportSE(URL);
            SoapPrimitive results = null;
            try 
            {
                aht.call(SOAP_ACTION, envelope);
                results = (SoapPrimitive) envelope.getResponse();
            } 
            catch (Exception e) 
            {               
                //Log.Write("Unable to connect to webservice : " +e.toString());
                return "ERROR";
            }           
            Result= results.toString();         

            return Result;

        } catch (Exception e) {
            //Log.Write("Error Occured in :"+URL+e.toString(),Log._LogLevel.NORAML);
            return "ERROR";
        }
    }


}
public InputStream sendPOSTRequest(String strPostURL, String strParamToPost) 
{


    strPostURL = strPostURL.replace(" ", "%20");

    DefaultHttpClient defaultHttpClient = getHttpClient();
    HttpPost httpPost = new HttpPost(strPostURL);
    httpPost.addHeader("Content-Type", "text/xml; charset=utf-8");



    InputStream inputStream = null;

    try 
    {
        if(strParamToPost != null)
            httpPost.setEntity(new StringEntity(strParamToPost)); 

        LogUtils.info(LogUtils.SERVICE_LOG_TAG, "**Executing requst**");

        HttpResponse response = defaultHttpClient.execute(httpPost);

        LogUtils.info(LogUtils.SERVICE_LOG_TAG, "##Response received##");

        HttpEntity entity = response.getEntity();
        inputStream = entity.getContent();
    }
    catch (Exception e)
    {
        LogUtils.error("HttpHelper", "PostData Error :"+e.toString());
    }
    return inputStream;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM