【问题标题】:How to Invoke Dot Net Webservice from Android Activity with KSOAP如何使用 KSOAP 从 Android Activity 调用 Dot Net Webservice
【发布时间】:2016-02-15 22:09:38
【问题描述】:
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);
            }
        });

    }}

现在我将向您展示我调用 web 服务的代码,并注意您已将具有依赖关系的 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 上遇到异常,请查看我的代码并帮助寻找替代方案。最好的方法是使用我的网络服务并将您的代码作为答案。

'提前致谢'

【问题讨论】:

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


    【解决方案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();
    

    【讨论】:

    • 虽然理论上这可以回答这个问题,it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    • 更新了我的答案。 @AhmedAshour
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多