I want to get the latitude and longitude , i tyr to use AsyncTask , but i can't see the log value , it's not working
What step do i miss ? any help would be grateful.
my global variable:
double editLatitude, editLongitude;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.traffic_information_fragment, container, false);
mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFrag.getMapAsync(this);
//it's my json url----------------------------------------------
new TrafficInformationTask().execute("http://maps.googleapis.com/maps/api/geocode/json?address=國泰綜合醫院+TW=true_or_false");
return view;
}
it's my AsyncTask:
public class TrafficInformationTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... strings) {
String url = strings[0];
try {
String routeJson = getRoute(url);
return routeJson;
} catch (IOException ex) {
Log.d(TAG, ex.toString());
}
return null;
}
@Override
protected void onPostExecute(String s) {
showRoute(s);
}
}
private String getRoute(String url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
int responseCode = connection.getResponseCode();
StringBuilder jsonIn = new StringBuilder();
if (responseCode == 200) {
BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = bf.readLine()) != null) {
jsonIn.append(line);
}
} else {
Log.d(TAG, responseCode + "responseCode");
}
connection.disconnect();
Log.d(TAG, jsonIn + "jsonIn");
return jsonIn.toString();
}
//it's my parse step
private void showRoute(String route) {
try {
JSONObject jsonObject = new JSONObject(route);
JSONArray resultsArray = jsonObject.getJSONArray("results");
JSONObject zeroJson = resultsArray.getJSONObject(0);
JSONObject geometryJson = zeroJson.getJSONObject("geometry");
editLatitude = geometryJson.getJSONObject("location").getDouble("lat");
editLongitude = geometryJson.getJSONObject("location").getDouble("lng");
//there is no value from log
Log.d("editLatitude",editLatitude+"");
Log.d("editLongitude",editLongitude+"");
} catch (JSONException ex) {
ex.printStackTrace();
}
}