【问题标题】:retrofit , okhttp3 add header改造,okhttp3添加头
【发布时间】:2018-10-23 02:37:48
【问题描述】:

我需要从站点获取 XML 文件。我正在学习使用改造。 我需要通过 "X-AppId" 标头发出请求并附加我的 API 密钥。它应该是这样的:

X-AppId: my key.

如果我从浏览器执行此操作,我会得到答案。 通过改造,我获得了访问权限

错误 403 禁止代码 = 403,消息 = 禁止,url = https://

告诉我如何正确实现从服务器code = 200接收答案

这是我的实现:

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

}

这是我输出到日志的活动:

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

这是一个日志:

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

如果我使用没有标题的第三方网站,我会得到 200 的响应,没有问题。在这种情况下我做错了什么?谢谢。

【问题讨论】:

    标签: android retrofit okhttp3


    【解决方案1】:

    哦,伙计,你在做什么。您可以使用@Query、@Header 等注解。

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

    然后你可以像这样创建请求:

    Retrofit retrofit = new Retrofit.Builder(). 
    .baseUrl("https://rasp.rw.by")
    .addConverterFactory(SimpleXmlConverterFactory.create())
    .build();
    
    retrofit.create(myAPIinterface.class).getProducts(myId, "Minsk", "Warsaw").enqueue ...
    

    它有什么帮助?您忘记在第二次改造时添加标题,然后出现 403 错误。因此,您必须添加注释,这将是您忘记将值放入 header/query/etc 时的最后一个错误。

    【讨论】:

    • 谢谢,现在我从这样的服务器得到答案:code ValueRequiredException: Unable to meet @org.simpleframework.xml.ElementList(data=false, empty=true, entry=, inline=true, name=, required=true, type=void) on field 'products' public code 很明显答案就是从这里来的。做POJO还有待
    猜你喜欢
    • 2016-05-30
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    • 2017-05-21
    • 2020-08-25
    相关资源
    最近更新 更多