-1

從Android客戶端連接到我的web服務時出現問題。在我的MainActivity類別在該行66程序崩潰:連接到web服務時Android應用崩潰LOCAL

transporte.call(accionSoap, sobre); 

我認爲這個問題可能是與accionSoap東西,任何幫助將是非常apreciatted。

MainActivity:

package com.example.ejemplosoap; 

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

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 

public class MainActivity extends Activity { 

//Napespace definido en el servicio web 
private final String namespace = "http://ws.webapp.org/"; 
//Fichero de definicion del servicio web 
private final String url = "http://10.0.2.2:8080/WebServiceServer/services/Hello"; 
//namespace + metodo 
private final String accionSoap = "http://ws.webapp.org/sayHello"; 
//Metodo que queremos ejecutar en el servicio web 
private final String Metodo = "sayHello"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

public void onClickLogin(View view){ 
    try { 

     System.out.println("1"); 
     // Modelo el request 
     SoapObject request = new SoapObject(namespace, Metodo); 
     //request.addProperty("name", "Juan!"); // Paso parametros al WS 
     PropertyInfo primerParametro = new PropertyInfo(); 
     System.out.println("2"); 
     primerParametro.setName("name"); 
     primerParametro.setValue("Juan"); 
     request.addProperty(primerParametro); 
     System.out.println("3"); 
     // Modelo el Sobre 
     SoapSerializationEnvelope sobre = new   SoapSerializationEnvelope(SoapEnvelope.VER11); 
     //si esta hecho o no en puntoNet 
     sobre.dotNet = false; 
     sobre.setOutputSoapObject(request); 
     System.out.println("4"); 

     // Modelo el transporte 
     HttpTransportSE transporte = new HttpTransportSE(url); 
     System.out.println("5"); 

     // Llamada 
     transporte.call(accionSoap, sobre); 
     System.out.println("6"); 

     // Resultado 
     // SoapPrimitive resultado = (SoapPrimitive) sobre.getResponse(); 
     String cadenaDevuelta =(String) sobre.getResponse(); 
     Log.i("Resultado", cadenaDevuelta); 


    } catch (Exception e) { 
     Log.e("MainActivity", "ERROR"); 
       e.printStackTrace(); 

    } 

} 



} 

的logcat:

10-17 10:26:10.221: I/System.out(721): 1 
10-17 10:26:10.221: I/System.out(721): 2 
10-17 10:26:10.231: I/System.out(721): 3 
10-17 10:26:10.271: I/System.out(721): 4 
10-17 10:26:10.281: I/System.out(721): 5 
10-17 10:26:10.321: D/dalvikvm(721): GC_FOR_ALLOC freed 100K, 8% free 2588K/2808K, paused 38ms, total 42ms 
10-17 10:26:10.381: E/MainActivity(721): ERROR 
10-17 10:26:10.381: W/System.err(721): android.os.NetworkOnMainThreadException 
10-17 10:26:10.391: W/System.err(721): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 
10-17 10:26:10.391: W/System.err(721): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) 
10-17 10:26:10.391: W/System.err(721): at libcore.io.IoBridge.connectErrno(IoBridge.java:144) 
10-17 10:26:10.391: W/System.err(721): at libcore.io.IoBridge.connect(IoBridge.java:112) 
10-17 10:26:10.391: W/System.err(721): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 
10-17 10:26:10.401: W/System.err(721): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) 
10-17 10:26:10.401: W/System.err(721): at java.net.Socket.connect(Socket.java:842) 
10-17 10:26:10.401: W/System.err(721): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76) 
10-17 10:26:10.411: W/System.err(721): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50) 
10-17 10:26:10.411: W/System.err(721): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340) 
10-17 10:26:10.421: W/System.err(721): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 
10-17 10:26:10.421: W/System.err(721): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 
10-17 10:26:10.421: W/System.err(721): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316) 
10-17 10:26:10.421: W/System.err(721): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311) 
10-17 10:26:10.421: W/System.err(721): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290) 
10-17 10:26:10.431: W/System.err(721): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240) 
10-17 10:26:10.431: W/System.err(721): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81) 
10-17 10:26:10.441: W/System.err(721): at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197) 
10-17 10:26:10.451: W/System.err(721): at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:126) 
10-17 10:26:10.451: W/System.err(721): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:179) 
10-17 10:26:10.462: W/System.err(721): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:116) 
10-17 10:26:10.462: W/System.err(721): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:111) 
10-17 10:26:10.471: W/System.err(721): at com.example.ejemplosoap.MainActivity.onClickLogin(MainActivity.java:66) 
10-17 10:26:10.481: W/System.err(721): at java.lang.reflect.Method.invokeNative(Native Method) 
10-17 10:26:10.481: W/System.err(721): at java.lang.reflect.Method.invoke(Method.java:511) 
10-17 10:26:10.491: W/System.err(721): at android.view.View$1.onClick(View.java:3592) 
10-17 10:26:10.491: W/System.err(721): at android.view.View.performClick(View.java:4202) 
10-17 10:26:10.501: W/System.err(721): at android.view.View$PerformClick.run(View.java:17340) 
10-17 10:26:10.512: W/System.err(721): at android.os.Handler.handleCallback(Handler.java:725) 
10-17 10:26:10.512: W/System.err(721): at android.os.Handler.dispatchMessage(Handler.java:92) 
10-17 10:26:10.512: W/System.err(721): at android.os.Looper.loop(Looper.java:137) 
10-17 10:26:10.512: W/System.err(721): at android.app.ActivityThread.main(ActivityThread.java:5039) 
10-17 10:26:10.521: W/System.err(721): at java.lang.reflect.Method.invokeNative(Native Method) 
10-17 10:26:10.521: W/System.err(721): at java.lang.reflect.Method.invoke(Method.java:511) 
10-17 10:26:10.521: W/System.err(721): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
10-17 10:26:10.531: W/System.err(721): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
10-17 10:26:10.531: W/System.err(721): at dalvik.system.NativeStart.main(Native Method) 
10-17 10:26:10.612: W/Trace(721): Unexpected value from nativeGetEnabledTags: 0 
10-17 10:26:10.612: W/Trace(721): Unexpected value from nativeGetEnabledTags: 0 
10-17 10:26:10.681: W/Trace(721): Unexpected value from nativeGetEnabledTags: 0 
10-17 10:26:10.681: W/Trace(721): Unexpected value from nativeGetEnabledTags: 0 

