0

重複の可能性:
android.os.NetworkOnMainThreadException

私はグーグルAPIクライアントを使用することにしました。この例からJSONを解析するクラスを取得しましたが、Android2.2では正常に機能します。ただし、Android 3.xまたは4.xでテストすると、クラッシュしました。それを修正する方法は?

public class GooglePlaces {

    /** Global instance of the HTTP transport. */
    private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();

    // Google API Key
    private static final String API_KEY = "AIzaSyCRLa4LQZWNQBcjCYcIVYA45i9i8zfClqc"; 
    private static final String PLACES_SEARCH_URL = "https://maps.googleapis.com/maps/api/place/search/json?";
    private static final String PLACES_SEARCH_GEOPOINT_URL = "http://maps.googleapis.com/maps/api/geocode/json?";
    private static final String PLACES_TEXT_SEARCH_URL = "https://maps.googleapis.com/maps/api/place/search/json?";
    private static final String PLACES_DETAILS_URL = "https://maps.googleapis.com/maps/api/place/details/json?";
    private static final String DIRECTION_URL = "https://maps.googleapis.com/maps/api/directions/json?";
    private double _latitude;
    private double _longitude;
    private double _radius;
    public String test = new String("");

    public PlacesList search(double latitude, double longitude, double radius, String types) throws Exception {

        this._latitude = latitude;
        this._longitude = longitude;
        this._radius = radius;

        try {

            HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
            HttpRequest request = httpRequestFactory.buildGetRequest(new GenericUrl(PLACES_SEARCH_URL));
            request.getUrl().put("key", API_KEY);
            request.getUrl().put("location", _latitude + "," + _longitude);
            request.getUrl().put("radius", _radius); // in meters
            request.getUrl().put("sensor", "false");
            if (types != null)
                request.getUrl().put("types", types);

            PlacesList list = request.execute().parseAs(PlacesList.class);
            // Check log cat for places response status
            Log.d("Places Status", "" + list.status);
            return list;

        } catch (HttpResponseException e) {
            Log.e("Error:", e.getMessage());
            return null;
        }
    }


    public static HttpRequestFactory createRequestFactory(final HttpTransport transport) {
        return transport.createRequestFactory(new HttpRequestInitializer() {
            public void initialize(HttpRequest request) {
                GoogleHeaders headers = new GoogleHeaders();
                headers.setApplicationName("AndroidHive-Places-Test");
                request.setHeaders(headers);
                JsonObjectParser parser = new JsonObjectParser(new JacksonFactory());
                request.setParser(parser);
            }
        });
    }
}

これは私がインポートしたライブラリです:

google-api-client-1.10.3-beta.jar
google-api-client-android2-1.10.3-beta.jar
google-http-client-1.10.3-beta.jar
google-http-client-android2-1.10.3-beta.jar
google-oauth-client-1.10.1-beta.jar
gson-2.1.jar
guava-11.0.1.jar
jackson-core-asl-1.9.4.jar
jsr305-1.3.9.jar
protobuf-java-2.2.0.jar

これはlogcatです

