I'm using Retrofit to consume a REST API endpoint, and I'm having the following issue. I think the something is wrong with data model on TransactionResponse class, but not sure yet.
java.io.EOFException: End of input at line 1 column 1 path $ in Android retrofit
My call request is as follows.
@FormUrlEncoded
@POST("/ifapi.php")
Call<TransactionResponse> loadData(@Field("user") TransactionRequest user);
TransactionRequest class :
import javax.annotation.Generated;
@Generated("org.jsonschema2pojo")
public class TransactionResponse {
private Settings settings;
/**
*
* @return
* The settings
*/
public Settings getSettings()
{
return settings;
}
/**
*
* @param settings
* The settings
*/
public void setSettings(Settings settings)
{
this.settings = settings;
}
private Actions actions;
/**
*
* @return
* The actions
*/
public Actions getActions()
{
return actions;
}
/**
*
* @param actions
* The actions
*/
public void setActions(Actions actions)
{
this.actions = actions;
}
}
And below is the code where call is actually done.
OkHttpClient httpClient = new OkHttpClient.Builder().build();
Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(
RestAPI.BASE_URL).client(httpClient).build();
RestAPI service = retrofit.create(RestAPI.class);
Call<TransactionResponse> meResponse = service.loadData(request);
meResponse.enqueue(new Callback<TransactionResponse>() {
@Override
public void onResponse(Call<TransactionResponse> call, Response<TransactionResponse> response) {
if (response.isSuccessful()) {
TransactionResponse body = response.body();
Actions actions = body.getActions();
Map<String, String> params = actions.getParams();
}
}
@Override
public void onFailure(Call<TransactionResponse> call, Throwable t){
t.printStackTrace();
}
});
Can anyone please help me? Thanks in advance.
I'm using the following libraries.
compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'javax.annotation:javax.annotation-api:1.2'
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
P.S I found that the request worked with form data, but not with JSON data.
I have found the solution, you need to post each parameters in the form as follows, not in a wrapper class.
@FormUrlEncoded
@POST("/ifapi.php")
Call<TransactionResponse> loadData(@Field("app_id") String app_id, @Field("install_id") String install_id, @Field("useragent") String useragent, @Field("ip") String ip, @Field("mccmnc") String mccmnc, @Field("action") String action );
And for main Activity Part.
OkHttpClient httpClient = new OkHttpClient.Builder().build();
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(gson)).baseUrl(RestAPI.BASE_URL).client(httpClient).build();
RestAPI service = retrofit.create(RestAPI.class);
Call<TransactionResponse> meResponse = service.loadData("1", getUUID(), getUserAgent(), getIPAddress(), "20404", "start");
Hope this will resolve your issues, Regards.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With