2013-04-30 46 views
0

我想從android用戶發送登錄憑證到通過visual studio在本地主機上運行的C#web服務。我已經使用KSOAP2進行SOAP通信,並希望將登錄信息發送到Web服務並在UI中進行打印。我已經使用Asynctask,但我一直在這裏發佈下面的錯誤。kSOAP2與webservices通信C#

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> 

回答

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

        } 
       }); 
} 
} 
0

這個錯誤是當你嘗試通過你的主線程通過http進行通信。

聲明「請求」對象之前,只需添加這些2號線:

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

實際上,本地主機是模擬器本身在這裏,因爲代碼仿真器中運行。所以你應該連接到10.0.2.2。詳細內容見 Android Emulator Networking,不要忘了在清單文件中的網絡權限

+0

不要忘記在manifest文件 添加網絡權限<使用許可權的android:NAME =「android.permission。 ACCESS_NETWORK_STATE「/> <使用權限android:name =」android.permission.ACCESS_WIFI_STATE「/> <使用權限android:name =」android.permission.INTERNET「/> – ama527 2018-02-22 05:57:32

+0

這不會提供問題的答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/18897356) – aifarfa 2018-02-22 08:01:55