Sakhawat Hossain
Sakhawat Hossain

Reputation: 463

Retrofit - Base url must end in /

I have tried all the possible solutions available on this site. I have used retrofit before and already solved that problem in the past but this time I wasn't able to solve it. I am using StackOverflow API to get the question and their details.

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

Method that I am using to call:

    @GET
    Call<Item> getAllQuestionsInformation(@Url String url);

My retrofit instance code with logging interceptor:

        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());
            }
        });

The error that I am getting:

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) 

Upvotes: 5

Views: 7149

Answers (2)

Martin Zeitler
Martin Zeitler

Reputation: 76699

The .baseUrl() is https://api.stackexchange.com/2.2/, including the API version, which should be the same for each request and it's also less effort to update to newer API versions. The request should look about like this:

@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
);

That response.body().getTitle() seems wrong, because the root node of the response JSON is items, which does not have a title... so you might need to map to classes Items and Item.

Upvotes: 4

Ali dashti
Ali dashti

Reputation: 430

your base url is https://api.stackexchange.com/2.2/.

and also in GET you should do like this: @GET(questions)

visit this link

Upvotes: 3

Related Questions