01-08 22:20:55.318: E/InputEventReceiver(771): Exception dispatching input event.
01-08 22:20:55.318: E/MessageQueue-JNI(771): Exception in MessageQueue callback: handleReceiveCallback
01-08 22:20:55.416: E/MessageQueue-JNI(771): android.os.NetworkOnMainThreadException
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:90)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:895)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at com.ftravelbook.json.GooglePlaces.getInfo(GooglePlaces.java:50)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at com.ftravelbook.maps.google.GoogleActivity$Touchy.onTouchEvent(GoogleActivity.java:356)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at com.google.android.maps.OverlayBundle.onTouchEvent(OverlayBundle.java:63)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at com.google.android.maps.MapView.onTouchEvent(MapView.java:681)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.View.dispatchTouchEvent(View.java:7127)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.View.dispatchPointerEvent(View.java:7307)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3174)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3119)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4155)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4134)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4226)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.os.MessageQueue.nativePollOnce(Native Method)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.os.MessageQueue.next(MessageQueue.java:125)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.os.Looper.loop(Looper.java:124)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at android.app.ActivityThread.main(ActivityThread.java:4745)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at java.lang.reflect.Method.invokeNative(Native Method)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at java.lang.reflect.Method.invoke(Method.java:511)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-08 22:20:55.416: E/MessageQueue-JNI(771):    at dalvik.system.NativeStart.main(Native Method)
01-08 22:20:55.416: D/AndroidRuntime(771): Shutting down VM
01-08 22:20:55.426: W/dalvikvm(771): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
01-08 22:20:55.476: E/AndroidRuntime(771): FATAL EXCEPTION: main
01-08 22:20:55.476: E/AndroidRuntime(771): android.os.NetworkOnMainThreadException
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
01-08 22:20:55.476: E/AndroidRuntime(771):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-08 22:20:55.476: E/AndroidRuntime(771):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-08 22:20:55.476: E/AndroidRuntime(771):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-08 22:20:55.476: E/AndroidRuntime(771):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
01-08 22:20:55.476: E/AndroidRuntime(771):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-08 22:20:55.476: E/AndroidRuntime(771):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
01-08 22:20:55.476: E/AndroidRuntime(771):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
01-08 22:20:55.476: E/AndroidRuntime(771):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-08 22:20:55.476: E/AndroidRuntime(771):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
01-08 22:20:55.476: E/AndroidRuntime(771):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
01-08 22:20:55.476: E/AndroidRuntime(771):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
01-08 22:20:55.476: E/AndroidRuntime(771):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
01-08 22:20:55.476: E/AndroidRuntime(771):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
01-08 22:20:55.476: E/AndroidRuntime(771):  at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:90)
01-08 22:20:55.476: E/AndroidRuntime(771):  at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:895)
01-08 22:20:55.476: E/AndroidRuntime(771):  at com.ftravelbook.json.GooglePlaces.getInfo(GooglePlaces.java:50)
01-08 22:20:55.476: E/AndroidRuntime(771):  at com.ftravelbook.maps.google.GoogleActivity$Touchy.onTouchEvent(GoogleActivity.java:356)
01-08 22:20:55.476: E/AndroidRuntime(771):  at com.google.android.maps.OverlayBundle.onTouchEvent(OverlayBundle.java:63)
01-08 22:20:55.476: E/AndroidRuntime(771):  at com.google.android.maps.MapView.onTouchEvent(MapView.java:681)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.View.dispatchTouchEvent(View.java:7127)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
01-08 22:20:55.476: E/AndroidRuntime(771):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
01-08 22:20:55.476: E/AndroidRuntime(771):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
01-08 22:20:55.476: E/AndroidRuntime(771):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.View.dispatchPointerEvent(View.java:7307)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3174)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3119)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4155)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4134)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4226)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.os.MessageQueue.nativePollOnce(Native Method)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.os.MessageQueue.next(MessageQueue.java:125)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.os.Looper.loop(Looper.java:124)
01-08 22:20:55.476: E/AndroidRuntime(771):  at android.app.ActivityThread.main(ActivityThread.java:4745)
01-08 22:20:55.476: E/AndroidRuntime(771):  at java.lang.reflect.Method.invokeNative(Native Method)
01-08 22:20:55.476: E/AndroidRuntime(771):  at java.lang.reflect.Method.invoke(Method.java:511)
01-08 22:20:55.476: E/AndroidRuntime(771):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-08 22:20:55.476: E/AndroidRuntime(771):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-08 22:20:55.476: E/AndroidRuntime(771):  at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

1

これはJSON/GSONの問題ではありません。問題は、UIスレッドでネットワーク呼び出しを行っていることです。これは、以前のバージョンのAndroidでは警告でしたが、後のバージョンのSDK(3.0だと思います)ではコンパイルエラーに変更されました。

結論として、これを修正するには、ネットワーク呼び出しをUIスレッドから(AsyncTaskを使用して)移動します。

于 2013-01-08T15:28:30.930 に答える