服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android使用OkHttp发送post请求

Android使用OkHttp发送post请求

2022-08-27 16:16FanRQ_ Android

这篇文章主要为大家详细介绍了Android使用OkHttp发送post请求,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

MainActivity.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<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 //依赖

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

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

原文链接:https://blog.csdn.net/FanRQ_/article/details/84034915

延伸 · 阅读

精彩推荐