0

I'm having some issues with my android application. I currently have a String with my JSON in it.

 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();

I am trying to initialize a JSONObject with that string in it like so.

JSONObject jsonObj = new JSONObject(jsonString);

I am getting this error.

org.json.JSONException: Value org.apache.http.message.BasicHttpResponse@3526f881 of type java.lang.String cannot be converted to JSONObject

I believe the reason for this error is because I am converting an HTTPPostResponse to a string and then trying to pass that string through to a JSONObject but i'm just really not sure how to fix it. Thanks for the help!

4
  • possible duplicate of How to print out returned message from HttpResponse? Commented Mar 18, 2015 at 17:14
  • Duplicate: stackoverflow.com/a/2573112/3761545 Commented Mar 18, 2015 at 17:14
  • What is the value of jsonString? Commented Mar 18, 2015 at 17:14
  • @Fahim thanks for the support but looks like the answer below worked! I added a comment of a problem that occured after fixing this problem that I ran into earlier and couldn't fix so help on that would be much appreciated as well! Commented Mar 18, 2015 at 17:20

2 Answers 2

2

Give this a shot:

try {
    response = httpClient.execute(httpPost);
    String responseBody = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
    e.printStackTrace();
}

JSONObject jsonObj = new JSONObject(responseBody);
Sign up to request clarification or add additional context in comments.

6 Comments

This worked for converting! I have one more problem that just occured. I'm now getting this issue. org.json.JSONException: Value [{"fromUser":"Andrew"},{"fromUser":"Jimmy"},{"fromUser":"Mac"}] of type org.json.JSONArray cannot be converted to JSONObject And the line of code is this pendingRequests = jsonObj.getJSONArray(TAG_FROMUSER); Any ideas?
The string is an JSONArray, but you are trying to convert it to a JSONObject.
@P-aBäckström what shall I do to fix that.
JSONArray jsonArray = new JSONArray(responseBody);
A JSONArray is an array, so in order to get your object you can do like this: JSONObject jsonObj = jsonArray.getJSONObject(0);
|
0

The actual message is contained by the HttpEntity which the BasicHttpReponse holds. You can retrieve an InputStream from the HttpEntity, make a String out of its contents and then make a JSONObject out of it. Of course you will need to check the response code (i.e. is it a 200?) first. All of this is quite cumbersome and error-prone.

I would recommend you to check out Volley instead, a simple JsonObjectRequest can be as easy as this (from the linked developer docs):

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

    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.