Retrofit2格式错误的响应

时间:2017-10-05 06:41:20

标签: android retrofit2

我已经尝试了一些不同的事情但没有成功,所以我想改用其他的库并尝试,但我会先把它留在这里以防万一。

我有一个应用程序可以对3个不同的URL进行2次GET和2次POST,而我在使用最后一个时遇到了问题。

我有这个Retrofit POST方法:

@Headers({
            "Accept: text/html",
            "Accept-Encoding: gzip, deflate, br",
            "Accept-Language: es-ES,es;q=0.8",
            "Cache-Control: no-cache",
            "Content-Type: application/x-www-form-urlencoded"
    })
    @FormUrlEncoded
    @POST("index.php?operacion=consulta")
    Call<String> postRaiaSearch(@Header("Cookie") String cookie, @Field("microchip") String microchip);

而且,在我解释这个问题之前,我想澄清一下,我已经尝试使用Call<String>来改变Call<ResponseBody>,或者将有效负载发送为@Field(使用@FormUrlEncoded并且Content-Type标题为application/x-www-form-urlencoded),也类似于@Body RequestBody

但是一切都给了我同样错误的格式化响应。

以下是格式良好的高级REST客户端响应正文的示例:

RAW: enter image description here

解析: enter image description here

Retrofit给我的怪物:

enter image description here

正如您所看到的,第一个坏处是Web返回HTML而不是JSON,但我认为我可以将其作为原始纯文本获取,但这并不顺利。

如果您想知道,这就是我制作Retrofit对象的方式:

public static Retrofit getRaiaApi() {
    if (raiaRetrofit == null) {
        raiaRetrofit = new Retrofit.Builder()
                .baseUrl(RAIA_URL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }
    return raiaRetrofit;
}

我还尝试添加.addConverterFactory(GsonConverterFactory.create()),或删除Scalars行(仅限Gson),甚至只删除它们。

我认为你真的不需要调用POST方法的方法,但无论如何我都会粘贴它:

private void nextRaiaSearch(String header) {
    callRaiaSearch = apiInterfaceRaia.postRaiaSearch(header, chipInput);
    callRaiaSearch.enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            Log.v("call", "onResponse");
            Log.v("call", response.body());
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            Log.v("call", "onFailure");
        }
    });
}

我应该为这一个请求尝试像Volley或任何其他东西而不是Retrofit吗?

进口产品是:

compile 'com.google.code.gson:gson:2.8.2'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:converter-scalars:2.3.0'

1 个答案:

答案 0 :(得分:2)

好的,我得到了一个解决方案,我收到的回应一塌糊涂。问题是POST方法的标题之一:

Accept-Encoding: gzip, deflate, br

我被告知放置的标题,但我根本不需要,因为它正在压缩文本。现在我得到了正常的响应答案,就像在原始的高级REST客户端响应中一样:

enter image description here

相关问题