0

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}
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
arsh.somal
  • 61
  • 1
  • 13
  • can you please provide your json string? Because you might get json array or json object so without json we cant help you out. But from your exception you are trying to access id which is not available in the json. – Silvans Solanki Jul 12 '12 at 06:10
  • Please provide the code for the previous Activity, which is launching this one. We need to understand how you are passing the data to this activity. – Romin Jul 12 '12 at 06:15
  • this is how i am getting the id from previous activity..`Intent i = new Intent(searchevent.this, eventDetails.class); i.putExtra("ar", id); startActivity(i);` – arsh.somal Jul 12 '12 at 06:18

2 Answers2

2

Please refer this answer i have given in below question

https://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);
}
Community
  • 1
  • 1
Nirali
  • 13,571
  • 6
  • 40
  • 53
  • `07-12 12:11:02.441: ERROR/exvcx(3273): error getting dataorg.json.JSONException: Value 0 at 1 of type java.lang.Integer cannot be converted to JSONArray ` – arsh.somal Jul 12 '12 at 06:42
  • `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(); for (int i = 0; i < valArray1.length(); i++) { JSONObject arr = valArray1.getJSONObject(i); eveNam.setText(arr.getString("cityname")); } ` – arsh.somal Jul 12 '12 at 06:49
  • in this code which line are you getting the above error? and what your json response string that you mentioned above is correct na? – Nirali Jul 12 '12 at 06:57
  • yeah the response string is correct. and getting error on `JSONArray valArray1 = valArray.getJSONArray(1);` – arsh.somal Jul 12 '12 at 07:12
  • can you debugg and check what value you are getting in "valArray". Because then only we can check what exactly you are getting at "1" position of valArray.getJSONArray(1) – Nirali Jul 12 '12 at 07:35
  • `07-12 13:21:14.534: INFO/varray(5187): value[[{"location":"Entry try","createdate":"2012-07-11 06:24:31","formatteddate":"July 13, 2012","block":"n","image":"4ffd626f021487.45227344.jpg","phnumber":"2467568","deleted":"n","id":"73","interestname":"Comedy","username":"princeb","address":"Rydgggh","name":"Dangerous","statename":"Alaska","eventdate":"2012-07-13 15:45:29","cityname":"Juneau"}],1024,1,1024] ` – arsh.somal Jul 12 '12 at 07:52
  • 1
    JSONArray valArray1 = valArray.getJSONArray(0); instead of 1 write 0 this will solve your problem – Nirali Jul 12 '12 at 08:02
-1

problems i noticed are :

  • you set the id parameter after executing the HTTPPost request.
  • not reading the response from the server input stream. have you tried to print the read data to console and verified it is the correct one.

use the read function below

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;
}
  • now you will get the JSON string wrapped in a JSONObject
  • now traverse this object to get the cityname

code sample

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();
}
sunil
  • 6,444
  • 1
  • 32
  • 44