Md. Sajedul Karim Md. Sajedul Karim - 5 months ago 29
Android Question

Json Object Request using Retrofit2

I am new in retrofit. I completed all setup.
I add this gradle in build.gradle file

compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'


My Interface is like this:

public interface ILoginInterface {
String BASE_URL= "MY_BASE_URL/";

@POST("MY/API")
Call<LoginResponseEntity> startLogin(@Body JSONObject jsonObject);

class Factory{
private static ILoginInterface instance;

public static ILoginInterface getInstance(){
if(instance==null){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();

instance = retrofit.create(ILoginInterface.class);
}

return instance;
}

}
}


My Calling procedure is like this:

ILoginInterface.Factory.getInstance().startLogin(jsonObject).enqueue(new Callback<LoginResponseEntity>() {
@Override
public void onResponse(Call<LoginResponseEntity> call, retrofit2.Response<LoginResponseEntity> response) {
Log.d("MS",response.body().fullName);
}

@Override
public void onFailure(Call<LoginResponseEntity> call, Throwable t) {
Log.d("MS",t.getMessage());
}
});


Here
jsonObject
is like this:

{"user_name":"sajedul Karim", "password":"123456"}


Here it seems everything is ok but i didn't getting proper response.
I found a solution. it is here . Does anybody have proper solution like Volley JsonObjectRequest

Answer

May be you are importing

    import org.json.JSONObject;

you should use

    import com.google.gson.JsonObject;

Then you will get it's value.

Comments