Retrofit: Sending POST request to server in android

14,687

After waiting for the best response I thought of answering my own question. This is how I resolved my problem.

I changed the converter of RestAdapter of RetrofitService and created my own Converter. Below is my StringConverter

static class StringConverter implements Converter {

    @Override
    public Object fromBody(TypedInput typedInput, Type type) throws ConversionException {
        String text = null;
        try {
            text = fromStream(typedInput.in());
        } catch (IOException ignored) {/*NOP*/ }

        return text;
    }

    @Override
    public TypedOutput toBody(Object o) {
        return null;
    }

    public static String fromStream(InputStream in) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder out = new StringBuilder();
        String newLine = System.getProperty("line.separator");
        String line;
        while ((line = reader.readLine()) != null) {
            out.append(line);
            out.append(newLine);
        }
        return out.toString();
    }
}

Then I set this converter to the RestAdapter in the Application class.

RestAdapter restAdapter = new RestAdapter.Builder()
        .setEndpoint(BASE_URL)
        .setConverter(new StringConverter())
        .build();
    mRetrofitService = restAdapter.create(RetrofitService.class);

Now whenever I use Retrofit, I get the response in String. Then I converted that String JSONObject.

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

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

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

Hence, I got the result in JSON form. Then I parsed this JSON as required.

Share:
14,687
Nitesh Kumar
Author by

Nitesh Kumar

Updated on June 11, 2022

Comments

  • Nitesh Kumar
    Nitesh Kumar almost 2 years

    I am using 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}

    Edit: 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 CallBack<JSONObject> ...?

  • Muhammad Irfan
    Muhammad Irfan about 9 years
    mRetrofitService.getUser(user, new Callback<JSONObject>() and public void success(String result, Response arg1) . Either JSONObject should be string OR String result should be JSONObject result. I tried your code and found this
  • Vicky Chijwani
    Vicky Chijwani over 8 years
    I've corrected the code to use Callback<String> instead of Callback<JSONObject>.