Android sends post requests using OkHttp

  • 2021-10-24 23:45:08
  • OfStack

In this article, we share the specific code of sending post request using OkHttp for your reference. The specific contents are as follows

MainActivity.java


public class MainActivity extends AppCompatActivity {

 private EditText mEt_qq;
 private EditText mEt_pwd;
 private TextView mTv_status;

 String path = "http://169.254.53.96:8080/web/LoginServlet";

 private static final int SUCCESS = 665;
 private static final int FALL = 894;

 Handler handler=new Handler(){
  @Override
  public void handleMessage(Message msg) {
   switch (msg.what) {
    case SUCCESS:
     String text= (String) msg.obj;
     mTv_status.setText(text);
     break;
    case FALL:
     Toast.makeText(MainActivity.this, " No net ", Toast.LENGTH_SHORT).show();
     break;
    default:
     break;
   }
  }
 };

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  // Initialize the control 
  initVIew();
 }

 private void initVIew() {
  mEt_qq = (EditText) findViewById(R.id.et_qq);
  mEt_pwd = (EditText) findViewById(R.id.et_pwd);
  mTv_status = (TextView) findViewById(R.id.tv_status);
 }

 /**
  *  Use Post Make a form ( Key-value pair ) Upload , Complete login 
  * @param view
  */
 public void login(View view){

  // Get the information entered by the user , Make non-empty judgment 
  String qq = mEt_qq.getText().toString().trim();
  String pwd =mEt_pwd.getText().toString().trim();
  if(TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd) ){
   Toast.makeText(MainActivity.this, " Cannot be entered as null ", Toast.LENGTH_SHORT).show();
   return;
  }

  //1.0  Create okhttpClinet
  OkHttpClient okHttpClient = new OkHttpClient.Builder()
    .connectTimeout(10, TimeUnit.SECONDS)
    .readTimeout(10,TimeUnit.SECONDS)
    .writeTimeout(10,TimeUnit.SECONDS)
    .build();

  FormBody formBody= new FormBody.Builder()
    .add("qq", qq).add("pwd", pwd)
    .build();

  Request request= new Request.Builder()
    .post(formBody)
    .url(path)
    .build();

  Call call = okHttpClient.newCall(request);

  call.enqueue(new Callback() {
   @Override
   public void onFailure(Call call, IOException e) {
    handler.sendEmptyMessage(FALL);
   }

   @Override
   public void onResponse(Call call, Response response) throws IOException {
    String string = response.body().string();
    Message msg = Message.obtain();
    msg.obj=string;
    msg.what=SUCCESS;
    handler.sendMessage(msg);
   }
  });
 }
}

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

 <EditText
  android:id="@+id/et_qq"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:hint=" Please enter qq Number " />

 <EditText
  android:id="@+id/et_pwd"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:hint=" Please enter your password "
  android:inputType="textPassword" />

 <Button
  android:onClick="login"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text=" Landing " />


  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/tv_status"
   android:text=" Login status :"
   />

</LinearLayout>

build. gradle//Dependence


implementation 'com.squareup.okhttp3:okhttp:3.4.2'

Related articles: