2

I want to parse a JSON from a url that is inside <HTML> tags in android using JsonReader. I have tried multiple examples on Stackoverflow and Androud reference, but i keep getting org.json.JSONException: Value <!DOCTYPE error or null pointer exception

This is the URL that i want to parse: Link

This is the code i have using this example: How to parse JSON in Android

I am getting nullpointexception on getData class

JSON Parser class:

public class JSONParser
{
    public String json = "";
    public InputStream is = null;
    public JSONObject jObj = null;

public JSONObject getJSON(String url)
{
    try
    {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    } 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) {
       e.printStackTrace();
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    // return JSON String
    return jObj;
}

MapsActivity:

private void setUpMap()
{
    new getData().execute();
}

class getData extends AsyncTask<String, String, String>
{
    @Override
    protected String doInBackground(String... params)
    {
        JSONParser jsonParser = new JSONParser();
        JSONObject json = jsonParser.getJSON(url);

        try
        {
            String id = json.getString("ID");
            String name = json.getString("Name");
            long lat = json.getLong("Lat");
            long lng = json.getLong("Long");
            String sms = json.getString("Sms");
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
        return null;
    }
}
6
  • 1
    JSON Parser only reads values which are in JSON format otherwise it throws an error.. if you are getting HTML tags in response it means that there is some warning or error in your .php page.. first you have to solve this and after that it will gives you response into JSON format Commented Mar 4, 2015 at 5:52
  • please give full logcat...And how you approach to parse this. Commented Mar 4, 2015 at 5:53
  • 1
    Is it just me that finds it rather odd that link returns valid JSON, yet is wrapped in an html page and content type set to text/html, why not just use application/json? I don't see the benefit of making it an html response... Commented Mar 4, 2015 at 5:55
  • 1
    I think the the page is giving you JSONArray object not JSONObject i guess Commented Mar 4, 2015 at 7:48
  • @Preethi Rao, you were right. it was in an array. Commented Mar 4, 2015 at 9:09

5 Answers 5

2

I used this method to strip the HTML from the JSON responsehtml: How to strip or escape html tags in Android

public String stripHtml(String html)
{
    return Html.fromHtml(html).toString();
}

Then retrieved a JSONArray instead of an Object

HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity); 
String noHTML = stripHtml(data);

JSONArray jsonArray = new JSONArray(noHTML);

for(int i = 0; i < jsonArray.length(); i++)
 {
     StopInfo stops = new StopInfo();
     JSONObject jsonObject = jsonArray.getJSONObject(i);

     stops.id = jsonObject.getString("ID");
     stops.name = jsonObject.getString("Name");
     stops.lat = jsonObject.getLong("Lat");
     stops.lng = jsonObject.getLong("Long");
     stops.sms = jsonObject.getString("Sms");

     stopArrayList.add(stops);
 }
Sign up to request clarification or add additional context in comments.

Comments

0

You would need to extract the JSON component from the HTML page. One way of doing it for this structure would be something like:

// If `html` is the full HTML string
String json = html.substring(html.indexOf("["), html.lastIndexOf("]") + 1);

Comments

0

This is web site fault. Json request isn't in html tags. But you can replace it easily from request string. Or you can parse with Jsoup like that:

Document doc = Jsoup.Parse(request);
String parsedrequest = doc.text();

Comments

0

You can execute this as http request to server, Do execute it separate thread(may be a asynctask).

    HttpClient client = new DefaultHttpClient();
    HttpGet getRquest = new HttpGet(urlLink);
    try {
        HttpResponse respnse = client.execute(getRquest);
        StatusLine statusLine = respnse.getStatusLine();
        String responseS = statusLine.getReasonPhrase();
        if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
            HttpEntity entity = respnse.getEntity();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            entity.writeTo(out);

            responseS = out.toString();
            out.close();
            out = null;
        }

Response will be your json string. Your response string is JSONArray so

JSONArray js = new JSONArray(jsonString);

Hope it works

Have a look into your Json data using online json parser

Comments

-1

Looks like youre trying to parse the source code for some reason.

Take a look at this link, it'll guide you through properly parsing JSON formatted response.

3 Comments

This is the one ive tried aswell but got that error :(
could you post the code you're using to grab and parse the response? If you're getting the HTML tags you're somehow grabbing the page source and not the actual content.
Hi i've edited my post to show what ive done from the example

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.