在OkHttp工作正常的情况下,Retrofit2响应是错误的请求

时间:2017-02-13 10:42:21

标签: java retrofit retrofit2 okhttp simple-xml-converter

编辑我添加了一个小型演示测试)

我是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以设置headerscookiesAddCookiesInterceptor.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&#58;&#47;&#47;&#37;5bNo&#37;20Host&#37;5d&#47;jsc&#47;xp2&#47;fns&#46;vast&#63;", is invalid.<p>
D/OkHttp: Reference&#32;&#35;9&#46;756f87dd&#46;1487483027&#46;1f41287
 D/OkHttp: </BODY></HTML>

Althogth两个调用都使用相同的GETRetrofit2调用失败了400.可能是Headers或Cookie,但我无法解决。

1 个答案:

答案 0 :(得分:1)

我认为最好在OkHttp中启用日志记录并检查

  1. 请求的网址是正确的
  2. 收到的数据也是正确的。
  3. 有关如何启用OkHttp Loggin的更多信息,请访问:https://github.com/square/okhttp/tree/master/okhttp-logging-interceptor