0

我有一个在谷歌应用引擎上运行的网络服务,我想从一个安卓设备调用一个可用的服务。我已经关注并合并了大多数关于 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
4

1 回答 1

2

试试看..

尝试在下面的 mainactivityactivity 中使用以下代码setContentView()以避免 networkOnmainThread 异常..

if (android.os.Build.VERSION.SDK_INT > 9) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
            }

但是,在不阻塞主线程的情况下使用 AsyncTask 在后台线程内执行繁重的操作会很棒!

并参考此链接...

AsyncTask Android 示例

于 2013-09-03T16:21:59.187 回答