我有一个在Google应用引擎上运行的网络服务,我想从Android设备调用可用的服务。我已经关注并合并了关于KSoap2的大多数教程的代码,但我仍然在android上获得了一个例外。这是代码:
private static String METHOD_NAME = "getData";
private static String SOAP_ACTION = "http://example.com/getData";
private static String WSDL_URL = "http://arduino-data-server.appspot.com/FunctionsService.wsdl";
private static String NAMESPACE = "http://example.com/";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER12);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(WSDL_URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject) envelope.getResponse();
if (result != null) {
TextView t = (TextView) this.findViewById(R.id.text_view);
t.setText("SOAP response:\n\n"+ result.getProperty(0).toString());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
那是错误日志:
09-03 18:09:04.805: W/dalvikvm(28352): threadid=1: thread exiting with uncaught exception (group=0x41909700)
09-03 18:09:04.821: E/AndroidRuntime(28352): FATAL EXCEPTION: main
09-03 18:09:04.821: E/AndroidRuntime(28352): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.provasoap/com.example.provasoap.MainActivity}: android.os.NetworkOnMainThreadException
09-03 18:09:04.821: E/AndroidRuntime(28352): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-03 18:09:04.821: E/AndroidRuntime(28352): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-03 18:09:04.821: E/AndroidRuntime(28352): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-03 18:09:04.821: E/AndroidRuntime(28352): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-03 18:09:04.821: E/AndroidRuntime(28352): at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 18:09:04.821: E/AndroidRuntime(28352): at android.os.Looper.loop(Looper.java:137)
09-03 18:09:04.821: E/AndroidRuntime(28352): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-03 18:09:04.821: E/AndroidRuntime(28352): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 18:09:04.821: E/AndroidRuntime(28352): at java.lang.reflect.Method.invoke(Method.java:525)
09-03 18:09:04.821: E/AndroidRuntime(28352): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-03 18:09:04.821: E/AndroidRuntime(28352): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-03 18:09:04.821: E/AndroidRuntime(28352): at dalvik.system.NativeStart.main(Native Method)
09-03 18:09:04.821: E/AndroidRuntime(28352): Caused by: android.os.NetworkOnMainThreadException
09-03 18:09:04.821: E/AndroidRuntime(28352): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
09-03 18:09:04.821: E/AndroidRuntime(28352): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
09-03 18:09:04.821: E/AndroidRuntime(28352): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-03 18:09:04.821: E/AndroidRuntime(28352): at java.net.InetAddress.getAllByName(InetAddress.java:214)
09-03 18:09:04.821: E/AndroidRuntime(28352): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
09-03 18:09:04.821: E/AndroidRuntime(28352): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
09-03 18:09:04.821: E/AndroidRuntime(28352): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
09-03 18:09:04.821: E/AndroidRuntime(28352): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
09-03 18:09:04.821: E/AndroidRuntime(28352): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
09-03 18:09:04.821: E/AndroidRuntime(28352): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
09-03 18:09:04.821: E/AndroidRuntime(28352): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
09-03 18:09:04.821: E/AndroidRuntime(28352): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
09-03 18:09:04.821: E/AndroidRuntime(28352): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
09-03 18:09:04.821: E/AndroidRuntime(28352): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
09-03 18:09:04.821: E/AndroidRuntime(28352): at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
09-03 18:09:04.821: E/AndroidRuntime(28352): at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:126)
09-03 18:09:04.821: E/AndroidRuntime(28352): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:179)
09-03 18:09:04.821: E/AndroidRuntime(28352): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:116)
09-03 18:09:04.821: E/AndroidRuntime(28352): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:111)
09-03 18:09:04.821: E/AndroidRuntime(28352): at com.example.provasoap.MainActivity.onCreate(MainActivity.java:47)
09-03 18:09:04.821: E/AndroidRuntime(28352): at android.app.Activity.performCreate(Activity.java:5133)
09-03 18:09:04.821: E/AndroidRuntime(28352): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-03 18:09:04.821: E/AndroidRuntime(28352): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-03 18:09:04.821: E/AndroidRuntime(28352): ... 11 more
答案 0 :(得分:2)
试试..
尝试在setContentView()
下面的mainactivityactivity中使用以下代码,以避免networkOnmainThread异常。
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
但是,使用AsyncTask在后台线程中进行大量操作而不阻塞主线程会很棒!
并参考此链接......