简体   繁体   中英

Connecting to C# asp.net using Ksoap2 in Android

I'm trying to connect to my asp web service and get the response using this codes. It gives no error but it's not connecting to my web service either. Can anyone point my mistake? thanks

public class MainActivity extends Activity {

    private static final String SOAP_ACTION = "http://tempuri.org/MyFirstWebMethod";
    private static final String OPERATION_NAME = "MyFirstWebMethod";
    private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
    private static final String SOAP_ADDRESS = "http://10.0.2.2:1595/test.asmx";
    String name = "Test";
    String last = "Me";
    String result;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ((Button) findViewById(R.id.btngo)).setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                 SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME); 
                 request.addProperty("name", name);
                 request.addProperty("last", last);
                 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                 envelope.dotNet=true;
                 envelope.setOutputSoapObject(request); 
                 HttpTransportSE ht = new HttpTransportSE(SOAP_ADDRESS);
                 try {
                 ht.call(SOAP_ACTION, envelope);
                 SoapObject response  = (SoapObject)envelope.getResponse();

                 result = response.getProperty(1).toString();

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

                Toast.makeText(getBaseContext(), ""+result, Toast.LENGTH_LONG).show();
            }
        });


    }
}

this is what the log cat said

11-08 13:15:00.344: I/Choreographer(977): Skipped 58 frames!  The application may be doing too much work on its main thread.
11-08 13:15:00.405: W/System.err(977): android.os.NetworkOnMainThreadException
11-08 13:15:00.405: W/System.err(977):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
11-08 13:15:00.413: W/System.err(977):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
11-08 13:15:00.413: W/System.err(977):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
11-08 13:15:00.413: W/System.err(977):  at libcore.io.IoBridge.connect(IoBridge.java:112)
11-08 13:15:00.413: W/System.err(977):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
11-08 13:15:00.413: W/System.err(977):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
11-08 13:15:00.413: W/System.err(977):  at java.net.Socket.connect(Socket.java:842)
11-08 13:15:00.413: W/System.err(977):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
11-08 13:15:00.413: W/System.err(977):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
11-08 13:15:00.413: W/System.err(977):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
11-08 13:15:00.413: W/System.err(977):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
11-08 13:15:00.413: W/System.err(977):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11-08 13:15:00.413: W/System.err(977):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
11-08 13:15:00.413: W/System.err(977):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
11-08 13:15:00.413: W/System.err(977):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
11-08 13:15:00.437: W/System.err(977):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
11-08 13:15:00.437: W/System.err(977):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
11-08 13:15:00.437: W/System.err(977):  at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:75)
11-08 13:15:00.437: W/System.err(977):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:136)
11-08 13:15:00.437: W/System.err(977):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:90)
11-08 13:15:00.437: W/System.err(977):  at com.example.xmlparser.MainActivity$1.onClick(MainActivity.java:39)
11-08 13:15:00.443: W/System.err(977):  at android.view.View.performClick(View.java:4084)
11-08 13:15:00.443: W/System.err(977):  at android.view.View$PerformClick.run(View.java:16966)
11-08 13:15:00.443: W/System.err(977):  at android.os.Handler.handleCallback(Handler.java:615)
11-08 13:15:00.443: W/System.err(977):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-08 13:15:00.443: W/System.err(977):  at android.os.Looper.loop(Looper.java:137)
11-08 13:15:00.443: W/System.err(977):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-08 13:15:00.443: W/System.err(977):  at java.lang.reflect.Method.invokeNative(Native Method)
11-08 13:15:00.453: W/System.err(977):  at java.lang.reflect.Method.invoke(Method.java:511)
11-08 13:15:00.453: W/System.err(977):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-08 13:15:00.453: W/System.err(977):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-08 13:15:00.453: W/System.err(977):  at dalvik.system.NativeStart.main(Native Method)
11-08 13:15:00.618: I/Choreographer(977): Skipped 49 frames!  The application may be doing too much work on its main thread.
Sucessful Connection with Asp.net Webservice-----
package ProductVerificationCard.in;

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

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class AdminLogin extends Activity {
    /** Called when the activity is first created. */
    Button btn_ok;
    TextView textView;
    private static final String SOAP_ACTION = "http://tempuri.org/Login";

    private static final String OPERATION_NAME = "Login";

    private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";

    private static final String SOAP_ADDRESS = "http://10.0.2.2/new/WebService.asmx";
    String s;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn_ok=(Button) findViewById(R.id.btn_login);
        textView=(TextView) findViewById(R.id.tv_error);

        btn_ok.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
                        OPERATION_NAME);

                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                        envelope.dotNet = true;

                        envelope.setOutputSoapObject(request);

                        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);

                        try

                        {

                        httpTransport.call(SOAP_ACTION, envelope);

                        Object response = envelope.getResponse();

                        //textView.setText(response.toString());
                         s=response.toString();
                         if(s=="true")
                         {
                             Intent intent=new Intent(AdminLogin.this,MenuForm.class);
                                startActivity(intent);

                         }

                         else
                         {
                             textView.setText("Enter Valid Username or Password");
                         }
                        }

                        catch (Exception exception)

                        {

                        textView.setText(exception.toString());

                        }
                // TODO Auto-generated method stub
                }
        });

    }
}

Do u think webservice at "http://10.0.2.2:1595" will be accessible publicly ?(In my option NO). Try to host it on public IP and then try . Your code seems to be OK .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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