欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android使用OkHttp发送post请求

程序员文章站 2023-11-18 17:30:19
本文实例为大家分享了使用okhttp发送post请求的具体代码,供大家参考,具体内容如下 mainactivity.java public class main...

本文实例为大家分享了使用okhttp发送post请求的具体代码,供大家参考,具体内容如下

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, "没有网", toast.length_short).show();
     break;
    default:
     break;
   }
  }
 };

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  //对控件进行初始化操作
  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);
 }

 /**
  * 使用post进行表单(键值对)上传,完成登录
  * @param view
  */
 public void login(view view){

  //得到用户输入的信息,进行非空判断
  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, "不能输入为空", toast.length_short).show();
   return;
  }

  //1.0 创建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="请输入qq号码" />

 <edittext
  android:id="@+id/et_pwd"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:hint="请输入密码"
  android:inputtype="textpassword" />

 <button
  android:onclick="login"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="登陆" />


  <textview
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/tv_status"
   android:text="登陆状态:"
   />

</linearlayout>

build.gradle //依赖

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。