wVV wVV - 9 months ago 31
Android Question

Enable to process a json response in retrofit2?

1) Json response.

{
"customer_info": {
"customer_id": "1",
"customer_group_id": "1",
"store_id": "0",
"firstname": "john",
"lastname": "kam",
"email": "johnoconner@gmail.com",
"telephone": "+3458690730867",
"fax": "",
"password": "c5bf3e452f02e3c639ce8513245d500cfdfbad9c",
"salt": "qXXoVmh0j",
"cart": null,
"wishlist": null,
"newsletter": "0",
"address_id": "1",
"custom_field": "",
"ip": "45.113.251.60",
"status": "1",
"approved": "1",
"verified": "0",
"safe": "0",
"token": "",
"date_added": "2017-03-08 15:49:28"
},
"error_warning": "",
"redirect": "",
"success": "",
"email": "johnoconner@gmail.com",
"password": "12345678"
}


2)Using http://www.jsonschema2pojo.org/ for making a java class.

3)

Gson gson = new GsonBuilder()
.setLenient()
.create();

Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http://thefruitbowl.in/?route=api/web_api/")
.addConverterFactory(GsonConverterFactory.create(gson));

Retrofit retrofit = builder.build();

LoginInterface loginInterface=retrofit.create(LoginInterface.class)

Call<Example> call = loginInterface.checkUserExistOrNot("abc@gmail.com", "12345");

call.enqueue(new Callback<Example>() {
@Override
public void onResponse(Call<Example> call, Response<Example> response) {

Log.i("response is",response.body().toString());
}

@Override
public void onFailure(Call<Example> call, Throwable t) {
t.printStackTrace();
}
});


4)The error i get is

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

5) Scratch to many thing but didn't understant what was the wrong.

6) i'm attaching my java class also.
6.1)https://www.dropbox.com/s/ni5bdrwo9zsfwkv/CustomerInfo.java?dl=0
6.2)https://www.dropbox.com/s/fjsi4yqct5ppp23/Example.java?dl=0

Answer Source

Your base url must be http://thefruitbowl.in/ and your endpoint ?route=api/web_api/login.

The retrofit call

public void retrofitTest()
{
    Gson gson = new GsonBuilder()
            .setLenient()
            .create();

    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("http://thefruitbowl.in/") // not the base url
            .addConverterFactory(GsonConverterFactory.create(gson));

    Retrofit retrofit = builder.build();

    LoginInterface loginInterface=retrofit.create(LoginInterface.class);
    Call<Example> exampleCall = loginInterface.checkUserExistOrNot("abc@gmail.com", "12345");
    exampleCall.enqueue(new Callback<Example>() {
        @Override
        public void onResponse(Call<Example> call, retrofit2.Response<Example> response) {
            Example example = response.body();
            Log.i("response",example.getEmail());
        }

        @Override
        public void onFailure(Call<Example> call, Throwable t) {

        }
    });
}

And the interface

public interface LoginInterface
{

    @GET("?route=api/web_api/login")
    @Headers("Content-Type: application/json")
    Call<Example> checkUserExistOrNot(@Query("email") String email, @Query("password") String password);
}

And i do get the response

 response: abc@gmail.com