kSOAP2与Web服务C#的通信

时间:2013-04-30 05:29:42

标签: android asp.net soap android-emulator ksoap2

我正在尝试通过visual studio将Android用户的登录凭据发送到在我的本地主机上运行的C#Web服务。我使用KSOAP2进行SOAP通信,并希望将登录信息发送到Web服务并在UI中打印。我使用过Asynctask,但我在这里发布了以下错误。

ksoap 2 communication - sending the asynctask result to UI thread and print

有人可以在我出错的地方帮忙吗?????

   public class MainActivity extends Activity {

        EditText un,pw;
        TextView tv;
        Button test;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button test=(Button) findViewById(R.id.login);
            un = (EditText) findViewById(R.id.et_un);
            pw = (EditText) findViewById(R.id.et_pw);


                test.setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
 final String NAMESPACE = "http://sparking.org/login";
        final String METHOD_NAME = "login";
        final String SOAP_ACTION = "http://sparking.org/login";
        final String URL = "http://localhost:63734/service.asmx";

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE transport = new HttpTransportSE(URL);

      try {
        transport.call(SOAP_ACTION, envelope);
        SoapPrimitive resultstring = (SoapPrimitive)envelope.getResponse();

          }

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

                        } 
                    }); 
     }
    }

错误信息:

 04-30 05:15:09.215: D/gralloc_goldfish(2997): Emulator without GPU emulation detected.
    04-30 05:15:29.105: W/System.err(2997): android.os.NetworkOnMainThreadException
    04-30 05:15:29.115: W/System.err(2997):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
    04-30 05:15:29.115: W/System.err(2997):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
    04-30 05:15:29.115: W/System.err(2997):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    04-30 05:15:29.125: W/System.err(2997):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
    04-30 05:15:29.125: W/System.err(2997):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
    04-30 05:15:29.135: W/System.err(2997):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
    04-30 05:15:29.135: W/System.err(2997):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
    04-30 05:15:29.135: W/System.err(2997):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
    04-30 05:15:29.135: W/System.err(2997):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
    04-30 05:15:29.135: W/System.err(2997):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
    04-30 05:15:29.135: W/System.err(2997):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
    04-30 05:15:29.155: W/System.err(2997):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
    04-30 05:15:29.155: W/System.err(2997):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
    04-30 05:15:29.165: W/System.err(2997):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
    04-30 05:15:29.165: W/System.err(2997):     at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46)
    04-30 05:15:29.175: W/System.err(2997):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68)
    04-30 05:15:29.175: W/System.err(2997):     at com.example.cmpe220.MainActivity$1.onClick(MainActivity.java:73)
    04-30 05:15:29.185: W/System.err(2997):     at android.view.View.performClick(View.java:4204)
    04-30 05:15:29.195: W/System.err(2997):     at android.view.View$PerformClick.run(View.java:17355)
    04-30 05:15:29.205: W/System.err(2997):     at android.os.Handler.handleCallback(Handler.java:725)
    04-30 05:15:29.215: W/System.err(2997):     at android.os.Handler.dispatchMessage(Handler.java:92)
    04-30 05:15:29.215: W/System.err(2997):     at android.os.Looper.loop(Looper.java:137)
    04-30 05:15:29.225: W/System.err(2997):     at android.app.ActivityThread.main(ActivityThread.java:5041)
    04-30 05:15:29.225: W/System.err(2997):     at java.lang.reflect.Method.invokeNative(Native Method)
    04-30 05:15:29.235: W/System.err(2997):     at java.lang.reflect.Method.invoke(Method.java:511)
    04-30 05:15:29.235: W/System.err(2997):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    04-30 05:15:29.249: W/System.err(2997):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    04-30 05:15:29.249: W/System.err(2997):     at dalvik.system.NativeStart.main(Native Method)
    04-30 05:17:04.375: D/dalvikvm(2997): GC_CONCURRENT freed 175K, 12% free 2579K/2900K, paused 76ms+8ms, total 229ms

清单:

 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.cmpe220"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="14" />
        <permission android:name="android.permission.INTERNET"></permission>

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.220.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

3 个答案:

答案 0 :(得分:0)

我想您的OnClickListener方法存在问题,请尝试此操作。

public class MainActivity extends Activity {

    EditText un,pw;
    TextView tv;
    Button test;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button test=(Button) findViewById(R.id.login);
        un = (EditText) findViewById(R.id.et_un);
        pw = (EditText) findViewById(R.id.et_pw);


    test.setOnClickListener(new OnClickListener(){
    public void onClick(View v) {
    final String NAMESPACE = "http://sparking.org/login";
    final String METHOD_NAME = "login";
    final String SOAP_ACTION = "http://sparking.org/login";
    final String URL = "http://localhost:63734/service.asmx";

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE transport = new HttpTransportSE(URL);

  try {
    transport.call(SOAP_ACTION, envelope);
    SoapPrimitive resultstring = (SoapPrimitive)envelope.getResponse();

      }

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

                    } 
                }); 
 }
}

答案 1 :(得分:0)

当您尝试通过主线程中的http进行通信时,该错误就会出现。

在声明'request'对象之前添加这两行:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

答案 2 :(得分:0)

实际上,localhost是模拟器本身,因为代码在模拟器中运行。所以你应该连接到10.0.2.2。有关详细信息,请参阅 Android Emulator Networking并且不要忘记清单文件中的网络权限