带有HTML标签的Android JSON解析 [英] Android JSON parsing with HTML tags

查看:232
本文介绍了带有HTML标签的Android JSON解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JsonReader从android中<HTML>标记内的URL解析JSON.我已经尝试过关于Stackoverflow和Androud引用的多个示例,但是我不断收到org.json.JSONException: Value <!DOCTYPE错误或null pointer exception

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

这是我要解析的URL:链接

This is the URL that i want to parse: Link

这是我使用此示例的代码:如何在Android中解析JSON

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

我在getData类上得到nullpointexception

I am getting nullpointexception on getData class

JSON解析器类:

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:

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

推荐答案

我使用此方法从JSON响应HTML中剥离HTML:

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

然后检索了JSONArray而不是对象

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

这篇关于带有HTML标签的Android JSON解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