Android:无法从json检索数据

时间:2012-07-12 06:04:08

标签: android json http-post httpresponse

我从前一个活动获得一个事件的id到此活动,并将此id传递给当前活动中的url,以获取该URL中的cityname。我的代码是......

String s = getIntent().getStringExtra("ar");

try{          
    HttpPost hpost = new HttpPost("xxxxxxxxxxxxxxxxx/id");
    HttpResponse response = login.client.execute(hpost);
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("id", s));
    hpost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    String re = EntityUtils.toString(response.getEntity());
    System.out.print(re);

    JSONObject root = new JSONObject(re);  
    eveState.setText(root.getString("cityname"));  
}
catch(Exception e){
    Log.e("exvcx", "error getting data" +e.toString());
}

我得到的例外是cityname没有价值。我想我无法将id传递给这个url ..请告诉我这是正确的方法吗?如果是,请提供问题的解决方案,否则请纠正我在哪里做错了。感谢。

{"status":1,"response":[{"id":"73","name":"Dangerous","address":"Rydgggh","location":"Entry try","phnumber":"2467568","createdate":"2012-07-11 06:24:31","image":"4ffd626f021487.45227344.jpg","block":"n","deleted":"n","cityname":"Juneau","statename":"Alaska","interestname":"Comedy","username":"princeb","eventdate":"2012-07-13 15:45:29","formatteddate":"July 13, 2012"}],"imageWidth":1024,"imageHeight":1024}

2 个答案:

答案 0 :(得分:2)

请参考我在下面的问题中给出的答案

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

我有这个数组

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

以下是我提取国家/地区详情

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

答案 1 :(得分:-1)

我注意到的问题是:

  • 在执行HTTPPost请求后设置id参数。
  • 未读取服务器输入流的响应。您是否尝试将读取数据打印到控制台并验证它是否正确。

使用下面的阅读功能

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;
}
  • 现在您将获得包装在JSONObject
  • 中的JSON字符串
  • 现在遍历此对象以获取cityname

代码示例

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