-1

Help Me for this issue, please code is as follow

Activity

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TaskData taskData= new TaskData();
        taskData.setEmail("praik@gmail.com");
        Task task= new Task();
        task.setTaskData(taskData);
        Log.v("@@@WWE"," Call Initiated");
        task.setTask("getUserDetail");


        JSONObject taskDatas= new JSONObject();

        JSONObject taskM= new JSONObject();
        try {
            taskDatas.put("email","praik@gmail.com");
            taskM.put("task","getUserDetail");
            taskM.put("taskData",taskDatas);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Map<String,JSONObject> taskMap= new HashMap<>();
        taskMap.put("reqObject",taskM);
        getUserDetails(taskMap);
        Log.v("@@@WWE"," Call Over");
    }

  public void getUserDetails(Map<String,JSONObject> task){
        ServiceInterfaceApi interfaceApi= ServiceClass.getApiService();
        Call<UserList>call=interfaceApi.getUserDetails(task);
        Log.v("@@@WWE","Retrofit Request Method =  "+call.request().method());
        Log.v("@@@WWE","Retrofit Request Body =  "+call.request().body());
        Log.v("@@@WWE","Retrofit Request Url = "+call.request().url());
        Log.v("@@@WWE","Retrofit Request executed = "+call.isExecuted());
        call.enqueue(new Callback<UserList>() {
            @Override
            public void onResponse(Call<UserList> call, Response<UserList> response) {
                Log.v("@@@","Response");
                if (response.isSuccessful()){
                    Log.v("@@@","Sucess");
                    userList=response.body().getUserData();
                    for (int i = 0; i < userList.size(); i++) {
                        UserDatum userDatum=userList.get(i);
                        Log.v("@@@@WWE"," USer Email "+userDatum.getEmail());
                        Log.v("@@@@WWE"," USer Name "+userDatum.getName());
                        Log.v("@@@@WWE"," USer Gender "+userDatum.getGender());
                    }
                }
                if(response.code()==400){
                    Toast.makeText(Home.this,"400 code ",Toast.LENGTH_LONG).show();
                }
            }
            @Override
            public void onFailure(Call<UserList> call, Throwable t) {
                Log.v("@@@WWE","Failure "+t.getMessage());
            }
        });
    }

Service Interface

@Headers("Content-Type: application/json")
@POST("index.php")
    Call<UserList>getUserDetails(@QueryMap Map<String,JSONObject> stringTaskMap);

Service Class

public class ServiceClass {
    public ServiceClass() {
    }

    private static Retrofit getRetroClient(){
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();
        return new Retrofit.Builder()
                .baseUrl(HttpConstants.baseUrl)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
    }

    public static ServiceInterfaceApi getApiService(){
        return getRetroClient().create(ServiceInterfaceApi.class);
    }
}

Pojo Class:

public class UserList {

    @SerializedName("userData")
    @Expose
    private List<UserDatum> userData = null;
    @SerializedName("code")
    @Expose
    private Integer code;
    @SerializedName("message")
    @Expose
    private String message;

    public List<UserDatum> getUserData() {
        return userData;
    }

    public void setUserData(List<UserDatum> userData) {
        this.userData = userData;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

public class UserDatum {

    @SerializedName("email")
    @Expose
    private String email;
    @SerializedName("password")
    @Expose
    private String password;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("image")
    @Expose
    private String image;
    @SerializedName("mobile")
    @Expose
    private String mobile;
    @SerializedName("birth_date")
    @Expose
    private String birthDate;
    @SerializedName("dateofregister")
    @Expose
    private String dateofregister;
    @SerializedName("gender")
    @Expose
    private String gender;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(String birthDate) {
        this.birthDate = birthDate;
    }

    public String getDateofregister() {
        return dateofregister;
    }

    public void setDateofregister(String dateofregister) {
        this.dateofregister = dateofregister;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

}

Expected Response

{"userData":[{"email":"foo@mail.com","password":"12345","name":"foo bar","image":"foo","mobile":"123","birth_date":"12345646","dateofregister":"1493706304000","gender":"male"}],"code":200,"message":"DONE"}

here is my error message Failure java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 6 column 1 path $ Thanks in Advance

Pratik Vyas
  • 609
  • 7
  • 20

2 Answers2

0

Like exception said, you are getting object but it is initialized as String. In POJO only string is message so obviously that's the problem. Message is some kind of object and real string is probably inside of it. So just create message object and then pull your string message from the inside. First initialize that object in parent class:

@SerializedName("message")
@Expose
private Message message;

then add the subclass in POJO

class Message{

   @SerializedName("message")
   @Expose
   private String message;

    public String getMessage() {
      return message;
    }

    public void setMessage(String message) {
      this.message = message;
    }

}

EDIT: HEADER

@Headers("Content-Type: application/json")
@POST("index.php")
Call<UserList>getUserDetails(@QueryMap Map<String,JSONObject> stringTaskMap);
tompadre
  • 789
  • 7
  • 20
  • I am posting the expected response check it if it helps {"userData":[{"email":"foo@mail.com","password":"12345","name":"foo bar","image":"foo","mobile":"123","birth_date":"12345646","dateofregister":"1493706304000","gender":"male"}],"code":200,"message":"DONE"} – Pratik Vyas May 17 '17 at 08:49
  • Have you tested response through postman or some similar program because you obviosuly getting string where it shouldn't be and your POJO looks right? And also checked headers? – tompadre May 17 '17 at 09:18
  • yes @tompadre I checked in chrome's REST API client Postman and also same request object using google volley – Pratik Vyas May 17 '17 at 09:21
  • What about headers? And since it's php, check this answer http://stackoverflow.com/a/43512374/5577679 – tompadre May 17 '17 at 09:24
  • Headers from PHP file header('Content-type: application/json; charset=utf-8'); – Pratik Vyas May 17 '17 at 09:43
  • put the header on the interface like I did in my edit in the comment above altough it's a longshot. – tompadre May 17 '17 at 10:13
0

I think problem is not in your code just check your server log, what json string your server send to you match with your current expected output json

To check server output you can use postman or restclient - just pass your url ,method ,and parameter

secondly ill suggest you to user serializable pojo class like

public class UserList implements Serializable {

}
public class UserDatum implements Serializable {

}

Object send to server must me impliment Serializable or parcelable interface

I have use something like this GsonConverterFactory.create()

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ROOT)
                .client(defaultHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
sushant gosavi
  • 2,781
  • 3
  • 26
  • 47
  • I tried your idea to Implement Serializable it didn't work and yes before asking in StackOverflow I tried a lot. – Pratik Vyas May 17 '17 at 10:58
  • It is the expected response check in the code I mentioned that – Pratik Vyas May 17 '17 at 11:04
  • Who handle the server, ask that person to send you the server response of this api and show us (" you showing us what you want ,I ask you what you get ") – sushant gosavi May 17 '17 at 11:07
  • Did you require PHP code? or JSON response? I myself handle the server dude and yes I copied the response from the postman and mentioned it as expected response there. – Pratik Vyas May 17 '17 at 11:17