Getting simple JSON object response using Retrofit library

后端 未结 8 2012
感动是毒
感动是毒 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<MyResponse> callback);
    
    0 讨论(0)
  • 2020-12-17 18:22

    I`m using this site to create my classes (POJO) from JSON.

    http://www.jsonschema2pojo.org/ just be sure to set to JSON insted of JSON Schema and check GSON, because retrofit is using GSON as well for parsing.

    your retrofit code looks fine.

    0 讨论(0)
  • 2020-12-17 18:24

    try this instead :

    @GET("/stockers/login")
    public void login(
            @Query("email") String email,
            @Query("password") String password,
            Callback<Response> callback);  // set the callback generic parameter to Response
    
    
    ApiManager.getInstance().mUrlManager.login(
            email.getText().toString().trim(),
            password.getText().toString().trim(),
            new Callback<Response>()
            {
                @Override
                public void success(Response response, Response response1)
                {
                    String json = response.getBody();
                    try {
                        JSONObject jsonObj = new JSONObject(json);
                    } catch(JSONException e) {
                    }
    
                    alog.dismiss();
    
    0 讨论(0)
  • 2020-12-17 18:26

    The answers seam kinda old and for Retrofit 1, if you are using Retrofit 2 and don't want to use a converter you have to use ResponseBody.

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

    And then in your callback in the onResponse method call string on the body and create a JSONObject from it.

    if(response.isSuccessful())
        JSONObject json = new JSONObject(response.body().string());
    
    0 讨论(0)
  • 2020-12-17 18:27

    You can create custom factory like belowe or copy it from here : https://github.com/marcinOz/Retrofit2JSONConverterFactory

    public class JSONConverterFactory extends Converter.Factory {
        public static JSONConverterFactory create() {
            return new JSONConverterFactory();
        }
    
        private JSONConverterFactory() {
        }
    
        @Override public Converter<?, RequestBody> requestBodyConverter(Type type,
                                                                        Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
            if (type == JSONObject.class
                    || type == JSONArray.class) {
                return JSONRequestBodyConverter.INSTANCE;
            }
            return null;
        }
    
        @Override
        public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
                                                                Retrofit retrofit) {
            if (type == JSONObject.class) {
                return JSONResponseBodyConverters.JSONObjectResponseBodyConverter.INSTANCE;
            }
            if (type == JSONArray.class) {
                return JSONResponseBodyConverters.JSONArrayResponseBodyConverter.INSTANCE;
            }
            return null;
        }
    }
    

    public class JSONRequestBodyConverter<T> implements Converter<T, RequestBody> {
        static final JSONRequestBodyConverter<Object> INSTANCE = new JSONRequestBodyConverter<>();
        private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain; charset=UTF-8");
    
        private JSONRequestBodyConverter() {
        }
    
        @Override public RequestBody convert(T value) throws IOException {
            return RequestBody.create(MEDIA_TYPE, String.valueOf(value));
        }
    }
    

    public class JSONResponseBodyConverters {
        private JSONResponseBodyConverters() {}
    
        static final class JSONObjectResponseBodyConverter implements Converter<ResponseBody, JSONObject> {
            static final JSONObjectResponseBodyConverter INSTANCE = new JSONObjectResponseBodyConverter();
    
            @Override public JSONObject convert(ResponseBody value) throws IOException {
                try {
                    return new JSONObject(value.string());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }
    
        static final class JSONArrayResponseBodyConverter implements Converter<ResponseBody, JSONArray> {
            static final JSONArrayResponseBodyConverter INSTANCE = new JSONArrayResponseBodyConverter();
    
            @Override public JSONArray convert(ResponseBody value) throws IOException {
                try {
                    return new JSONArray(value.string());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-17 18:32

    Just define the type of the object you want to get as a String as com.google.gson.JsonObject instead of String and call .toString() on that object to get the JSON string itself.

    0 讨论(0)
提交回复
热议问题