the response from the server as below:
{
success: true,
token: "someRandomToken"
}
How can I retrieve token
from the response?
I tried to follow the solution provided here but I can't add generic to the Call
inside onResponse method
EDIT
Here is my method
public void loginUser(final Context context, String url, String username, String passowrd, loginJson loginjson) {
final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
loginjson.setUsername(username);
loginjson.setPassword(passowrd);
Gson gson = new Gson();
String jsonFromForm = gson.toJson(loginjson);
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(JSON, jsonFromForm);
Request request = new Request.Builder()
.url(url)
.post(body)
.addHeader("content-type", "application/json; charset=utf-8")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println("Failure!!");
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
if(response.isSuccessful()) {
if (context != null) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if(response.body() != null){
}
}
});
}
//
} else {
if (context != null) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, context.getResources().getString(R.string.failed_to_login),
Toast.LENGTH_SHORT).show();
}
});
}
}
}
});
}
After implementing @Piyush Patel answer:
I change the Call
and the Response
to retrofit2.Call<tokenRetrieved>
and retrofit2.Reponse<tokenRetrieved>
respectively. but the Callback
in my enqueue
prompt an error asking me to implement its onResponse
and onFailure
methods
newbie question: Am I using retrofit1
methods?!!