謝謝!

日誌中ShadishKumar解決方案:

10-17 11:29:26.734: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0 
10-17 11:29:26.744: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0 
10-17 11:29:26.744: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0 
10-17 11:29:26.754: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0 
10-17 11:29:26.774: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0 
10-17 11:29:26.774: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0 
10-17 11:29:26.875: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0 
10-17 11:29:26.884: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0 
10-17 11:29:26.904: I/System.out(767): 1 
10-17 11:29:26.904: I/System.out(767): 2 
10-17 11:29:26.904: I/System.out(767): 3 
10-17 11:29:26.904: I/System.out(767): 4 
10-17 11:29:26.904: I/System.out(767): 5 
10-17 11:29:26.944: D/dalvikvm(767): GC_FOR_ALLOC freed 308K, 16% free 2600K/3080K, paused 32ms, total 32ms 
10-17 11:29:26.955: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0 
10-17 11:29:26.955: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0 
10-17 11:29:26.975: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0 
10-17 11:29:26.975: W/Trace(767): Unexpected value from nativeGetEnabledTags: 0 
10-17 11:29:27.095: E/MainActivity(767): ERROR 
10-17 11:29:27.095: W/System.err(767): java.io.IOException: HTTP request failed, HTTP status: 500 
10-17 11:29:27.105: W/System.err(767): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:195) 
10-17 11:29:27.105: W/System.err(767): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:116) 
10-17 11:29:27.105: W/System.err(767): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:111) 
10-17 11:29:27.105: W/System.err(767): at com.example.ejemplosoap.MainActivity$ServiceCallTask.doInBackground(MainActivity.java:75) 
10-17 11:29:27.105: W/System.err(767): at com.example.ejemplosoap.MainActivity$ServiceCallTask.doInBackground(MainActivity.java:1) 
10-17 11:29:27.115: W/System.err(767): at android.os.AsyncTask$2.call(AsyncTask.java:287) 
10-17 11:29:27.115: W/System.err(767): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
10-17 11:29:27.115: W/System.err(767): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
10-17 11:29:27.125: W/System.err(767): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
10-17 11:29:27.125: W/System.err(767): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
10-17 11:29:27.134: W/System.err(767): at java.lang.Thread.run(Thread.java:856) 

回答

0

您需要使用的AsyncTask與Web服務調用的工作,

package com.example.ejemplosoap; 

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

import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 

