猿问

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

从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数据的最简单方法是在列表视图中显示它


慕的地10843
浏览 629回答 3
3回答

尚方宝剑之说

它非常易于使用。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();}

阿晨1998

JSONObject(html).getString("name");
随时随地看视频慕课网APP

相关分类

Android
我要回答