gpt4 book ai didi

android - 改造 - 基本网址必须以/结尾

转载 作者:行者123 更新时间:2023-12-03 21:20:02 27 4
gpt4 key购买 nike

我已经尝试了该站点上所有可能的解决方案。我以前使用过改造,过去已经解决了这个问题,但这次我无法解决。我正在使用 StackOverflow API 来获取问题及其详细信息。

API : https://api.stackexchange.com/2.2/questions?order=asc&sort=votes&site=stackoverflow&tagged=android&filter=withbody



我用来调用的方法:
    @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) 

最佳答案

.baseUrl()https://api.stackexchange.com/2.2/ ,包括 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 ,它没有标题...所以您可能需要映射到类 ItemsItem .

关于android - 改造 - 基本网址必须以/结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55204527/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com