为了账号安全,请及时绑定邮箱和手机立即绑定

从Android上的URL简单解析JSON并在listview中显示

从Android上的URL简单解析JSON并在listview中显示

慕的地10843 2019-08-17 15:26:38
从Android上的URL简单解析JSON并在listview中显示我正在尝试解析从我的Android应用程序中的URL获取的JSON结果...我在互联网上尝试了一些例子,但无法让它发挥作用。JSON数据如下所示:[     {         "city_id": "1",         "city_name": "Noida"     },     {         "city_id": "2",         "city_name": "Delhi"     },     {         "city_id": "3",         "city_name": "Gaziyabad"     },     {         "city_id": "4",         "city_name": "Gurgaon"     },     {         "city_id": "5",         "city_name": "Gr. Noida"     }]获取URL并解析JSON数据的最简单方法是在列表视图中显示它
查看完整描述

3 回答

?
尚方宝剑之说

TA贡献1788条经验 获得超4个赞

它非常易于使用。

public class JSONParser {static InputStream is = null;static JSONObject jObj = null;static String json = "";// constructorpublic JSONParser() {}// function get json from url// by making HTTP POST or GET methodpublic JSONObject makeHttpRequest(String url, String method,
        List<NameValuePair> params) throws IOException {

    // 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(httpParameters);
            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 (Exception ex) {
        Log.d("Networking", ex.getLocalizedMessage());
        throw new IOException("Error connecting");
    }

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

然后在您的应用程序中,创建此类的实例。如果需要,您可能希望传递构造函数“GET”或“POST”。

public JSONParser jsonParser = new JSONParser();try {

    // Building Parameters ( you can pass as many parameters as you want)
    List<NameValuePair> params = new ArrayList<NameValuePair>();

    params.add(new BasicNameValuePair("name", name));
    params.add(new BasicNameValuePair("age", 25));

    // Getting JSON Object
    JSONObject json = jsonParser.makeHttpRequest(YOUR_URL, "POST", params);} catch (JSONException e) {
    e.printStackTrace();}


查看完整回答
反对 回复 2019-08-17
?
阿晨1998

TA贡献2037条经验 获得超6个赞

JSONObject(html).getString("name");


查看完整回答
反对 回复 2019-08-17
  • 3 回答
  • 0 关注
  • 628 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信