【发布时间】:2012-07-12 06:04:08
【问题描述】:
我从上一个活动中获取某个事件的一个 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}
【问题讨论】:
-
你能提供你的json字符串吗?因为您可能会得到 json 数组或 json 对象,所以没有 json 我们无法帮助您。但是从您的异常中,您尝试访问 json 中不可用的 id。
-
请提供上一个活动的代码,它正在启动这个活动。我们需要了解您如何将数据传递给此活动。
-
这就是我从以前的活动中获取 id 的方式..
Intent i = new Intent(searchevent.this, eventDetails.class); i.putExtra("ar", id); startActivity(i);
标签: android json http-post httpresponse