How to Handle Empty Response Body with Retrofit 2

How can I handle empty response body with Retrofit 2?

Edit:

As Jake Wharton points out,

@GET("/path/to/get")
Call<Void> getMyData(/* your args here */);

is the best way to go versus my original response --

You can just return a ResponseBody, which will bypass parsing the response.

@GET("/path/to/get")
Call<ResponseBody> getMyData(/* your args here */);

Then in your call,

Call<ResponseBody> dataCall = myApi.getMyData();
dataCall.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Response<ResponseBody> response) {
// use response.code, response.headers, etc.
}

@Override
public void onFailure(Throwable t) {
// handle failure
}
});

Empty response body when parsing API with Retrofit and Gson

@FormUrlEncoded
@POST("user/login")
suspend fun login(
@Field("email") email:String,
@Field("password") password:String,
): Response<ContentResponse>

data class ContentResponse(
@SerializedName("content")
val loginResponse: LoginResponse
)

Maybe this can help you. you are getting data in content node so

Retrofit is returning an empty body

the problem was from me I forget to pull the back end code in the serve and the response I post in the question I discover it was from the local host not from the server it took from me three days to discover that

Response body empty using Retrofit2

Create a New Model like this

public class ResponseApi{
boolean success;
User payload;

public boolean isSuccess() {
return success;
}

public void setSuccess(boolean success) {
this.success = success;
}

public User getPayload() {
return payload;
}

public void setPayload(User payload) {
this.payload = payload;
}

}

And then change your code like this

public interface NetworkAPI {

@POST("users/login")
Call< ResponseApi > login(@Body UserLogin userLogin);
}

Hope it will work.. Actually Retrofit unable to parse your json object due to different Structure.

Retrofit. POST with request body without response body

Using POST you'll probably not use @Query, but rather a @Body. E.g.

data class LoginRequest(
val userLogin: String,
val userPassword: String
)

and in your interface :

 @POST("login")
fun userLogin(@Body request: LoginRequest):Call<Void>

EDIT:
Since it's an issue with ClearText:
You need to add android:usesCleartextTraffic="true" to your AndroidManifest.
Refer to
Android 8: Cleartext HTTP traffic not permitted
Option 2 for more info.

Receiving an empty body in retrofit Response

you should try like this way ....

public interface SlotsAPI {

/*Retrofit get annotation with our URL
And our method that will return a Json Object
*/
@GET(url)
Call<JsonElement> getSlots();
}

in request method

 retrofit.Call<JsonElement> callback = api.getSlots();
callback.enqueue(new Callback<JsonElement>() {
@Override
public void onResponse(Response<JsonElement> response) {
if (response != null) {
Log.d("OnResponse", response.body().toString());
}
}


Related Topics



Leave a reply



Submit