Call SOAP service from android with ksoap error [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-24 18:11:08

问题


I have a webservice running on Google app engine, and i would to call a available service from an android device. I've follow and merge code of most tutorial about KSoap2, but i still obtain an exception on android. this is the code:

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();
}

and that's the error log:

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

回答1:


Try it..

Try using the below code inside your mainactivityactivity below setContentView() to avoid networkOnmainThread exception..

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

But, doing heavy operation inside background thread using AsyncTask without blocking main thread would be great!

And also refer this link...

AsyncTask Android example



来源:https://stackoverflow.com/questions/18597002/call-soap-service-from-android-with-ksoap-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!