Here is my API end point:
http://192.168.1.22:8000/api/auth/login
I need to POST
email
and password
to receive Token
from server
Server Post response:
{
"status": "ok",
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjIsImlzcyI6Imh0dHA6XC9cL21vbmV5bWF0Y2guYXBwOjgwMDBcL2FwaVwvYXV0aFwvbG9naW4iLCJpYXQiOjE0OTE0OTYzNDEsImV4cCI6MTQ5MTQ5OTk0MSwibmJmIjoxNDkxNDk2MzQxLCJqdGkiOiJhZmJhMDEzMzI5OWIwZmY0MzY3MTdlZDE4NmVkODI4OSJ9.bGlInsHHjdveeabz0j_36yICZtK32q1B9oNW44dk_x4"
}
here is my service Retrofit 2
:
public interface ApiInterface {
@POST("auth/login")
Call<Login> authenticate(@Body User user);
}
here is my Login Model
public class Login {
@SerializedName("status")
@Expose
private String status;
@SerializedName("token")
@Expose
private String token;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
here is my User Model
:
public class User {
@SerializedName("email")
@Expose
private String email;
@SerializedName("password")
@Expose
private String password;
public User(String email, String password) {
this.email = email;
this.password = password;
}
}
and this is how i call it in Activity
public void sendNetworkRequest(User user){
Log.d(TAG,"send request fired");
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http://192.168.1.22:8000/api/")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
ApiInterface apiInterface = retrofit.create(ApiInterface.class);
Call<Login> call = apiInterface.authenticate(user);
call.enqueue(new Callback<Login>() {
@Override
public void onResponse(Call<Login> call, Response<Login> response) {
Toast.makeText(LoginActivity.this, "wuhuuu"+ response.body().getToken(), Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<Login> call, Throwable t) {
Log.d(TAG,"went wrong");
Toast.makeText(LoginActivity.this, "something went wront"+t.toString(), Toast.LENGTH_LONG).show();
}
});
}
UPDATED: this is the Error I get:
com.google.gson.stream.malformedjsonexception use jsonreader.setlenient(true) to accept malformed at line 1column 1 $path
UPDATE Note: the email
and password
need to be sent in body form data
.
Note: I can access the API from browser in Emulator so i guess it means there is something wrong in my logic or approach.
Complete Stack trace:
D/LoginActivity: went wrong
com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1567)
at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1416)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:597)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:429)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:201)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:116)
at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:106)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)