Sachin Malik Sachin Malik - 11 days ago 4
Android Question

How to send a retrofit 2 post call with no parameters

I have to call an api using retrofit 2 in android. but with no values. When I does that, it shows that there must be at least 1 @field. Below is the code I am using.

public interface gitAPI {
@FormUrlEncoded
@POST("/MembersWS.svc/GetMemberAvailability/MBR0011581")
Call<Questions[]> loadQuestions();
}






Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.1.99:82")
.addConverterFactory(GsonConverterFactory.create())
.build();
// prepare call in Retrofit 2.0
gitAPI stackOverflowAPI = retrofit.create(gitAPI.class);

Call<Questions[]> call = stackOverflowAPI.loadQuestions();
call.execute();

Answer

Declare body value in your interface with next:

@Body RequestBody body and wrap String JSON object:

RequestBody body = RequestBody.create(MediaType.parse("application/json"), (new JsonObject()).toString());
Comments