Android - 使用谷歌地图api和json获取距离

时间:2016-09-01 13:31:31

标签: java android json google-maps

我可以使用以下代码从网上检索数据:

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void onClick (View v){
    new GetMethodDemo().execute("https://maps.googleapis.com/maps/api/directions/json?origin=Breclav&destination=Brno&sensor=false&units=metric&mode=driving");

}

public class GetMethodDemo extends AsyncTask<String , Void ,String> {
    String server_response;

    @Override
    protected String doInBackground(String... strings) {

        URL url;
        HttpURLConnection urlConnection = null;

        try {
            url = new URL(strings[0]);
            urlConnection = (HttpURLConnection) url.openConnection();

            int responseCode = urlConnection.getResponseCode();

            if(responseCode == HttpURLConnection.HTTP_OK){
                server_response = readStream(urlConnection.getInputStream());

                Log.v("CatalogClient", server_response);
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        Log.e("Response", "" + server_response);
    }
}

private String readStream(InputStream in) {
    BufferedReader reader = null;
    StringBuffer response = new StringBuffer();
    try {
        reader = new BufferedReader(new InputStreamReader(in));
        String line = "";
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return response.toString();

}       }

我能够在调试中看到我收到了像link这样的数据,但我发现我应该使用下面的代码来选择与我收到的数据的距离。不知道如何将这些代码组合在一起..我想在textView中有两个城市之间距离url的距离。

 final JSONObject json = new JSONObject(response);
 JSONArray routeArray = json.getJSONArray("routes");
 JSONObject routes = routeArray.getJSONObject(0);

 JSONArray newTempARr = routes.getJSONArray("legs");
 JSONObject newDisTimeOb = newTempARr.getJSONObject(0);

 JSONObject distOb = newDisTimeOb.getJSONObject("distance");
 JSONObject timeOb = newDisTimeOb.getJSONObject("duration");

 Log.i("Diatance :", distOb.getString("text"));
 Log.i("Time :", timeOb.getString("text"));

我不是这些代码的作者......谢谢

0 个答案:

没有答案
相关问题