(编辑我添加了一个小型演示测试)
我是Retrofit
的新手。
我需要做的是从服务器检索XML
文件并将其转换为POJO。问题是我的测试服务器上的所有内容都与静态文件完美配合,但是当我在真实服务器上尝试此操作时,我无法使其与Retrofit2
一起使用,但是当我直接使用时它确实有效使用OkHttp
,因此我猜测生成的Request
出了问题。
编辑:我将代码切换为使用字符串转换器,因为问题仅在于响应。
这是我的基本实现。
public interface ZedoClient {
@retrofit2.http.Headers({
"User-Agent: Mozilla/5.0 (Linux; Android 5.1; Quattro_L50_HD LMY47D/Quattro_L50_HD)",
"Cookie : ZCBC=1;FFcat=3533,3,90;FFad=0;FFMChanCap=5345280B3533,3#2605191|0,1#0,24:"
})
@GET("fns.vast")
Call<Vast> getVast(@QueryMap(encoded=true) Map<String, String> options);
我添加了两个Interceptor
以设置headers
和cookies
:
AddCookiesInterceptor.class
class AddCookiesInterceptor implements Interceptor {
private final Headers headers;
AddCookiesInterceptor(Headers headers) {
this.headers = headers;
}
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
builder.headers(headers);
return chain.proceed(builder.build());
}
}
ReceivedCookiesInterceptor.class
class ReceivedCookiesInterceptor implements Interceptor {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.Response originalResponse = chain.proceed(chain.request());
if (!originalResponse.headers("Set-Cookie").isEmpty()) {
HashSet<String> cookies = new HashSet<>();
for (String header : originalResponse.headers("Set-Cookie")) {
cookies.add(header);
}
}
return originalResponse;
}
}
以下是我所做的测试,它显示了原始OkHttp
如何按预期工作,但retrofit2
失败了:
public static Vast test(String baseUrl, Map<String, String> queryParams, final Headers headers) {
try {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient httpClient = new OkHttpClient.Builder().addInterceptor(logging).build();
Request.Builder request = new Request.Builder()
.url("http://********/fns.vast?v=vast2&d=90&ct=mnf%3AKARBONN%5Emdn%3AQuattro_L50_HD%5Emdu%3AQuattro_L50_HD%5Etyp%3ASmartPhone%5Econ%3Awifi%5Eosn%3AAndroid%5Evsn%3A5.1%5Eosv%3A22%5Ecn%3AUS%5Elng%3Aen%5E&s=3&n=3533&c=3&z=0.4933822377892413");
if (headers != null) {
request.headers(headers);
}
okhttp3.Response oldRespnse = httpClient.newCall(request.build()).execute();
System.out.println("successsForOkhttp: " + oldResponse.isSuccessful());
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
if (headers != null) {
clientBuilder.networkInterceptors().add(logging);
clientBuilder.networkInterceptors().add(new AddCookiesInterceptor(headers));
clientBuilder.networkInterceptors().add(new ReceivedCookiesInterceptor());
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(clientBuilder.build())
.addConverterFactory(SimpleXmlConverterFactory.create())
.build();
ZedoClient client = retrofit.create(ZedoClient.class);
Call<Vast> call = client.getVast(queryParams);
Response<Vast> response = call.execute();
System.out.println("successsForRetrofit: " + response.isSuccessful());
if (response.isSuccessful()) {
return response.body();
} else {
MyLog.e("error:" + response.raw().message());
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
这是输出:
I/System.out: successsForOkhttp: true
I/System.out: successsForRetrofit: false
I/System.out: error:Bad Request
根据建议,我为Logging-Interceptor
请求和OkHttp
请求添加了Retrofit2
,这是输出:
D/OkHttp: --> GET http://**********/fns.vast?v=vast2&d=90&ct=mnf%3AKARBONN%5Emdn%3AQuattro_L50_HD%5Emdu%3AQuattro_L50_HD%5Etyp%3ASmartPhone%5Econ%3Awifi%5Eosn%3AAndroid%5Evsn%3A5.1%5Eosv%3A22%5Ecn%3AUS%5Elng%3Aen%5E&s=3&n=3533&c=3&z=0.4933822377892413 http/1.1
D/OkHttp: User-Agent: Mozilla/5.0 (Linux; Android 5.1; Quattro_L50_HD LMY47D/Quattro_L50_HD)
D/OkHttp: Cookie: ZCBC=1;FFcat=3533,3,90;FFad=0;FFMChanCap=5345280B3533,3#2605191|0,1#0,24:
D/OkHttp: --> END GET
D/OkHttp: <-- 200 OK http://**********/fns.vast?v=vast2&d=90&ct=mnf%3AKARBONN%5Emdn%3AQuattro_L50_HD%5Emdu%3AQuattro_L50_HD%5Etyp%3ASmartPhone%5Econ%3Awifi%5Eosn%3AAndroid%5Evsn%3A5.1%5Eosv%3A22%5Ecn%3AUS%5Elng%3Aen%5E&s=3&n=3533&c=3&z=0.4933822377892413 (720ms)
D/OkHttp: --> GET http://**********/fns.vast?v=vast2&d=90&ct=mnf%3AKARBONN%5Emdn%3AQuattro_L50_HD%5Emdu%3AQuattro_L50_HD%5Etyp%3ASmartPhone%5Econ%3Awifi%5Eosn%3AAndroid%5Evsn%3A5.1%5Eosv%3A22%5Ecn%3AUS%5Elng%3Aen%5E&s=3&n=3533&c=3&z=0.4933822377892413 http/1.1
D/OkHttp: User-Agent: Mozilla/5.0 (Linux; Android 5.1; Quattro_L50_HD LMY47D/Quattro_L50_HD)
D/OkHttp: Cookie : ZCBC=1;FFcat=3533,3,90;FFad=0;FFMChanCap=5345280B3533,3#2605191|0,1#0,24:
D/OkHttp: Host: xp1.zedo.com
D/OkHttp: Connection: Keep-Alive
D/OkHttp: Accept-Encoding: gzip
D/OkHttp: --> END GET
D/OkHttp: <-- 400 Bad Request http://**********/fns.vast?v=vast2&d=90&ct=mnf%3AKARBONN%5Emdn%3AQuattro_L50_HD%5Emdu%3AQuattro_L50_HD%5Etyp%3ASmartPhone%5Econ%3Awifi%5Eosn%3AAndroid%5Evsn%3A5.1%5Eosv%3A22%5Ecn%3AUS%5Elng%3Aen%5E&s=3&n=3533&c=3&z=0.4933822377892413 (208ms)
D/OkHttp: Server: ******Host
D/OkHttp: Mime-Version: 1.0
D/OkHttp: Content-Type: text/html
D/OkHttp: Content-Length: 272
D/OkHttp: Expires: Sun, 19 Feb 2017 05:43:47 GMT
D/OkHttp: Date: Sun, 19 Feb 2017 05:43:47 GMT
D/OkHttp: Connection: close
D/OkHttp: <HTML><HEAD>
D/OkHttp: <TITLE>Invalid URL</TITLE>
D/OkHttp: </HEAD><BODY>
D/OkHttp: <H1>Invalid URL</H1>
D/OkHttp: The requested URL "http://%5bNo%20Host%5d/jsc/xp2/fns.vast?", is invalid.<p>
D/OkHttp: Reference #9.756f87dd.1487483027.1f41287
D/OkHttp: </BODY></HTML>
Althogth两个调用都使用相同的GET
,Retrofit2
调用失败了400.可能是Headers
或Cookie,但我无法解决。
答案 0 :(得分:1)
我认为最好在OkHttp中启用日志记录并检查
有关如何启用OkHttp Loggin的更多信息,请访问:https://github.com/square/okhttp/tree/master/okhttp-logging-interceptor