我有一种方法,它利用谷歌距离API来计算2个邮政编码之间的距离。我在某种程度上无法得到距离,并在我的尝试捕获中捕获错误。有人能够弄清楚代码有什么问题吗?通过将URL放在浏览器中来测试值,它可以显示结果。我相信obj.getJSONArray给我的大小为0,这就是错误所在。
protected Double doInBackground(String pincode_1, String pincode_2) {
this.pincode_1 = pincode_1;
this.pincode_2 = pincode_2;
try {
String key = "my api key inserted here";
String s = String.format("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=%s&destinations=%s&key=%s", pincode_1, pincode_2, key);
URL url = new URL(s);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
int statusCode = urlConnection.getResponseCode();
if (statusCode == HttpURLConnection.HTTP_OK) {
Scanner sc = new Scanner(urlConnection.getInputStream());
StringBuilder sb = new StringBuilder();
while (sc.hasNext())
sb.append(sc.next());
String json = sb.toString();
JSONObject obj = new JSONObject(json);
double distance = obj.getJSONArray("rows").getJSONObject(0).getJSONArray("elements").getJSONObject(0).getJSONObject("distance").getDouble("value");
urlConnection.disconnect();
sc.close();
return distance;
}
} catch (Exception e) {
Log.e("DistanceError", "Error " +Log.getStackTraceString(e));
return null;
}
return null;
}
错误消息
11-01 14:55:35.528 16725-16725/com.example.utsav.edufind E/DistanceError: Error android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
at java.net.InetAddress.getAllByName(InetAddress.java:752)
at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:345)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:126)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java)
at controller.DistanceCalculator.doInBackground(DistanceCalculator.java:39)
at controller.SearchController.search(SearchController.java:98)
at boundary.SearchResultsUI.initializeData(SearchResultsUI.java:222)
at boundary.SearchResultsUI.onCreate(SearchResultsUI.java:119)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
更新 在我添加了2行代码后,我有此消息错误
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()。permitAll()。build(); StrictMode.setThreadPolicy(策略);
11-01 15:06:38.620 22216-22216/com.example.utsav.edufind E/DistanceError: Error org.json.JSONException: Index 0 out of range [0..0)
at org.json.JSONArray.get(JSONArray.java:293)
at org.json.JSONArray.getJSONObject(JSONArray.java:521)
at controller.DistanceCalculator.doInBackground(DistanceCalculator.java:48)
at controller.SearchController.search(SearchController.java:98)
at boundary.SearchResultsUI.initializeData(SearchResultsUI.java:225)
at boundary.SearchResultsUI.onCreate(SearchResultsUI.java:122)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:1)
您需要创建一个扩展AsyncTask的类,例如:
private class GetDistance extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String pincode_1, String pincode_2) {
this.pincode_1 = pincode_1;
this.pincode_2 = pincode_2;
try {
String key = "my api key inserted here";
String s = String.format("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=%s&destinations=%s&key=%s", pincode_1, pincode_2, key);
URL url = new URL(s);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
int statusCode = urlConnection.getResponseCode();
if (statusCode == HttpURLConnection.HTTP_OK) {
Scanner sc = new Scanner(urlConnection.getInputStream());
StringBuilder sb = new StringBuilder();
while (sc.hasNext())
sb.append(sc.next());
String json = sb.toString();
JSONObject obj = new JSONObject(json);
double distance = obj.getJSONArray("rows").getJSONObject(0).getJSONArray("elements").getJSONObject(0).getJSONObject("distance").getDouble("value");
urlConnection.disconnect();
sc.close();
return distance;
}
} catch (Exception e) {
Log.e("DistanceError", "Error " +Log.getStackTraceString(e));
return null;
}
return null;
}
@Override
protected void onPostExecute(String result) {
//do what you want with the distance on the main thread
}
}
}
将方法移动到此类后,可以实例化它并调用execute。这应该至少处理你的NetworkOnMainThreadException。