我正在使用Retrofit2发出网络请求,我想为其添加一个标头。设置标头后,我设置了一个日志记录机制来查看日志。这就是我现在看到的,请求中没有标题。
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
以下是涉及的课程: 我设置了一个HeaderInterceptor,我将标头设置为请求,之后是日志记录。
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;
}
}
}
用于设置标题的类:
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(&#34; Retrofit&#34;,request.headers()。toString())。似乎HeaderInterceptor类没有实例化,但我无法看到错误的位置。提前谢谢!
答案 0 :(得分:0)
很抱歉,我没有将标头设置为变量的结果,因此不在请求中包含它:
溶液:
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());