简体   繁体   中英

Retrofit POST raw string body

I am using Retrofit to send a POST request to a server. The body of the POST must be in the form jdata={"key1":"value1",...} along with a Content-Type header set to application/x-www-form-urlencoded . I found a similar question but the accepted answer is not working.

Here's what I tried -

My interface

public interface APIHandler {
    @Headers("Content-Type: application/x-www-form-urlencoded")
    @FormUrlEncoded
    @POST(URL)
    Call<ResponseBody> getdata(@Field("jdata") String jdata);
}

Call function

public void load() {
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("BASE_URL")
        .addConverterFactory(GsonConverterFactory.create())
        .build();

// prepare call in Retrofit 2.0
APIHandler iAPI = retrofit.create(APIHandler.class);

String requestBody = "{\"id\":\"value\",\"id1\":\"value2\"}"
Call<ResponseBody> call = iAPI.getData(requestBody);

call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> c, Response<ResponseBody> response) {
        if (response.isSuccess()) {
            ResponseBody result = response.body();
            String gs = new Gson().toJson(result);
            Log.d("MainActivity", "response = " + gs + " status: " + statusCode);

        } else {
            Log.w("myApp", "Failed");
        }
    }

    @Override
    public void onFailure(Call<ResponseBody> c, Throwable t) {
    }
});
}

But I receive response = null and status = 200 . What am I doing wrong? The expected response is only a string and not a JSON array.

I am leaving this here so that it helps someone.

The above code is correct. As I mentioned in the last line, a plain string response was expected. But since it is not a JSON response, the conversion probably did not work and the response was null. The only solution I could find was to directly convert the response to string -

try {
        stresp = response.body().string()
        Log.d("MainActivity", "response = " + stresp + " status: " + statusCode);
    } catch (IOException e) {
                    //Handle exception
}

There might be a better way to handle this but that worked for me!

You can use like that. I have tested this and it working fine

public interface APIHandler {
    @POST(URL)
    Call<ResponseBody> getdata(@Body JsonObject body);
}

Request body:

JsonObject requestBody = new JsonObject();
requestBody.addProperty("id", "value1");
requestBody.addProperty("id1", "value2");

Prepare call in Retrofit 2.0

APIHandler iAPI = retrofit.create(APIHandler.class);

And Call function :

 Call<ResponseBody> call = iAPI.getData(requestBody);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> c, Response<ResponseBody> response) {
            if (response.isSuccess()) {
                String result = response.body().string();

                Log.d("MainActivity", "response = " + result);

            } else {
                Log.w("myApp", "Failed");
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> c, Throwable t) {
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM