简体   繁体   中英

retrofit , okhttp3 add header

I need to get the XML file from the site. I'm learning to use Retrofit. I need to make a request and attach my API key via the "X-AppId" header. It should look like this:

X-AppId: my key.

If I do this from the browser, I get the answer. Through the retrofit I get the access

error 403 Forbidden code = 403, message = Forbidden, url = https: //

Tell me how it is implemented properly to receive an answer from the server code = 200

Here is my implementation:

public interface myAPIinterface {
@GET("/api/ru/index/route/?from=Minsk&to=Warsaw")
Call<Routes> getProducts();

}

This is the activity where I output to the log:

private void getProducts(){
    final ProgressDialog loading = ProgressDialog.show(this,"Fetching Data","Please wait...",false,false);
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    Log.d(TAG, "getProducts");
    httpClient.addInterceptor(new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request request = chain.request()
                    .newBuilder()
                    .addHeader("X-AppId:", "97377f7b702d7198e47a2bf12eec74")
                    .build();
            return chain.proceed(request);
        }
    });

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://rasp.rw.by")
            .addConverterFactory(SimpleXmlConverterFactory.create())
            .build();
    myAPIinterface api = retrofit.create(myAPIinterface.class);
Call<Routes> call = api.getProducts();
    call.enqueue(new Callback<Routes>() {
        @Override
        public void onResponse(@NonNull Call<Routes> call, @NonNull Response<Routes> response) {
            Log.d(TAG, "onResponse");
            Log.d(TAG, String.valueOf(kk));
            Log.d(TAG, String.valueOf(response));
            loading.dismiss();}
       @Override
        public void onFailure(Call<Routes> call, Throwable throwable) {
            loading.dismiss();
            Log.d(TAG, "onFailure" + throwable);
        }
    });

this is a log:

Response{protocol=http/1.1, code=403, message=Forbidden, url= https://rasp.rw.by/api/ru/index/route/?from=Minsk&to=Warsaw }

if I take third-party sites where there are no headers, I get a response of 200 without problems. What am I doing wrong in this case? Thank you.

Oh, man, what are you doing. You can use annotations like @Query, @Header, etc.

public interface myAPIinterface {

    @GET("/api/ru/index/route")
    Call<Routes> getProducts(@Header("X-AppId:") String YOUR_APP_ID,
                             @Query("from") String from,
                             @Query("to") String to)
}

Then you can create request like this:

Retrofit retrofit = new Retrofit.Builder(). 
.baseUrl("https://rasp.rw.by")
.addConverterFactory(SimpleXmlConverterFactory.create())
.build();

retrofit.create(myAPIinterface.class).getProducts(myId, "Minsk", "Warsaw").enqueue ...

How It can help? You forgot to add header at second retrofit and then you have 403 error. So, You must add annotations, and this will be the last mistake when you forgot to put value to header/query/etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM