Android: unable to retrieve data from json

后端 未结 2 917
说谎
说谎 2021-01-26 13:17

I am getting one id of some event from the previous activity to this activity and passing this id to the url in current activity to get cityname present in that url. My code is.

相关标签:
2条回答
  • 2021-01-26 13:38

    Please refer this answer i have given in below question

    https://stackoverflow.com/a/11260845/1441666

    I have this array

    {
    "result": "success",
    "countryCodeList":
    [
      {"countryCode":"00","countryName":"World Wide"},
      {"countryCode":"kr","countryName":"Korea"}
    ] 
    }
    

    Here below I am fetching country details

    JSONObject json = new JSONObject(jsonstring);
    JSONArray nameArray = json.names();
    JSONArray valArray = json.toJSONArray(nameArray);
    
    JSONArray valArray1 = valArray.getJSONArray(1);
    
    valArray1.toString().replace("[", "");
    valArray1.toString().replace("]", "");
    
    int len = valArray1.length();
    
    for (int i = 0; i < valArray1.length(); i++) {
    
     Country country = new Country();
     JSONObject arr = valArray1.getJSONObject(i);
     country.setCountryCode(arr.getString("countryCode"));                        
     country.setCountryName(arr.getString("countryName"));
     arrCountries.add(country);
    }
    
    0 讨论(0)
  • 2021-01-26 13:43

    problems i noticed are :

    • you set the id parameter after executing the HTTPPost request.
    • not reading the response from the server input stream. have you tried to print the read data to console and verified it is the correct one.

    use the read function below

    public static JSONObject read(String url, String id){
    
        InputStream is = null;
        String result = "";
        // http post
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
    
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("id", id));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        // convert response to string
        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();
            result = sb.toString();
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        JSONObject jsonObject=null;
        try {
            jsonObject = new JSONObject(result);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    
        return jsonObject;
    }
    
    • now you will get the JSON string wrapped in a JSONObject
    • now traverse this object to get the cityname

    code sample

    try {
        JSONObject jsonObject = new JSONObject(json);
        JSONArray jsonArray = jsonObject.optJSONArray("response");
        String cityName = ((JSONObject)jsonArray.get(0)).getString("cityname");
        System.out.println("Cityname : "+cityName);
    } catch (JSONException e2) {
        e2.printStackTrace();
    }
    
    0 讨论(0)
提交回复
热议问题