Android:无法从json检索数据

Android:无法从json检索数据,android,json,http-post,httpresponse,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&l

我正在从上一个活动获取某个事件的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());
}

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

我有这个数组

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

jsonobjectjson=newjsonobject(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
我注意到的问题有:

  • 在执行HTTPPost请求后,可以设置
    id
    参数
  • 不从服务器输入流读取响应。您是否尝试将读取的数据打印到console,并验证它是否正确
使用下面的读取功能

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

你能提供你的json字符串吗?因为您可能会得到json数组或json对象,所以如果没有json,我们无法帮助您。但是从您的异常中,您试图访问json中不可用的id。请提供启动此活动的上一个活动的代码。我们需要了解您是如何将数据传递给此活动的。这是我如何从以前的活动获取id的..
Intent i=new Intent(searchevent.this,eventDetails.class);i、 putExtra(“ar”,id);星触觉(i)
07-12 12:11:02.441:ERROR/exvcx(3273):获取dataorg.json.JSONException时出错:java.lang.Integer类型的1处的值0无法转换为JSONArray
JSONObject json=new JSONObject(re);JSONArray nameArray=json.names();JSONArray valArray=json.toJSONArray(nameArray);JSONArray valArray1=valArray.getJSONArray(1);valArray1.toString().replace(“[”,”);valArray1.toString().replace(“]”,”);int len=valArray1.length();对于(int i=0;i
此代码中的哪一行出现上述错误?上面提到的json响应字符串是正确的吗?是的,响应字符串是正确的。并在
JSONArray valArray1=valArray.getJSONArray(1)上获取错误
JSONArray valArray1=valArray.getJSONArray(0);这将解决您的问题,而不是1写0problem@downvoter我可以知道这次否决投票的原因吗?这样我才知道问题所在
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;
}
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();
}