从Android客户端连接到我的web服务时遇到问题。在我的MainActivity类中,程序在第66行崩溃:
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();
}
}
}
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 :(得分:0)
android.os.NetworkOnMainThreadException
您正在主线程中运行不允许的网络操作,在另一个线程中执行操作。
使用AsyncTask
并使用doInBackground
方法进行通话,或创建new Thread()
并使用run
方法进行通话。
答案 1 :(得分: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
的更多信息答案 2 :(得分: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();
}
}