gpt4 book ai didi

android - 从谷歌方向网络服务解析 json 文件

转载 作者:行者123 更新时间:2023-11-29 00:25:43 25 4
gpt4 key购买 nike

我想创建一个 android 应用程序,用户可以在其中选择起点和终点,然后该应用程序将计算到达目的地所需的距离和时间。

为了做到这一点,我使用了谷歌方向网络服务。

这是示例请求:

JSON data of Google Direction API

返回对象为json对象。

我不知道如何向网络服务请求以及如何解析json对象。

提前致谢。

最佳答案

//try this way
String response = new JSONParser().getJSONFromUrl(makeURL(startLatitude,startLongitude,endLatitude,endtLongitude),new responseListener() {
@Override
public void onResponseComplete(String response) {
try {
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
JSONArray step = new JSONObject(response).getJSONArray("routes").getJSONObject(0).getJSONArray("legs")
.getJSONObject(0).getJSONArray("steps");

for (int i = 0; i < step.length(); i++) {
HashMap<String,Object> row = new HashMap<String,Object>();
row.put("address", step.getJSONObject(i).getString("html_instructions"));
row.put("start",new LatLng(Double.parseDouble(step.getJSONObject(i).getJSONObject("start_location").getString("lat")), Double.parseDouble(step.getJSONObject(i).getJSONObject("start_location").getString("lng"))));
row.put("end", new LatLng(Double.parseDouble(step.getJSONObject(i).getJSONObject("start_location").getString("lat")), Double.parseDouble(step.getJSONObject(i).getJSONObject("start_location").getString("lng"))));
list.add(row);

}

}catch (Exception e){
e.printStackTrace();
}
}
});
public String makeURL(double startLatitude, double startLongitude, double endLatitude, double endLongitude) {
StringBuilder localStringBuilder = new StringBuilder();
localStringBuilder.append("http://maps.googleapis.com/maps/api/directions/json");
localStringBuilder.append("?origin=");
localStringBuilder.append(Double.toString(startLatitude));
localStringBuilder.append(",");
localStringBuilder.append(Double.toString(startLongitude));
localStringBuilder.append("&destination=");
localStringBuilder.append(Double.toString(endLatitude));
localStringBuilder.append(",");
localStringBuilder.append(Double.toString(endLongitude));
localStringBuilder.append("&sensor=false&mode=driving&alternatives=true");
return localStringBuilder.toString();
}

public class JSONParser {
InputStream is = null;
JSONObject jObj = null;
String json = "";

public JSONParser() {
}

public void getJSONFromUrl(final String url, final responseListener target) {
new AsyncTask<Void, Void, String>() {
protected String doInBackground(Void... params) {
HttpURLConnection httpURLConnection = null;
StringBuilder stringBuilder = new StringBuilder();
try {
httpURLConnection = (HttpURLConnection) new URL(url).openConnection();
InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());

int read;
char[] buff = new char[1024];
while ((read = inputStreamReader.read(buff)) != -1) {
stringBuilder.append(buff, 0, read);
}
return stringBuilder.toString();
} catch (MalformedURLException localMalformedURLException) {
return "";
} catch (IOException localIOException) {
return "";
} finally {
if (httpURLConnection != null)
httpURLConnection.disconnect();
}
}

protected void onPostExecute(String result) {
super.onPostExecute(result);
target.onResponseComplete(result);
}
}.execute();
}
}

interface responseListener{
public void onResponseComplete(String response);
}

关于android - 从谷歌方向网络服务解析 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19651886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com