public class MainActivity extends Activity { 

//Napespace definido en el servicio web 
private final String namespace = "http://ws.webapp.org/"; 
//Fichero de definicion del servicio web 
private final String url = "http://10.0.2.2:8080/WebServiceServer/services/Hello"; 
//namespace + metodo 
private final String accionSoap = "http://ws.webapp.org/sayHello"; 
//Metodo que queremos ejecutar en el servicio web 
private final String Metodo = "sayHello"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

public void onClickLogin(View view){ 

    new ServiceCallTask().execute(); 
} 

class ServiceCallTask extends AsyncTask<String, String, String> 
{ 

    @Override 
    protected String doInBackground(String... params) { 
     try { 

      System.out.println("1"); 
      // Modelo el request 
      SoapObject request = new SoapObject(namespace, Metodo); 
      //request.addProperty("name", "Juan!"); // Paso parametros al WS 
      PropertyInfo primerParametro = new PropertyInfo(); 
      System.out.println("2"); 
      primerParametro.setName("name"); 
      primerParametro.setValue("Juan"); 
      request.addProperty(primerParametro); 
      System.out.println("3"); 
      // Modelo el Sobre 
      SoapSerializationEnvelope sobre = new   SoapSerializationEnvelope(SoapEnvelope.VER11); 
      //si esta hecho o no en puntoNet 
      sobre.dotNet = false; 
      sobre.setOutputSoapObject(request); 
      System.out.println("4"); 

      // Modelo el transporte 
      HttpTransportSE transporte = new HttpTransportSE(url); 
      System.out.println("5"); 

      // Llamada 
      transporte.call(accionSoap, sobre); 
      System.out.println("6"); 

      // Resultado 
      // SoapPrimitive resultado = (SoapPrimitive) sobre.getResponse(); 
      String cadenaDevuelta =(String) sobre.getResponse(); 
      Log.i("Resultado", cadenaDevuelta); 

     } catch (Exception e) { 
      Log.e("MainActivity", "ERROR"); 
        e.printStackTrace(); 
     } 
     return null; 
    } 


} 



} 

請參閱此瞭解更多有關的AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html

+0

仍然無法運行,新的日誌添加到您的解決方案 – user2887592

+0

@ user2887592更改此代碼後,您是否得到相同的錯誤或獲取一些其他錯誤。請爲您的代碼添加更新後的代碼和logcat。在線** MainActivity.java上有什麼代碼:75 ** – SathishKumar

+0

我認爲它是一樣的,你可以看到新的日誌。第75行是transporte.call(accionSoap,sobre); – user2887592

0
android.os.NetworkOnMainThreadException 

您正在運行這是不允許在主線程的網絡操作,請在另一個線程操作。

要麼使用AsyncTask並做呼叫在doInBackground方法,或創建一個new Thread()做在run方法調用。

+0

http://stackoverflow.com/questions/19431456/android -app-crashes-when-connecting-to-webservice-in-local-500-internal-server -e – user2887592

0
public void onClickLogin(View view){  

Runnable runnable = new Runnable() { 
         public void run() { 
           runOnThread(); 
           }; 
        Thread mythread = new Thread(runnable); 
        mythread.start(); 

       } 
    } 


//===== 
    public void runOnThread(){ 

    System.out.println("1"); 
      // Modelo el request 
      SoapObject request = new SoapObject(namespace, Metodo); 
      //request.addProperty("name", "Juan!"); // Paso parametros al WS 
      PropertyInfo primerParametro = new PropertyInfo(); 
      System.out.println("2"); 
      primerParametro.setName("name"); 
      primerParametro.setValue("Juan"); 
      request.addProperty(primerParametro); 
      System.out.println("3"); 
      // Modelo el Sobre 
      SoapSerializationEnvelope sobre = new   SoapSerializationEnvelope(SoapEnvelope.VER11); 
      //si esta hecho o no en puntoNet 
      sobre.dotNet = false; 
      sobre.setOutputSoapObject(request); 
      System.out.println("4"); 

      // Modelo el transporte 
      HttpTransportSE transporte = new HttpTransportSE(url); 
      System.out.println("5"); 

      // Llamada 
      transporte.call(accionSoap, sobre); 
      System.out.println("6"); 

      // Resultado 
      // SoapPrimitive resultado = (SoapPrimitive) sobre.getResponse(); 
      String cadenaDevuelta =(String) sobre.getResponse(); 
      Log.i("Resultado", cadenaDevuelta); 


     } catch (Exception e) { 
      Log.e("MainActivity", "ERROR"); 
        e.printStackTrace(); 

     } 

    } 
+0

http://stackoverflow.com/questions/19431456 /機器人-APP-崩潰-時-連接到web服務的功能於本地-500-內部服務器-E – user2887592

相關問題