I am just trying to show user data after hitting the API using Retrofit. my api response is:
{
"password":"111222333",
"name":"test name",
"email":"testem@gmail.com",
"username":"test1",
"customer_id":"201060",
"phone":"0196789"
}
but unfortunately, I am getting
"Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $" error.
I am totally stuck to show my json response.
My User.java class:
public class User {
@SerializedName("name")
@Expose
private String name;
@SerializedName("email")
@Expose
private String email;
@SerializedName("username")
@Expose
private String username;
@SerializedName("customer_id")
@Expose
private String customerId;
@SerializedName("phone")
@Expose
private String phone;
@SerializedName("password")
@Expose
private String password;
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getUsername() {
return username;
}
public String getCustomerId() {
return customerId;
}
public String getPhone() {
return phone;
}
public String getPassword() {
return password;
}
}
My Login class:
Gson gson = new GsonBuilder().setLenient().create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://us-central1-gmx-notification.cloudfunctions.net/")
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
all_api = retrofit.create(allApi.class);
private void getUserDetails(String userName,String passWord){
Call<User> call = all_api.getUserDetails(userName,passWord);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if(!response.isSuccessful()){
Log.d(response.body());
}
else{
User user = response.body();
String content = "";
content+= "Name: "+user.getName()+"\n";
content+= "Email: "+user.getEmail()+"\n";
content+= "Customer ID: "+user.getCustomerId()+"\n";
content+= "Phone: "+user.getPhone()+"\n";
Log.d(content);
}
});
}
and my retrofit api class:
package com.material.components;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface allApi {
@GET("login")
Call <User> getUserDetails(
@Query("email") String email,
@Query("password") String password
);
}