android 中使用 soap, 发生异常, 不知道哪里 错了?报错-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

android 中使用 soap, 发生异常, 不知道哪里 错了?报错

爱吃鱼的程序员 2020-06-22 11:05:39 92

代码如下, 报错是call方法, 我用的是虚拟机,服务器是本机

     public void onClick(View arg0)
     {

      String sayHello = "sayHello";
      String NameSpace = "http://www.zhangboyue.com/";
      String WSDL = "http://localhost/xFireService/services/MyService";
      
      String result = "";
   
      SoapObject request = new SoapObject(NameSpace,sayHello);
   
      request.addProperty("msg", "zhangboyue");
      
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
        SoapEnvelope.VER11);
   
      envelope.bodyOut = request;
      envelope.setOutputSoapObject(request);
      envelope.encodingStyle = "UTF-8";
  
      HttpTransportSE ht = new HttpTransportSE(WSDL, 15000);
      ht.debug = true;
 
      try
      {
       ht.call(null, envelope);
       if (envelope.getResponse() != null)
       {
        SoapObject response = (SoapObject) envelope.getResponse();
        result = response.toString();
        Toast.makeText(getActivity(), result, Toast.LENGTH_SHORT).show();
       }
      } catch (IOException e)
      {
       System.out.println("IOException");
      } catch (XmlPullParserException e)
      {
       System.out.println("XmlPullParserException");
      }

下面是log

04-04 06:24:18.691: E/AndroidRuntime(2132): FATAL EXCEPTION: main
04-04 06:24:18.691: E/AndroidRuntime(2132): android.os.NetworkOnMainThreadException
04-04 06:24:18.691: E/AndroidRuntime(2132):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:109)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:157)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:96)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at com.fragments.micxiyou.MyInfoActivity$1.onClick(MyInfoActivity.java:64)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at android.view.View.performClick(View.java:4202)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at android.view.View$PerformClick.run(View.java:17340)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at android.os.Handler.handleCallback(Handler.java:725)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at android.os.Looper.loop(Looper.java:137)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at android.app.ActivityThread.main(ActivityThread.java:5039)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at java.lang.reflect.Method.invokeNative(Native Method)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at java.lang.reflect.Method.invoke(Method.java:511)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-04 06:24:18.691: E/AndroidRuntime(2132):  at dalvik.system.NativeStart.main(Native Method)
04-04 06:24:18.828: D/dalvikvm(2132): GC_CONCURRENT freed 131K, 8% free 3424K/3700K, paused 16ms+36ms, total 169ms

 

使用不报错 hole使用 solid使用 soa使用 android不用
分享到
取消 提交回答
全部回答(1)
  • 爱吃鱼的程序员
    2020-06-23 11:59:32

    日志已经说的很清楚了, NetworkOnMainThreadException, 访问网络的代码不能在mainthread 里。

    引用来自“linan”的评论

    日志已经说的很清楚了, NetworkOnMainThreadException, 访问网络的代码不能在mainthread 里。

    网络访问必须用线程,不然就出现anr.这个是android的规定

    引用来自“索马里海盗甲”的评论

    网络访问必须用线程,不然就出现anr.这个是android的规定

    0 0
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

相似问题
最新问题