0

I am making an android application and this is the first error it is throwing:

Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

This is my class function:

package com.ernest.httppost;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    // function get json from url
    // by making HTTP POST or GET method
    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }else if(method == "GET"){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }          

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }


        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

I think it is returning that error at jObj = new JSONObject(json); in the last try-catch block. Any ideas how to solve this?

5
  • Java is a programming language. Eclipse is an IDE (Integrated Development Environment). Commented Jan 13, 2013 at 13:09
  • 2
    I think the string you are constructing the JSONObject with, should be a valid json string. Since the error states the value <br cannot be converted I guess you are trying to construct a json object from a HTML string. Make sure you are passing a correctly formatted json string. For example: "{\"name\":\"John Ernest Guadalupe\", \"reputation\":181, \"messages\":[\"msg 1\",\"msg 2\",\"msg 3\"]}" Commented Jan 13, 2013 at 13:09
  • @Jules you should write an answer Commented Jan 13, 2013 at 13:13
  • @Ondra I don't think the android tag is relevant, even though it's an Android application. Commented Jan 13, 2013 at 13:19
  • Right... I got this habit automated too much. Commented Jan 13, 2013 at 13:22

2 Answers 2

3

Looks like your response with <br is not JSON, so this parsing error is expected.

Are you hitting the correct URL with the correct payload in your request? Try debugging to see what you are requesting and what the response is - the mistake will probably be obvious.

Sign up to request clarification or add additional context in comments.

3 Comments

<br is definitely not valid JSON
I am sure the URL is correct, but I don't know what you mean by payload. How do I format it into a JSONobject string or something?
I mean the parameter key/value pairs of the request - it's a payload in a POST request, or just encoded in the URL for a GET. Your code is already putting params into the request, question is, are they correct? As in, will they produce the JSON response you need?
2

comment promoted to answer:

I think the string you are constructing the JSONObject with, should be a valid json string. Since the error states the value <br cannot be converted I guess you are trying to construct a json object from a HTML string. Make sure you are passing a correctly formatted json string. For example:

"{\"name\":\"John Ernest Guadalupe\", \"reputation\":181, \"messages\":[\"msg 1\",\"msg 2\",\"msg 3\"]}" 

Like davnicwil said: The problem is probably being cause by the URL not solely returning a correctly formatted JSON string.

6 Comments

@John The json string should be generated by the page specified by the url. Could you share the url you are using with us? (Or just the output if it's a local url)
those are local resources (on your local network) so I can't view them from here. could you please visit any of the pages in your browser, then view source, and paste the source here? Best update your answer with the output.
Actually they are php functions. How will I be able to view the results? I don't know my way around Eclipse very well
just go to the url in you web browser. (firefox, chrome, internet explorer, safari, whatever). Then right click on the page and click view page source. You will see the generated html. That's the content generated by the php functions, and also the content that will be used to pass to the JSONObject. So it is important that it will look like a valid Json string, and not some html codes (which will most likely be the case.) If you don't know how to write PHP, then I'm afraid you are lost at this project, because it's the PHP that will be generating the strings for your json obects.
|

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.