2013-01-08 369 views
0

可能重複:
android.os.NetworkOnMainThreadException谷歌JSON解析問題

我決定使用google api client。我從this example得到一個解析JSON的類,它在android 2.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) 
+1

檢查此:[android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) –

+0

使用GSON它比任何其他json庫好多了! – binnyb

回答

1

這不是一個JSON/GSON問題。這個問題,你是在UI線程上進行網絡調用。這是早期版本的Android中的一個警告 - 並且在更高版本的SDK中更改爲編譯錯誤(我認爲是3.0)。

底線,移動您的網絡調用關閉UI線程(使用AsyncTask)來解決這個問題。