安卓:无法从JSON数据数据、JSON

2023-09-04 05:04:31 作者:红人館

我收到从previous活动,本次活动的一些事件中的一个ID,并通过这个ID到URL中的当前活动获得城市名present在该网址。我的code是..

 的String = getIntent()getStringExtra(AR)。

尝试{
    HttpPost执行hpost =新HttpPost(xxxxxxxxxxxxxxxxx / ID);
    HTT presponse响应= login.client.execute(执行hpost);
    名单<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>(1);
    nameValuePairs.add(新BasicNameValuePair(ID,S));
    hpost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
    串重新= EntityUtils.toString(response.getEntity());
    System.out.print(重);

    JSONObject的根=新的JSONObject(重);
    eveState.setText(root.getString(城市名));
}
赶上(例外五){
    Log.e(exvcx,错误获取数据+ e.toString());
}
 

我得到的例外是,城市名没有任何价值。我想我无法将ID传递给该url..Please告诉这是正确的方法是什么?如果有,请提供解决问题的方法,否则请纠正我,我做错了。谢谢你。

{"status":1,"response":[{"id":"73","name":"Dangerous","address":"Rydgggh","location":"Entry试试,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:2012年7月13日}],ImageWidth等:1024,imageHeight:1024}

解决方案 安卓 json数据简易 格式化打印日志 Logcat

请参考这个答案我给下面的问题

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

我有这样的阵列

  {
结果:成功,
国codeLIST:
[
  {国code:00,国家名称:环球},
  {国code:KR,国家名称:韩国}
]
}
 

下面下面我获取国家信息

 的JSONObject JSON =新的JSONObject(jsonstring);
JSONArray nameArray = json.names();
JSONArray的valarray = json.toJSONArray(nameArray);

JSONArray valArray1 = valArray.getJSONArray(1);

。valArray1.toString()代替([,);
。valArray1.toString()代替(],);

INT的len = valArray1.length();

的for(int i = 0; I< valArray1.length();我++){

 国家国家=新的国家();
 JSONObject的ARR = valArray1.getJSONObject(我);
 country.setCountry code(arr.getString(国code));
 country.setCountryName(arr.getString(国家名称));
 arrCountries.add(国家);
}
 

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..

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

The exception I am getting is that no value for cityname. I think I am unable to pass the id to this url..Please tell is this the right method? If yes please provide solution to the problem otherwise please correct me where I am doing wrong. Thanks.

{"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}

解决方案

Please refer this answer i have given in below question

http://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);
}