Getting simple JSON object response using Retrofit library

后端 未结 8 2061
感动是毒
感动是毒 2020-12-17 17:59

I have a web query with JSON response as:

{
    \"status\":true,
    \"result\":
      {
        \"id\":\"1\",
        \"name\":\"ABC 1\",
        \"email\":         


        
8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-17 18:09

    Instead of Callback with JSONObject class, you could use the Retrofit basic callback which use the Response class and then, once you get the response, you had to create the JSONObject from it.

    See this: https://stackoverflow.com/a/30870326/2037304

    Otherwise you can create your own model class to handle the response.

    First the Result class:

    public class Result {
        public int id;
        public String name;
        public String email;
        public String password;
        public boolean status;
        public Date created;
    }
    

    And then your response class to use with Retrofit

    public class MyResponse {
        public boolean status;
        public Result result;
        public String message;
    }
    

    Now you can call:

     @GET("/stockers/login") 
     public void login( 
        @Query("email") String email,
        @Query("password") String password,
        Callback callback);
    

提交回复
热议问题