android soap webservers 无法执行 报错 ht.call(SOAP_ACTION, envelope);解决方法
1.可能没有加入网络访问权限,在Manifest里面加入,<uses-permission android:name="android.permission.INTERNET"/>
2.版本问题,很可能的问题,因为在android高于9的会出错,所以简单粗暴的办法就是,直接将版本信息
<uses-sdk
android:minSdkVersion="?"
android:targetSdkVersion="?"
/>
删除即可。
其他具体参考代码如下:
1 package com.example.soaptestforandroid; 2 3 import java.io.IOException; 4 5 import org.ksoap2.SoapEnvelope; 6 import org.ksoap2.serialization.SoapObject; 7 import org.ksoap2.serialization.SoapSerializationEnvelope; 8 import org.ksoap2.transport.HttpTransportSE; 9 import org.xmlpull.v1.XmlPullParserException; 10 11 import android.os.*; 12 import android.annotation.SuppressLint; 13 import android.app.Activity; 14 import android.util.Log; 15 import android.view.Menu; 16 import android.widget.*; 17 18 public class MainActivity extends Activity 19 { 20 21 private static final String SERVICE_NAMESPACE = "http://services.jason.com/"; 22 private static final String SERVICE_URL = "http://172.16.1.164:8080/MyWebServer/UserInfoPort?wsdl"; 23 private EditText tv = null; 24 25 26 27 @SuppressLint("NewApi") 28 @Override 29 protected void onCreate(Bundle savedInstanceState) 30 { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.activity_main); 33 34 35 try { 36 tv = (EditText) findViewById(R.id.hello); 37 38 //如果本系统为4.0以上(Build.VERSION_CODES.ICE_CREAM_SANDWICH为android4.0) 39 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 40 // 详见StrictMode文档 41 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 42 .detectDiskReads().detectDiskWrites().detectNetwork() 43 .penaltyLog().build()); 44 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() 45 .detectLeakedSqlLiteObjects().detectLeakedClosableObjects() 46 .penaltyLog().penaltyDeath().build()); 47 } 48 49 // 调用的方法 50 String mathodName = "GetUserInfo"; 51 String SOAP_ACTION = SERVICE_NAMESPACE + mathodName; 52 // 创建HttpTransportSE对象 53 HttpTransportSE ht = new HttpTransportSE(SERVICE_URL); 54 55 ht.debug = true; 56 // 使用soap协议创建Envelop对象 57 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( 58 SoapEnvelope.VER10); 59 // 实例化SoapObject对象 60 SoapObject object = new SoapObject(SERVICE_NAMESPACE, mathodName); 61 // 将SoapObject对象设置为SoapSerializationEnvelope对象的传出SOAP消息 62 envelope.bodyOut = object; 63 envelope.dotNet = true; 64 envelope.setOutputSoapObject(object); 65 // 调用webService 66 ht.call(SOAP_ACTION, envelope); 67 System.out.println("envelope.getResponse()---" 68 + envelope.getResponse()); 69 if (envelope.getResponse()!= null) { 70 71 SoapObject result = (SoapObject) envelope.bodyIn; 72 String name = result.getProperty(0).toString(); 73 System.out.println(name); 74 tv.setText("返回值为:" + name); 75 76 } else { 77 tv.setText("无返回值"); 78 } 79 } catch (Exception e) { 80 // TODO Auto-generated catch block 81 e.printStackTrace(); 82 } 83 84 } 85 86 @Override 87 public boolean onCreateOptionsMenu(Menu menu) 88 { 89 // Inflate the menu; this adds items to the action bar if it is present. 90 getMenuInflater().inflate(R.menu.main, menu); 91 return true; 92 } 93 94 }