【问题标题】:Google Maps Geocoding API request issue in Android Studio with okHTTP带有okHTTP的Android Studio中的Google Maps Geocoding API请求问题
【发布时间】: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


    【解决方案1】:

    您的问题: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
    });
    

    【讨论】:

    • 我试过了,但请求仍然失败并出现错误:W/System.err﹕ java.io.IOException: stream was reset: PROTOCOL_ERROR 蓝色。它转到public void onFailure(Request request, IOException throwable) 方法。
    • OkHttpsClient吗?它似乎不存在于 API 中,所以我只使用 OkHttpClient
    • 我猜是这个问题:github.com/square/okhttp/issues/1195。我正在考虑尝试 loopj.com/android-async-http 而不是 okHttp...
    • @NikitaVlasenko 可能是这样,也可能是this。结束评论表明它是一个问题服务器端,而不是库。
    • 那么,您的意思是这可能是 google API 的地理编码问题,我需要使用其他一些 API?这很奇怪。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多