Java 在Soap Web服务响应中获取Json数据
我使用下面的代码在soap Web服务响应中获取JSONJava 在Soap Web服务响应中获取Json数据,java,android,json,web-services,soap,Java,Android,Json,Web Services,Soap,我使用下面的代码在soap Web服务响应中获取JSON SoapSerializationEnvelope envelope = new new SoapSerializationEnvelope(SoapEnvelope.VER11); try { SOAP_ACTION = namespace + MethodName; //Adding values to request object request = new SoapObject(namespace, M
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)
看起来您得到了JSON,那么您为什么要尝试解析SOAP呢? 我发现它很复杂,几乎不可能处理其他纯JSON响应 我让服务器端用SOAP(用于.NET)和JSON(用于Android)进行响应 下面是我从远程服务获取数据并对其进行解析(在本例中为int)的方法
现在,请记住,您需要AsyncTask类来调用此类类,特别是如果您希望将结果放入UI中。请发布完整的堆栈跟踪。另外,什么是
envelope
和什么是SoapObject
?在堆栈跟踪中,我得到了SoapSerializable中意外的标记和信封的异常,我编辑了我的问题。我有点困惑,我是否得到了正确的json响应或格式不正确12-27 13:49:58.905:I/Webservice(2196):意外类型(在java.io中的位置:TEXT[{“result”:“}]@1:16。InputStreamReader@406d5c48)
//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();
}
}