我从前一个活动获得一个事件的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}
答案 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)
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;
}
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();
}