我使用下面的代码在soap webservice响应中获取JSON
SoapSerializationEnvelope envelope = new new SoapSerializationEnvelope(SoapEnvelope.VER11);
try {
SOAP_ACTION = namespace + MethodName;
//Adding values to request object
request = new SoapObject(namespace, MethodName);
//Adding Double value to request object
PropertyInfo weightProp =new PropertyInfo();
//Adding String value to request object
request.addProperty("myParam1", "" + myParam1);
request.addProperty("myParam2", "" + myParam2);
SetEnvelope(url);
try {
//SOAP calling webservice
androidHttpTransport.call(SOAP_ACTION, envelope);
//Got Webservice response
SoapObject res = (SoapObject) envelope.getResponse();
return Integer.parseInt(res);
} catch (Exception e) {
return -1;
//return 0;
}
} catch (Exception e) {
return -1;
//return 2;
}
作为回应我的Json数据
({"result":"123456" })
然后我在线检查了我的Json,然后我转换了我的JSON数据
{"result":"123456" }
但在我得到的两种情况中 例外
12-27 13:49:58.905: I/Webservice(2196): unexpected type (position:TEXT [{"result":"" }]@1:16 in java.io.InputStreamReader@406d5c48)
答案 0 :(得分:0)
好像你正在获得JSON所以为什么要尝试解析SOAP? 我发现它很复杂,几乎不可能处理其他纯粹的JSON响应。
我让我的服务器端在SOAP(for .NET)和JSON for(Android)中都做出响应。
所以这就是我用来从远程服务获取数据并解析它(在本例中为int)。
//This method receives 2 parameters and return string - just example...
//I'm using HttpGet but there are also HttpPost objects
public int getResults(String yourParameter1,String yourParameter2)
{
int results=0;
Log.d("Webservice call:","Started");
//Creating the get URL
String url= "http://my.webservice.url/targetfile.aspx?parameter1="+yourParameter1+"¶meter2'"+yourParameter2;
Log.d("URL:",url);
HttpClient hc = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
String tempresult="";
Log.d("hc",hc.toString());
Log.d("post",get.getURI().toString());
try {
HttpResponse rp = hc.execute(get);
Log.d("rp",rp.getEntity().toString());
Log.d("rp2",rp.getStatusLine().toString());
// Get hold of the response entity
HttpEntity entity = rp.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
tempresult=rp.toString();
Log.d("tempresult",tempresult);
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
tempresult= convertStreamToString(instream);
// now you have the string representation of the HTML request
instream.close();
Log.d("result",tempresult.toString());
}
//tempresults holding the JSON
JSONObject json = new JSONObject(tempresult);
//getting the "results" value
results=Integer.parseInt(json.getString("result"));
} catch (Exception e) {
e.printStackTrace();
Log.d("error parsing JSON",e.toString());
}
return results;
}
//This method is to handle response
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
现在,请记住,如果要将结果输入UI,您需要使用AsyncTask类来调用此类。