从Android客户端连接到我的web服务时遇到问题。在我的MainActivity类中,程序在以下行崩溃:
transporte.call(accionSoap, sobre);
我认为问题可能与accionSoap有关,任何帮助都会非常令人沮丧。
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();
}
}
}
谢谢!
NEW LOG UPDATED:
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
答案 0 :(得分:0)
你在这一行上有nullpointerexception
Log.e("ERROR", e.getMessage());
因此e.getMessage()返回null。
所以将其改为
Log.e("ERROR", "" + e.getMessage());
或在此行之前加if(e.getMessage()!=null)
答案 1 :(得分:0)
您的问题似乎就在这一行 - 可能是第76行,并且该方法中唯一一个似乎使用Log.e
Log.e("ERROR", e.getMessage());
异常的getMessage()方法很可能返回null,而Log.e()调用则不会处理。
改为考虑
Log.e("MainActivity", "ERROR");
e.printStackTrace();
考虑到Log方法的第一个参数传统上是标识日志记录组件的“标记”,而不是消息的“内容”。