0

Google アプリ エンジンで実行されている Web サービスがあり、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
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 に答える