Android JSON parsing with HTML tags

好久不见. 提交于 2019-12-13 02:15:54

问题


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;
    }
}

回答1:


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



回答2:


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



回答3:


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



回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/28847407/android-json-parsing-with-html-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!