【发布时间】:2015-11-29 05:45:54
【问题描述】:
我正在尝试在 Android Studio 中使用 okHTTP (http://square.github.io/okhttp/) 从 Google Maps Geocoding API 获取 JSON,但我的程序崩溃并显示以下错误日志:
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4020)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.squareup.okhttp.Dns$1.lookup(Dns.java:39)
at com.squareup.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:184)
at com.squareup.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:153)
at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:95)
at com.squareup.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:345)
at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328)
at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
at com.squareup.okhttp.Call.getResponse(Call.java:276)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:234)
at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:196)
at com.squareup.okhttp.Call.execute(Call.java:79)
...
最后一个错误日志的 Caused by 暗示问题可能出在 okHTTP 安全通信中。我没有设置它,因为我不知道我是否需要这样做以及如何去做。此页面可能相关,但我不确定:https://github.com/square/okhttp/wiki/HTTPS
通过https 与 Google Geocoding API 进行通信。
这是我尝试使用的代码:
OkHttpClient client = new OkHttpClient();
String strAddress = "Winnetka";
String googleMapsAPIKey = this.getString(R.string.google_maps_api_key);
String url = null;
try {
url = "https://maps.googleapis.com/maps/api/geocode/json" +
"?address=" + URLEncoder.encode(strAddress, "UTF-8") + "&key=" + googleMapsAPIKey;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
Request request = new Request.Builder()
.url(url)
.build();
try {
Response response = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
return null;
}
在线Response response = client.newCall(request).execute();失败
我注销了我形成的 url,它看起来与 API 网站 (https://developers.google.com/maps/documentation/geocoding/intro) 上的示例中使用的相同:
https://maps.googleapis.com/maps/api/geocode/json?address=Winnetka&key=...
有人可以帮忙吗?
【问题讨论】:
标签: android android-studio google-geocoding-api