Reputation: 481
I have tried some different things already with no success so I was thinking of changing to some other library and try, but I will leave this here first just in case.
I have an app that makes 2 GET and 2 POST to 3 different URLs, and I'm having trouble with the last one.
I have this Retrofit POST method:
@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);
And, before I explain the problem I want to clarify that I already tried changin Call<String>
with Call<ResponseBody>
, or sending the payload as @Field
(with @FormUrlEncoded
and the Content-Type
header as application/x-www-form-urlencoded
) and also like a @Body
with RequestBody
.
But everything gives to me the same wrong formatted response.
Here is a sample of the Advanced REST Client response body nicely formatted:
And the monster that Retrofit is giving to me:
As you can see, the first bad thing is that the web returns an HTML instead of a JSON, but I thought I could just obtain it as raw plain text, but that did not go well.
In case you wonder, this is how I made the Retrofit object:
public static Retrofit getRaiaApi() {
if (raiaRetrofit == null) {
raiaRetrofit = new Retrofit.Builder()
.baseUrl(RAIA_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
return raiaRetrofit;
}
And I also tried adding .addConverterFactory(GsonConverterFactory.create())
, or deleting the Scalars line (with the Gson one only), or even just deleting both of them.
I don't think you really need the method that's calling that POST method, but I will paste it anyway:
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");
}
});
}
Should I try something like Volley or any other thing instead of Retrofit just for this one request?
The imports made are:
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'
Upvotes: 3
Views: 745
Reputation: 481
Ok, I got a solution for that response mess I was recieving. The problem was one of the headers of the POST method:
Accept-Encoding: gzip, deflate, br
A header I was told to put but I didn't need at all, since it was compressing the text. Now I get normal response answers, just like in the raw Advanced REST Client response:
Upvotes: 2