android_rst
Lurker
I need to pass value of distance obtained from doInBackground() to another activity in android programming. I tried creating object of this class in the activity class and then calling like obj.distance but it did not worked out.I would appreciate any help.
Code:
public class DistanceActivity extends AsyncTask<String, Void,String> {
public static String distance;
[USER=1021285]@override[/USER]
protected String doInBackground(String... urls) {
URL a = null;
try {
a = new URL(urls[0]);
HttpURLConnection c1 = null;
c1 = (HttpURLConnection) a.openConnection();
c1.setRequestMethod("GET");
c1.connect();
int responseCode = c1.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader br = null;
br = new BufferedReader(new InputStreamReader(c1.getInputStream()));
StringBuilder sb = new StringBuilder();
String s, s1 = "";
while ((s1 = br.readLine()) != null) {
sb.append(s1);
}
String jsonStr = sb.toString();
System.out.println("JSONstr : " + jsonStr);
distance = parseJSON(jsonStr);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("DISTANCE = " + distance);
//return (Double.parseDouble(String.valueOf(d)));
return distance;
}
private String parseJSON(String s) {
String d = "";
try {
JSONObject ob = new JSONObject(s);
JSONArray jarray = ob.getJSONArray("destination_addresses");
d = ob.getJSONArray("rows").getJSONObject(0).getJSONArray("elements").getJSONObject(0).getJSONObject("distance").get("value").toString();
System.out.println("Inside parser Distance=" + d);
} catch (JSONException e) {
e.printStackTrace();
}
return d;
}
Last edited by a moderator: