我已经尝试了该站点上所有可能的解决方案。我以前使用过改造,过去已经解决了该问题,但是这次我无法解决。我正在使用StackOverflow API来获取问题及其详细信息。
我用来调用的方法:
@GET
Call<Item> getAllQuestionsInformation(@Url String url);
我的带有日志拦截器的改造实例代码:
String url="https://api.stackexchange.com/2.2/questions?order=asc&sort=votes&site=stackoverflow&tagged=android&filter=withbody/";
builder=new OkHttpClient.Builder();
HttpLoggingInterceptor httpLoggingInterceptor=new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(httpLoggingInterceptor);
Retrofit retrofit=new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.client(builder.build())
.build();
allApiService=retrofit.create(AllApiService.class);
final Call<Item> itemCall=allApiService.getAllQuestionsInformation(url);
itemCall.enqueue(new Callback<Item>() {
@Override
public void onResponse(Call<Item> call, Response<Item> response) {
Log.v("Info",""+response.body().getTitle());
questionTitleArrayList.add(response.body().getTitle());
}
@Override
public void onFailure(Call<Item> call, Throwable t) {
Log.v("Error",""+t.getMessage());
}
});
我遇到的错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{app.shakil.com.retrofit_paginationrecyclerview/app.shakil.com.retrofit_paginationrecyclerview.MainActivity}: java.lang.IllegalArgumentException: baseUrl must end in /: https://api.stackexchange.com/2.2/questions?order=asc&sort=votes&site=stackoverflow&tagged=android&filter=withbody/
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.IllegalArgumentException: baseUrl must end in /: https://api.stackexchange.com/2.2/questions?order=asc&sort=votes&site=stackoverflow&tagged=android&filter=withbody/
at retrofit2.Retrofit$Builder.baseUrl(Retrofit.java:513)
at retrofit2.Retrofit$Builder.baseUrl(Retrofit.java:456)
at app.shakil.com.retrofit_paginationrecyclerview.MainActivity.getQuestionInformationWithTitle(MainActivity.java:57)
at app.shakil.com.retrofit_paginationrecyclerview.MainActivity.onCreate(MainActivity.java:36)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
答案 0 :(得分:2)
.baseUrl()
是https://api.stackexchange.com/2.2/
,包括API版本,每个请求的API版本应该完全相同,并且更新新API版本的工作也较少。该请求应如下所示:
@GET("questions")
Call<Items> getQuestions(
@Query(value = "order") String sortOrder,
@Query(value = "sort") String sortField,
@Query(value = "site") String site,
@Query(value = "tagged") String tagged,
@Query(value = "filter") String filter
);
response.body().getTitle()
似乎也是错误的,因为响应JSON的根节点是items
,它没有标题...因此您可能需要类Items
和{{ 1}}。
答案 1 :(得分:1)