我的Android应用因为我不理解的错误而崩溃...
String TEST_URL = "http://10.0.2.2:8080";
PersonSvcApi personService = new RestAdapter.Builder()
.setEndpoint(TEST_URL).setLogLevel(LogLevel.FULL).build()
.create(PersonSvcApi.class);
Person person = new Person();
String id = "234";
person.setAge("21");
person.setName("Test");
person.setId(id);
boolean ok = personService.addPerson(person);
Collection<Person> people = personService.getPersonList();
错误出现在最后两行中,似乎我对连接做错了但是我没理由。我的网络服务器正在运行,我已经尝试过使用它,它可以使用简单的java代码。当我用android做的时候我得到这个:
10-27 10:09:20.031: E/AndroidRuntime(548): FATAL EXCEPTION: main
10-27 10:09:20.031: E/AndroidRuntime(548): retrofit.RetrofitError
10-27 10:09:20.031: E/AndroidRuntime(548): at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:395)
10-27 10:09:20.031: E/AndroidRuntime(548): at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
10-27 10:09:20.031: E/AndroidRuntime(548): at $Proxy0.addPerson(Native Method)
10-27 10:09:20.031: E/AndroidRuntime(548): at com.example.cloudapp.MainActivity$2.onClick(MainActivity.java:72)
10-27 10:09:20.031: E/AndroidRuntime(548): at android.view.View.performClick(View.java:3480)
10-27 10:09:20.031: E/AndroidRuntime(548): at android.view.View$PerformClick.run(View.java:13983)
10-27 10:09:20.031: E/AndroidRuntime(548): at android.os.Handler.handleCallback(Handler.java:605)
10-27 10:09:20.031: E/AndroidRuntime(548): at android.os.Handler.dispatchMessage(Handler.java:92)
10-27 10:09:20.031: E/AndroidRuntime(548): at android.os.Looper.loop(Looper.java:137)
10-27 10:09:20.031: E/AndroidRuntime(548): at android.app.ActivityThread.main(ActivityThread.java:4340)
10-27 10:09:20.031: E/AndroidRuntime(548): at java.lang.reflect.Method.invokeNative(Native Method)
10-27 10:09:20.031: E/AndroidRuntime(548): at java.lang.reflect.Method.invoke(Method.java:511)
10-27 10:09:20.031: E/AndroidRuntime(548): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-27 10:09:20.031: E/AndroidRuntime(548): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-27 10:09:20.031: E/AndroidRuntime(548): at dalvik.system.NativeStart.main(Native Method)
10-27 10:09:20.031: E/AndroidRuntime(548): Caused by: java.lang.ExceptionInInitializerError
10-27 10:09:20.031: E/AndroidRuntime(548): at com.google.gson.FieldBindingStrategy$1.matches(FieldBindingStrategy.java:28)
10-27 10:09:20.031: E/AndroidRuntime(548): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:125)
10-27 10:09:20.031: E/AndroidRuntime(548): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:78)
10-27 10:09:20.031: E/AndroidRuntime(548): at com.google.gson.Gson.getAdapter(Gson.java:378)
10-27 10:09:20.031: E/AndroidRuntime(548): at com.google.gson.Gson.toJson(Gson.java:619)
10-27 10:09:20.031: E/AndroidRuntime(548): at com.google.gson.Gson.toJson(Gson.java:605)
10-27 10:09:20.031: E/AndroidRuntime(548): at com.google.gson.Gson.toJson(Gson.java:558)
10-27 10:09:20.031: E/AndroidRuntime(548): at com.google.gson.Gson.toJson(Gson.java:538)
10-27 10:09:20.031: E/AndroidRuntime(548): at retrofit.converter.GsonConverter.toBody(GsonConverter.java:80)
10-27 10:09:20.031: E/AndroidRuntime(548): at retrofit.RequestBuilder.setArguments(RequestBuilder.java:353)
10-27 10:09:20.031: E/AndroidRuntime(548): at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:298)
10-27 10:09:20.031: E/AndroidRuntime(548): ... 14 more
10-27 10:09:20.031: E/AndroidRuntime(548): Caused by: android.os.NetworkOnMainThreadException
10-27 10:09:20.031: E/AndroidRuntime(548): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
10-27 10:09:20.031: E/AndroidRuntime(548): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
10-27 10:09:20.031: E/AndroidRuntime(548): at java.net.InetAddress.getLocalHost(InetAddress.java:371)
10-27 10:09:20.031: E/AndroidRuntime(548): at com.google.gson.util.N.<clinit>(N.java:87)
10-27 10:09:20.031: E/AndroidRuntime(548): ... 25 more
答案 0 :(得分:0)
当您看到“由...引起:android.os.NetworkOnMainThreadException”时,这意味着您正在尝试在主(UI)线程上进行网络调用。网络调用可能需要很短或很长时间才能执行,因此您不希望让用户等待(冻结UI /让应用程序无响应)。
我猜你使用Retrofit进行的网络调用是在onCreate中,或者是由某个事件监听器触发的。您将希望在单独的线程(Thread,AsyncTask)上进行此调用。