kSOAP2 communication with webservices C#

不打扰是莪最后的温柔 提交于 2019-12-25 07:39:16

问题


I am trying to send the login credentials from android user to a C# web service running in my local host through visual studio. I have used KSOAP2 for the SOAP communication and wanted to send the login info to the web service and print it in the UI. I have used Asynctask but I have been getting the below error posted here.

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

Can some one please help where I went wrong ?????

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

                        } 
                    }); 
     }
    }

ERROR MESSAGES:

 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

MANIFEST:

 <?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>

回答1:


I guess the problem in your OnClickListener method, just try this.

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

                    } 
                }); 
 }
}



回答2:


That error is when you try to communicate through http from your main thread.

Just add those 2 lines before declaring the 'request' object:

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



回答3:


actually, localhost is emulator itself here because the code runs inside the emulator. so you should connect to 10.0.2.2. for more details see Android Emulator Networking and don't forget the network permissions in the manifest file



来源:https://stackoverflow.com/questions/16292781/ksoap2-communication-with-webservices-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!