retrofit.RetrofitError 在 Android 项目中
retrofit.RetrofitError in Android project
我是 Retrofit 的新手。尝试时如果没有收到 retrofit.RetrofitError 和 NetworkOnMainThreadException 就无法发出请求:
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(Url.BASE_URL)
.build();
Api api = restAdapter.create(Api.class);
for(Url url : Url.values()) {
Journal journal = api.getJournalInfo(url.getUrl()); //Here error
System.out.println("journal : " + journal.getTitle());
}
这里是class期刊:
public class Journal {
private String title;
private String description;
private String image;
public String getTitle() {
return title;
}
}
class Url:
public enum Url {
ECONAMICS("economics.json"),
PHYSICS("mathematical.json"),
MATHEMATICAL("physics.json");
public static final String BASE_URL = "https://dl.dropboxusercontent.com/u/17192683/rest/journals/";
private String url;
Url(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
}
接口Api:
public interface Api {
@POST("/{url}")
public Journal getJournalInfo(@Path("url") String urlJournal);
}
完全异常:
03-21 23:44:43.824 22782-22782/tim_kriuchkov.com.task E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{tim_kriuchkov.com.task/tim_kriuchkov.com.task.MainActivity}: retrofit.RetrofitError
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.access0(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: retrofit.RetrofitError
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:400)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at $Proxy0.getJournalInfo(Native Method)
at tim_kriuchkov.com.task.MainActivity.onCreate(MainActivity.java:66) // here error
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
异常的下一部分:
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:469)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:292)
at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:505)
at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:134)
at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
最后一部分:
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at $Proxy0.getJournalInfo(Native Method)
at tim_kriuchkov.com.task.MainActivity.onCreate(MainActivity.java:66) // here error
你愿意帮我吗?我花了很多时间...
您 运行 您的 http 调用 不是 在单独的线程中,而是在主 android UI 线程中,android UI 线程被 android os(因此例外)。
更改您的 Api 界面以使用 Retrofits Callback
。这将异步执行您的请求,并在成功或出错时调用回调。
public interface Api {
@POST("/{url}")
public void getJournalInfo(@Path("url") String urlJournal, Callback<Journal> callback);
}
我是 Retrofit 的新手。尝试时如果没有收到 retrofit.RetrofitError 和 NetworkOnMainThreadException 就无法发出请求:
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(Url.BASE_URL)
.build();
Api api = restAdapter.create(Api.class);
for(Url url : Url.values()) {
Journal journal = api.getJournalInfo(url.getUrl()); //Here error
System.out.println("journal : " + journal.getTitle());
}
这里是class期刊:
public class Journal {
private String title;
private String description;
private String image;
public String getTitle() {
return title;
}
}
class Url:
public enum Url {
ECONAMICS("economics.json"),
PHYSICS("mathematical.json"),
MATHEMATICAL("physics.json");
public static final String BASE_URL = "https://dl.dropboxusercontent.com/u/17192683/rest/journals/";
private String url;
Url(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
}
接口Api:
public interface Api {
@POST("/{url}")
public Journal getJournalInfo(@Path("url") String urlJournal);
}
完全异常:
03-21 23:44:43.824 22782-22782/tim_kriuchkov.com.task E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{tim_kriuchkov.com.task/tim_kriuchkov.com.task.MainActivity}: retrofit.RetrofitError
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.access0(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: retrofit.RetrofitError
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:400)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at $Proxy0.getJournalInfo(Native Method)
at tim_kriuchkov.com.task.MainActivity.onCreate(MainActivity.java:66) // here error
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
异常的下一部分:
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:469)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:292)
at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:505)
at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:134)
at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
最后一部分:
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at $Proxy0.getJournalInfo(Native Method)
at tim_kriuchkov.com.task.MainActivity.onCreate(MainActivity.java:66) // here error
你愿意帮我吗?我花了很多时间...
您 运行 您的 http 调用 不是 在单独的线程中,而是在主 android UI 线程中,android UI 线程被 android os(因此例外)。
更改您的 Api 界面以使用 Retrofits Callback
。这将异步执行您的请求,并在成功或出错时调用回调。
public interface Api {
@POST("/{url}")
public void getJournalInfo(@Path("url") String urlJournal, Callback<Journal> callback);
}