0

我正在尝试连接到我的 asp Web 服务并使用此代码获取响应。它没有给出错误,但它也没有连接到我的网络服务。谁能指出我的错误?谢谢

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();
            }
        });


    }
}

原木猫是这样说的

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.
4

2 回答 2

2
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
                }
        });

    }
}
于 2012-11-08T05:54:00.430 回答
0

你认为“http://10.0.2.2:1595”的网络服务可以公开访问吗?(在我的选项中否)。尝试将其托管在公共 IP 上,然后尝试 . 您的代码似乎没问题。

于 2012-11-08T08:59:23.370 回答