Skip to content Skip to sidebar Skip to footer

Getting Simple Json Object Response Using Retrofit Library

I have a web query with JSON response as: { 'status':true, 'result': { 'id':'1', 'name':'ABC 1', 'email':'info@ABc.dcom', 'password':'

Solution 1:

Simply use JsonElement insted of JSONobject. Like:

@GET("/stockers/login")
Call<JsonElement> getLogin(
    @Query("email") String email,
    @Query("password") String password
);

Solution 2:

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:

publicclassResult{
    publicint id;
    publicString name;
    publicString email;
    publicString password;
    publicboolean status;
    public Date created;
}

And then your response class to use with Retrofit

publicclassMyResponse{
    publicboolean status;
    public Result result;
    publicString message;
}

Now you can call:

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

Solution 3:

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 = newJSONObject(response.body().string());

Solution 4:

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

publicclassJSONConverterFactoryextendsConverter.Factory {
    publicstaticJSONConverterFactory create() {
        returnnewJSONConverterFactory();
    }

    privateJSONConverterFactory() {
    }

    @OverridepublicConverter<?, RequestBody> requestBodyConverter(Type type,
                                                                    Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
        if (type == JSONObject.class
                || type == JSONArray.class) {
            returnJSONRequestBodyConverter.INSTANCE;
        }
        returnnull;
    }

    @OverridepublicConverter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
                                                            Retrofit retrofit) {
        if (type == JSONObject.class) {
            returnJSONResponseBodyConverters.JSONObjectResponseBodyConverter.INSTANCE;
        }
        if (type == JSONArray.class) {
            returnJSONResponseBodyConverters.JSONArrayResponseBodyConverter.INSTANCE;
        }
        returnnull;
    }
}

publicclassJSONRequestBodyConverter<T> implementsConverter<T, RequestBody> {
    staticfinal JSONRequestBodyConverter<Object> INSTANCE = newJSONRequestBodyConverter<>();
    privatestaticfinalMediaTypeMEDIA_TYPE= MediaType.parse("text/plain; charset=UTF-8");

    privateJSONRequestBodyConverter() {
    }

    @Overridepublic RequestBody convert(T value)throws IOException {
        return RequestBody.create(MEDIA_TYPE, String.valueOf(value));
    }
}

publicclassJSONResponseBodyConverters {
    privateJSONResponseBodyConverters() {}

    static final classJSONObjectResponseBodyConverterimplementsConverter<ResponseBody, JSONObject> {
        static final JSONObjectResponseBodyConverter INSTANCE = newJSONObjectResponseBodyConverter();

        @OverridepublicJSONObjectconvert(ResponseBody value) throws IOException {
            try {
                returnnewJSONObject(value.string());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            returnnull;
        }
    }

    static final classJSONArrayResponseBodyConverterimplementsConverter<ResponseBody, JSONArray> {
        static final JSONArrayResponseBodyConverter INSTANCE = newJSONArrayResponseBodyConverter();

        @OverridepublicJSONArrayconvert(ResponseBody value) throws IOException {
            try {
                returnnewJSONArray(value.string());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            returnnull;
        }
    }
}

Solution 5:

try this instead :

@GET("/stockers/login")
publicvoidlogin(
        @Query("email") String email,
        @Query("password") String password,
        Callback<Response> callback);  // set the callback generic parameter to ResponseApiManager.getInstance().mUrlManager.login(
        email.getText().toString().trim(),
        password.getText().toString().trim(),
        newCallback<Response>()
        {
            @Overridepublicvoidsuccess(Response response, Response response1)
            {
                String json = response.getBody();
                try {
                    JSONObject jsonObj = newJSONObject(json);
                } catch(JSONException e) {
                }

                alog.dismiss();

Post a Comment for "Getting Simple Json Object Response Using Retrofit Library"