2016-07-19 6 views
0

Ich verwende Retrofit2, um eine Netzwerkanforderung zu erstellen, und ich möchte eine Kopfzeile hinzufügen. Nach dem Setzen der Header habe ich einen Logging-Mechanismus gesetzt, um das Log zu sehen. Dies ist, was ich jetzt sehe, keine Header in der Anfrage.Header in Retrofit 2 Protokollierung nicht zu sehen

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 

Die Antwort vom Server:

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 

Hier sind die beteiligten Klassen: ich einen HeaderInterceptor gesetzt, wo ich die Header auf die Anforderung gesetzt und danach die der Protokollierung.

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

    } 
} 

Die Klasse für die Einstellung der Header:

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; 

    } 
} 

ich nicht einmal die Log.d sehen (. "Retrofit", request.headers() toString()). Es scheint, dass die HeaderInterceptor-Klasse nicht instanziiert wird, aber ich bin nicht in der Lage zu sehen, wo der Fehler ist. Danke im Voraus!

Antwort

1

Sorry, ich war die Zuordnung nicht das Ergebnis der Header auf eine variable Einstellung, also auch nicht in der Anfrage:

Lösung:

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()); 
Verwandte Themen