Retrofit post request structure
Here is some steps for call post request in retrofit.
Make http request with retrofit.
public IWebInterface serviceCallApi() { HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient.Builder builder = new OkHttpClient.Builder() .connectTimeout(Constants.Web.CONNECTION_TIMEOUT, TimeUnit.SECONDS) .writeTimeout(Constants.Web.CONNECTION_TIMEOUT, TimeUnit.SECONDS) .readTimeout(Constants.Web.CONNECTION_TIMEOUT, TimeUnit.SECONDS); builder.addInterceptor(logging); OkHttpClient client = builder.build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(add_your_base_url) .client(client) .addConverterFactory(JacksonConverterFactory.create()) .build(); return retrofit.create(IWebInterface .class);}
Create interface which will append request parameter
public interface IWebInterface {@POST("home/info/")@FormUrlEncodedCall<ResponseData> getJson(@Field("city_id") String title);}
Now you already created model. fill this model as a body to your retrofit call
public boolean callApi(){ boolean isSuccess = false; Call<ResponseData> call = serviceCallApi.getJson(pass_your_city); try { Response<ResponseData> response= call.execute(); if (response.code() == 200) { ResponseData responseParser = response.body(); if (responseParser != null) { isSuccess = true; } } } catch (IOException e) { Log.e("Exception", e.getMessage()); }} return isSuccess;
}
your model :
public class Post { @SerializedName("city_id") @Expose private String title; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; }}
here is the steps i did in my project for call POST retrofit.Hope it will help you!!