Retrofit: Retrofit: Sending POST request to server and getting result in JSONObject in android

Created on 26 May 2014  路  2Comments  路  Source: square/retrofit

I am Retrofit to call APIs. I am sending a post request to API but in the callback I am getting empty JSON like this {}.

Below is the code for RetrofitService

@POST("/com/searchusers.php")
void getUser(@Body JSONObject searchstring, Callback<JSONObject> callBack);

where searchstring JSON is like this {"search":"nitesh"}. In response I am supposed to get the detail of user "nitesh".

Below is the code for sending POST request

RetrofitService mRetrofitService = app.getRetrofitService();
mRetrofitService.getUser(user, new Callback<JSONObject>() {

    @Override
    public void success(JSONObject result, Response arg1) {
        System.out.println("success, result: " + result);
    }

    @Override
    public void failure(RetrofitError error) {
        System.out.println("failure, error: " + error);
    }
});

I am getting this output success, result: {}

Expected output is success, result: {"name":"nitesh",....rest of the details}

Note: I tried using Response instead of JSONObject like this

CallBack<Response>

and then I converted the raw response into String and I got the expected result. But the problem is its in String, I want the response in JSONObject.

How can I get the exact result using this

CallBack<JSONObject>

Most helpful comment

Do not use JSONObject. Ever. It's a terrible, awful API.

Retrofit uses Gson for conversion of message bodies to and from JSON. Here's a snippet from the website:

Retrofit uses Gson by default to convert HTTP bodies to and from JSON. If you want to specify behavior that is different from Gson's defaults (e.g. naming policies, date formats, custom types), provide a new Gson instance with your desired behavior when building a RestAdapter. Refer to the Gson documentation for more details on customization.

Please read the Gson documentation on how to create Java objects which represent the JSON objects you want to create. You can also look at the GitHub API sample in the project for an example of how the JSON deserialization works.

I would also suggest you ask specific questions on StackOverflow using the 'retrofit' tag. GitHub is for tracking upcoming features and bugs only.

All 2 comments

Do not use JSONObject. Ever. It's a terrible, awful API.

Retrofit uses Gson for conversion of message bodies to and from JSON. Here's a snippet from the website:

Retrofit uses Gson by default to convert HTTP bodies to and from JSON. If you want to specify behavior that is different from Gson's defaults (e.g. naming policies, date formats, custom types), provide a new Gson instance with your desired behavior when building a RestAdapter. Refer to the Gson documentation for more details on customization.

Please read the Gson documentation on how to create Java objects which represent the JSON objects you want to create. You can also look at the GitHub API sample in the project for an example of how the JSON deserialization works.

I would also suggest you ask specific questions on StackOverflow using the 'retrofit' tag. GitHub is for tracking upcoming features and bugs only.

Use GSON as new Gson().toJson(login) where login is the object defined in Callback Like this

api.getLocation(new Callback() {

            @Override
            public void success(Login login, Response response) {

            }

            @Override
            public void failure(RetrofitError retrofitError) {

            }

});

Was this page helpful?
0 / 5 - 0 ratings