Android 将字符串转换为JSONObject时出错
我的android应用程序有一些问题。我目前有一个包含JSON的字符串Android 将字符串转换为JSONObject时出错,android,json,Android,Json,我的android应用程序有一些问题。我目前有一个包含JSON的字符串 HttpResponse response = null; HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://www.brocksportfolio.com/GetPendingRequests.php"); List<NameValue
HttpResponse response = null;
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.brocksportfolio.com/GetPendingRequests.php");
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("Username", "Brock"));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
response = httpClient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
} catch (IOException e) {
e.printStackTrace();
}
String jsonStr = response.toString();
我得到了这个错误
org.json.JSONException: Value org.apache.http.message.BasicHttpResponse@3526f881 of type java.lang.String cannot be converted to JSONObject
我相信这个错误的原因是因为我正在将HTTPPostResponse转换为字符串,然后试图将该字符串传递给JSONObject,但我真的不知道如何修复它。谢谢你的帮助 试一试:
try {
response = httpClient.execute(httpPost);
String responseBody = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
JSONObject jsonObj = new JSONObject(responseBody);
实际消息包含在
BasicHttpReponse
保存的中。您可以从HttpEntity检索InputStream,从其内容生成字符串,然后从中生成JSONObject。
当然,您需要首先检查响应代码(即,它是否为200?)。所有这些都非常麻烦,而且容易出错
我建议您改为签出,一个简单的JsonObjectRequest可以像这样简单(从链接的开发人员文档中):
stringurl=”http://my-json-feed";
JsonObjectRequest jsObjRequest=新的JsonObjectRequest
(Request.Method.GET,url,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
mTxtDisplay.setText(“响应:+Response.toString());
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
//TODO自动生成的方法存根
}
});
duplicate的可能重复:jsonString的价值是多少?@Fahim感谢您的支持,但下面的答案似乎有效!我添加了一个问题的评论,该问题是在修复之前遇到的问题后发生的,无法修复,因此,如果您能提供帮助,我们将不胜感激!这对转化有用!我还有一个刚刚发生的问题。我现在知道这个问题了。org.json.JSONException:org.json.JSONArray类型的值[{“fromUser”:“Andrew”}、{“fromUser”:“Jimmy”}、{“fromUser”:“Mac”}]无法转换为JSONObject,代码行是this pendingRequests=jsonObj.getJSONArray(TAG_fromUser);有什么想法吗?字符串是一个JSONArray,但您正试图将其转换为一个JSONObject。@P-aBäckström我该怎么做才能修复它。JSONArray JSONArray=新JSONArray(ResponseBy);JSONArray是一个数组,所以为了获取对象,可以这样做:JSONObject jsonObj=JSONArray.getJSONObject(0);
try {
response = httpClient.execute(httpPost);
String responseBody = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
JSONObject jsonObj = new JSONObject(responseBody);
String url = "http://my-json-feed";
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
mTxtDisplay.setText("Response: " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});