我知道这个例外之前被问了一百万次,我看到各个帖子说不同的建议,但对我来说没有任何作用。
我需要使用httpget从网址获取一些数据,并在我的网址中提供特定的ID。 我的输出应该看起来像这个
{
"id": "35ZGXU7WQHFYY5BFTV6EACGEUS",
"createTime": "2014-04-11T12:52:26",
"updateTime": "2014-04-11T12:52:55",
"status": "Completed",
"transaction": {
"amount": {
"currencyCode": "GBP",
"total": 7.47
},
"qrcode": "189e8dad99908745o7439f8ffabdfipp",
"description": "This is the payment transaction description."
}
}
但由于某些内容出现此错误
04-11 18:24:14.655:E / STATUS_ERR(30067):org.json.JSONException:java.lang.String类型的值com.mywallet.android.rest.MyWalletRestService$getStatus@41837fd0不能转换为JSONObject
我的代码如下
String line = null;
try{
BufferedReader in = null;
HttpGet request = new HttpGet();
URI website = new URI(url);
request.setURI(website);
request.setHeader("Accept", "application/json");
request.setHeader(AppConstants.PAYMENT_HEADER1, BEARER);
request.setHeader(AppConstants.content_type, AppConstants.application_json);
response = httpClient.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
while ((line = in.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
JSONObject jObject = new JSONObject(toReturn);
}
if(jObject.has("error")){
RES_STATUS = AppConstants.FAIL;
}
else{
AppVariables.id = jObject.getString(AppVariables.id_);
AppVariables.createTime = jObject.getString(AppVariables.createTime_);
AppVariables.updateTime = jObject.getString(AppVariables.updateTime_);
AppVariables.status = jObject.getString(AppVariables.status_);
JSONObject oneObject = jObject.getJSONObject("transaction");
JSONObject twoObject = oneObject.getJSONObject("amount");
AppVariables.total = twoObject.getString("total");
AppVariables.currencyCode = twoObject.getString("currencyCode");
AppVariables.qrcode = oneObject.getString(AppVariables.qrcode_);
AppVariables.description = oneObject.getString(AppVariables.description_);
MWLog.e("AppVariables.id", AppVariables.id);
MWLog.e("AppVariables.createTime", AppVariables.createTime);
MWLog.e("AppVariables.updateTime", AppVariables.updateTime);
MWLog.e("AppVariables.status", AppVariables.status);
MWLog.e("AppVariables.currencyCode", AppVariables.currencyCode);
MWLog.e("AppVariables.total", AppVariables.total);
MWLog.e("AppVariables.qrcode", AppVariables.qrcode);
MWLog.e("AppVariables.des", AppVariables.description);
RES_STATUS = AppConstants.SUCCESS;
MWLog.e("BUYER", toReturn);
}
答案 0 :(得分:0)
使用此功能获取正确的json响应
public String getResponseBody(final HttpEntity entity) throws IOException, ParseException {
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
InputStream instream = entity.getContent();
if (instream == null) {
return "";
}
if (entity.getContentLength() > Integer.MAX_VALUE) {
throw new IllegalArgumentException(
"HTTP entity too large to be buffered in memory");
}
StringBuilder buffer = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, HTTP.UTF_8));
String line = null;
try {
while ((line = reader.readLine()) != null) {
buffer.append(line.trim());
}
} finally {
instream.close();
reader.close();
}
return buffer.toString().trim();
}
如何使用?
result= getResponseBody(response.getEntity());
答案 1 :(得分:0)
result = sb.toString();
JSONObject jObject = new JSONObject(toReturn);
不应该
result = sb.toString();
JSONObject jObject = new JSONObject(result);
您正在尝试将String转换为返回,其中包含无效的JSON数据。
答案 2 :(得分:0)
这里Amount
是一个JsonArray,你试图在json对象中强制它
JSONArray JsonArray2 = jsonobject.getJSONArray("amount");
试试这种方式