我是android新手。我需要从android连接到web服务。 webservice方法返回一个大于10MB的字符串值。我想过尝试使用KSOAP。有人可以给我一个简单的例子,使用来自android的KSOAP连接到SOAP web服务吗?
答案 0 :(得分:1)
添加KSOAP jar文件并使用Async方法进行webservice
public class Connection extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... params) {
String SOAP_ACTION = "http://www.example.com/Get/GetCount";
String METHOD_NAME = "GetCount";
String NAMESPACE = "http://www.example.com";
String URL = "http://www.example.com/Get.php?wsdl";
String Result;
SoapObject request1 = new SoapObject(NAMESPACE, METHOD_NAME );
request1.addProperty("AppId", 1);
SoapSerializationEnvelope envelope = new soapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true; envelope.setOutputSoapObject(request1);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true; try {
//this is the actual part that will call the webservice
androidHttpTransport.call(soapaction, envelope);
}
catch (IOException e) {
e.printStackTrace(); }
catch (XmlPullParserException e)
{ e.printStackTrace(); }
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject)envelope.bodyIn;
Result = result.getProperty(0).toString();
JSONObject jArray = new JSONObject(Result);
JSONArray Count = jArray.getJSONArray("Details");
ArrayList<HashMap<String, String>> myList = new ArrayList<HashMap<String, String>>();
for(int i=0;i < Count .length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = Count .getJSONObject(i);
map.put("id", e.getString("ID"));
myList.add(map);
}
}