在 Retrofit 2 日志记录中看不到 Headers

Can't see Headers in Retrofit 2 logging

我正在使用 Retrofit2 发出网络请求,我想向其中添加一个 header。设置 headers 后,我设置了一个日志记录机制来查看日志。这是我现在看到的,没有 header 请求。

07-19 18:17:41.553 23344-24481/com.carlos.capstone.debug D/OkHttp: --> GET http://finance.yahoo.com/webservice/v1/symbols/AMZN,GOOG,ORCL,%5ENDX,/quote?format=json&view=detail http/1.1
07-19 18:17:41.553 23344-24481/com.carlos.capstone.debug D/OkHttp: --> END GET

服务器响应:

07-19 18:17:41.867 23344-24499/com.carlos.capstone.debug D/OkHttp: <-- 406 Not Acceptable http://finance.yahoo.com/webservice/v1/symbols/TX60.TS,%5EGSPTSE,%5EIXIC,%5ENDX,%5EDJI,%5EGSPC,%5EBVSP,%5EMXX,%5EMERV,%5EIPSA/quote?bypass=true&format=json&view=detail (342ms)
07-19 18:17:41.867 23344-24499/com.carlos.capstone.debug D/OkHttp: Date: Tue, 19 Jul 2016 16:17:42 GMT
07-19 18:17:41.867 23344-24499/com.carlos.capstone.debug D/OkHttp: P3P: policyref="http://info.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC GOV"
07-19 18:17:41.867 23344-24499/com.carlos.capstone.debug D/OkHttp: content-length: 21
07-19 18:17:41.867 23344-24499/com.carlos.capstone.debug D/OkHttp: content-type: text/plain; charset=utf-8
07-19 18:17:41.867 23344-24499/com.carlos.capstone.debug D/OkHttp: Expires: -1
07-19 18:17:41.867 23344-24499/com.carlos.capstone.debug D/OkHttp: Vary: X-Ssl
07-19 18:17:41.867 23344-24499/com.carlos.capstone.debug D/OkHttp: Age: 0
07-19 18:17:41.867 23344-24499/com.carlos.capstone.debug D/OkHttp: Via: http/1.1 yts278.global.media.ir2.yahoo.com (ApacheTrafficServer [cMsSf ]), http/1.1 media-router33.prod.media.ir2.yahoo.com (ApacheTrafficServer [cMsSf ]), http/1.1 r05.ycpi.dea.yahoo.net (ApacheTrafficServer [cMsSf ])
07-19 18:17:41.867 23344-24499/com.carlos.capstone.debug D/OkHttp: Server: ATS
07-19 18:17:41.867 23344-24499/com.carlos.capstone.debug D/OkHttp: Cache-Control: max-age=0, private
07-19 18:17:41.867 23344-24499/com.carlos.capstone.debug D/OkHttp: Connection: keep-alive
07-19 18:17:41.868 23344-24499/com.carlos.capstone.debug D/OkHttp: Y-Trace: BAEAQAAAAAALB1fOMgMOYgAAAAAAAAAAiBQMqkYf28UAAAAAAAAAAAAFN_9spyqfAAU3_2ynuqWCC3MYAAAAAA--
07-19 18:17:41.868 23344-24499/com.carlos.capstone.debug D/OkHttp: <-- END HTTP

以下是涉及的 classes: 我设置了一个 HeaderInterceptor,我将 headers 设置为请求,然后是日志记录。

public class IndexOrShortInfoRApi {
    public static IIndexOrShortInfoData myService;
    public interface IIndexOrShortInfoData {

        @GET("/webservice/v1/symbols/TX60.TS,^GSPTSE,^IXIC,^NDX,^DJI,^GSPC,^BVSP,^MXX,^MERV,^IPSA/quote?format=json&view=detail")
        Call<IndexOrShortInfoDataResponse> getIndexesAmerica();
        @GET("/webservice/v1/symbols/^STOXX50E,^FTSE,^GDAXI,^FCHI,^IBEX,FTSEMIB.MI,PSI20.LS,BEL20.BR,^BFX,^SSMI,OBX.OL,RTS.RS,OMXC20.CO,^OMXSPI,^SSMI,FPXAA.PR,GD.AT,^ATX,^ISEQ/quote?format=json&view=detail")
        Call<IndexOrShortInfoDataResponse> getIndexesEurope();
        @GET("/webservice/v1/symbols/^N225,000001.SS,^AXJO,^AORD,^HSI,^BSESN,^NSEI,^NZ50,^TWII,^JKSE,^KLSE,^KS11,^STI,PSEI.PS/quote?format=json&view=detail")
        Call<IndexOrShortInfoDataResponse> getIndexesAsia();
        @GET("/webservice/v1/symbols/{ticker}/quote?format=json&view=detail")
        Call<IndexOrShortInfoDataResponse> getSecurityShortInfoByTicker(@Path("ticker") String ticker);
    }

    public static IIndexOrShortInfoData getMyService(){
        //OkHttpClient client=new OkHttpClient();

        HttpLoggingInterceptor logging=new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);
        Dispatcher dispatcher=new Dispatcher();
        dispatcher.setMaxRequests(3);

        OkHttpClient client=new OkHttpClient.Builder()
                .dispatcher(dispatcher)
                .addInterceptor(new HeaderInterceptor())
                .addInterceptor(logging)
                .build();

        if (myService==null) {
            Retrofit retrofit=new Retrofit.Builder()
                    .baseUrl("http://finance.yahoo.com/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(client)
                    .build();
            myService=retrofit.create(IIndexOrShortInfoData.class);
            return myService;

        } else {
            return myService;
        }

    }
}

设置header的class:

public class HeaderInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request =chain.request();
        request.newBuilder()
                .addHeader("User-Agent","Mozilla/5.0 (Linux; Android 6.0.1; MotoG3 Build/MPI24.107-55) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.81 Mobile Safari/537.36")
                .build();
        Log.d("Retrofit", request.headers().toString());
        Response response=chain.proceed(request);
        return response;

    }
}

我什至看不到 Log.d("Retrofit", request.headers().toString())。 HeaderInterceptor class 似乎没有实例化,但我看不出错误在哪里。提前致谢!

抱歉,我没有将设置 headers 的结果分配给变量,因此没有将其包含在请求中:

解决方案:

 request=request.newBuilder()
                .addHeader("User-Agent","Mozilla/5.0 (Linux; Android 6.0.1; MotoG3 Build/MPI24.107-55) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.81 Mobile Safari/537.36")
                .build();
        Log.d("Retrofit", request.headers().toString());