如何使用KSOAP从Android活动调用Dot Net Webservice

时间:2015-11-14 11:16:06

标签: android web-services exception-handling ksoap networkonmainthread

I want to invoke my live webservice on android activity and i am using Ksoap technique ,I have made activity are as follows;

Webservice live on server as -'http://scoolbag.somee.com/service.asmx'
Method Name- 'TalkTalk'
Working EmpId- 837382

I have made two java files out of which one is activity java file and by other i am calling my webservice.
namely 'Webservice.java' for ANDROID ACTIVITY
'WebserviceCall.java'for CALLING WEBSERVICE
==================================================================

现在我将指导我的代码部分,我已经开展了一项名为' webservice.java'

的活动

Android活动

public class webservice extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webservice);
        final Button webserviceCallButton = (Button) findViewById(R.id.btnInvoke);
        final EditText webserviceResponse = (EditText) findViewById(R.id.editEID);

        webserviceCallButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                Toast.makeText(getApplicationContext(),"Requesting to server",
                        Toast.LENGTH_LONG).show();

                //Create Webservice class object
                WebserviceCall com = new WebserviceCall();

                // Initialize variables
                String Eid= webserviceResponse.getText().toString();


                //Call Webservice class method and pass values and get response
                String aResponse = com.getTalkTalk(Eid);

                //Alert message to show webservice response
                Toast.makeText(getApplicationContext(), Eid+" User= "+aResponse+" Name",
                        Toast.LENGTH_LONG).show();

                Log.i("AndroidExampleOutput", "----"+aResponse);

                webserviceResponse.setText("Response : "+aResponse);
            }
        });

    }}

现在我将向您展示我调用webservice的代码并记下您已将依赖的Ksaop Jar文件插入liberary文件夹。请看下面提到的代码。

WebserviceCall.java

public class WebserviceCall {
String namespace = "http://www.niceald.in/";
    private String url = "http://scoolbag.somee.com/service.asmx";

    String SOAP_ACTION;
SoapObject request = null, objMessages = null;
SoapSerializationEnvelope envelope;
AndroidHttpTransport androidHttpTransport;

WebserviceCall() {
}


/**
 * Set Envelope
 */
protected void SetEnvelope() {

    try {

        // Creating SOAP envelope
        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        //You can comment that line if your web service is not .NET one.
        envelope.dotNet = true;

        envelope.setOutputSoapObject(request);
        androidHttpTransport = new AndroidHttpTransport(url);
        androidHttpTransport.debug = true;

    } catch (Exception e) {
        System.out.println("Soap Exception---->>>" + e.toString());
    }
}

// MethodName variable is define for which webservice function  will call
public String getTalkTalk(String EMPid)
{

    try {
        SOAP_ACTION = namespace + EMPid;

        //Adding values to request object
        request = new SoapObject(namespace, EMPid);

        //Adding Double value to request object
        // PropertyInfo weightProp =new PropertyInfo();



        SetEnvelope();

        try {

            //SOAP calling webservice
            androidHttpTransport.call(SOAP_ACTION, envelope);

            //Got Webservice response
            String result = envelope.getResponse().toString();

            return result;

        } catch (Exception e) {
            // TODO: handle exception
            return e.toString();
        }
    } catch (Exception e) {
        // TODO: handle exception
        return e.toString();
    }

}}

我收到了一个例外NetworkonMainThread,请查看我的代码并提供替代帮助。最好的方法是使用我的web服务并将代码作为答案。

'在此先感谢'

1 个答案:

答案 0 :(得分:1)

我认为你的信封有问题。请检查此链接Android Ksoap2 web services asmx并按照所有步骤操作。下面是ksoap用于传输HttpTransportSE的信封的内容。请使用这样的,

SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
soapObject.addProperty("Celsius","12");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(soapObject);
HttpTransportSE httpTransportSE = new HttpTransportSE(URL);
httpTransportSE.call(SOAP_ACTION, envelope);
SoapPrimitive soapPrimitive = (SoapPrimitive)envelope.getResponse();
Log.d("TAG", "doInBackground: "+soapPrimitive.toString());
return soapObject.toString();