gpt4 book ai didi

java - 在 Soap Web 服务响应中获取 Json 数据

转载 作者:太空宇宙 更新时间:2023-11-04 15:17:03 24 4
gpt4 key购买 nike

我使用下面的代码在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) 

最佳答案

看起来您正在获取 JSON,那么您为什么要尝试解析 SOAP?我发现处理其他纯 JSON 响应很复杂,几乎不可能。

我的服务器端以 SOAP(针对 .NET)和 JSON(针对 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+"&parameter2'"+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();
}
}

现在,请记住您需要 AsyncTask 类来调用此类,特别是如果您想将结果输入 UI 时。

关于java - 在 Soap Web 服务响应中获取 Json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20795488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com