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
Attempt to Reopen an Already-Closed Object SQLitedatabase
How to Store Hashmap So That It Can Be Retained It Value After a Device Reboot
Change Chip Widget Style Programmatically Not Working - Android
A Correct Way to Convert Byte[] in Java to Unsigned Char* in C++, and Vice Versa
CSS Overflow and Absolute Positioning Issue on Android Browser
Value <Br of Type Java.Lang.String Cannot Be Converted to JSONobject on Android
Why Is 0Dp Considered a Performance Enhancement
How to Animate a View in Android and Have It Stay in The New Position/Size
Enable C++11 Support on Android
How to Hide App Title in Android
Android Stream Video from Google Drive
How to Know the Calling Activity in Android
Eclipse Indexer Errors When Using Stl with Android Ndk
What Is Metadata ? and What Is The Use of It in Android