In this android programming source code example, we are going to use POST method in android Retrofit.
You can copy and adopt this source code example to your android project without reinventing the wheel.
Below is a step by step source code to use POST method in android Retrofit.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="15dp"> <android.support.design.widget.TextInputLayout android:id="@+id/titleinput" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/title" android:inputType="textNoSuggestions" android:maxLines="1" android:singleLine="true" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:id="@+id/bodyinput" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/titleinput"> <EditText android:id="@+id/body" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/body" android:inputType="textNoSuggestions" android:maxLines="1" android:singleLine="true" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:id="@+id/useridinput" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/bodyinput"> <EditText android:id="@+id/userid" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/userid" android:inputType="number" android:maxLines="1" android:singleLine="true" /> </android.support.design.widget.TextInputLayout> <Button android:id="@+id/submit" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent" android:layout_below="@id/useridinput" android:textColor="#ffffff" android:text="Submit"/> </RelativeLayout>
MainActivity.java
import android.app.ProgressDialog; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.google.gson.annotations.SerializedName; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; import retrofit2.http.Field; import retrofit2.http.FormUrlEncoded; import retrofit2.http.POST; public class RetrofitActivity2 extends AppCompatActivity { private EditText title; private EditText body; private EditText userid; private Button submit; private ProgressDialog progressDialog; private String baseUrl; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_retrofit2); title = (EditText) findViewById(R.id.title); body = (EditText) findViewById(R.id.body); userid = (EditText) findViewById(R.id.userid); submit = (Button) findViewById(R.id.submit); baseUrl = "http://jsonplaceholder.typicode.com/"; submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); } catch (Exception e) { // TODO: handle exception } submitData(); } }); } private void submitData(){ String enteredtitle = title.getText().toString(); String enteredbody = body.getText().toString(); String entereduserid = userid.getText().toString(); progressDialog = new ProgressDialog(RetrofitActivity2.this); progressDialog.setMessage(getString(R.string.loading)); progressDialog.setCancelable(false); progressDialog.show(); //Defining retrofit api service Retrofit retrofit = new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create()) .build(); ApiService service = retrofit.create(ApiService.class); Call<PostResponse> call = service.postData(enteredtitle, enteredbody, entereduserid); //calling the api call.enqueue(new Callback<PostResponse>() { @Override public void onResponse(Call<PostResponse> call, Response<PostResponse> response) { //hiding progress dialog progressDialog.dismiss(); if(response.isSuccessful()){ Toast.makeText(getApplicationContext(), "Post submitted Title: "+response.body().getTitle()+" Body: "+response.body().getBody()+" PostId: "+response.body().getId(), Toast.LENGTH_LONG).show(); } } @Override public void onFailure(Call<PostResponse> call, Throwable t) { progressDialog.dismiss(); Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show(); } }); } private interface ApiService { @FormUrlEncoded @POST("posts") Call<PostResponse> postData( @Field("title") String title, @Field("body") String body, @Field("userId") String userId); } private class PostResponse{ @SerializedName("title") private String title; @SerializedName("body") private String body; @SerializedName("userId") private String userId; @SerializedName("id") private Integer id; public void setTitle(String title){ this.title = title; } public String getTitle(){ return title; } public void setBody(String body){ this.body = body; } public String getBody(){ return body; } public void setUserId(String userId){ this.userId = userId; } public String getUserId(){ return userId; } public void setId(Integer id){ this.id = id; } public Integer getId(){ return id; } } }
If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.