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.
problems i noticed are :
id
parameter after executing the HTTPPost request.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 nameValuePairs = new ArrayList(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
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();
}