我正在尝试在 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
与 Google Geocoding API 的通信通过 https
进行。
这是我尝试使用的代码:
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=...
请问有人能帮忙吗?
最佳答案
你的问题:Caused by: android.os.NetworkOnMainThreadException
你应该使用 OkHttps 异步方法:
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException throwable) {
throwable.printStackTrace();
}
@Override
public void onResponse(Response response) throws IOException {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
// Do Stuff
});
关于android - Android Studio 中使用 okHTTP 的 Google Maps Geocoding API 请求问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33979807/