標籤:
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 Activity19 {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 @Override29 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 // 調用webService66 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 block81 e.printStackTrace();82 } 83 84 }85 86 @Override87 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 }View Code
android soap webservers 無法執行 報錯 ht.call(SOAP_ACTION, envelope);解決方案