0

我从上一个活动到此活动中获取某个事件的一个 id,并将此 id 传递给当前活动中的 url,以获取该 url 中存在的城市名称。我的代码是..

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

2 回答 2

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);
}
于 2012-07-12T06:24:13.367 回答
-1

我注意到的问题是:

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

使用下面的读取功能

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();
}
于 2012-07-12T07:51:04.373 回答