隐藏

Android Studio的OkHttp3使用

发布:2023/2/24 15:28:59作者:管理员 来源:本站 浏览次数:360

OkHttp是由Square公司开发的,OkHttp的开发项目主页地址:http://github.com/square/okhttp。


创建一个项目:


由于今天是4月5号所以就要Test0405命名。

在使用OkHttp是先在项目中添加OkHttp库,编辑app/build.gradle文件。

在dependencies中添加


implementation 'com.squareup.okhttp3:okhttp:3.14.2'  //okhtttp库

implementation 'com.squareup.okio:okio:1.17.4'       //okhttp需要依赖的基础




在activity_main.xml中添加一个button按钮,和一个textview控件。


<Button

   android:id="@+id/send_request"

   android:layout_width="match_parent"

   android:layout_height="wrap_content"

android:text="Send request get" />

<TextView

   android:id="@+id/request_text"

   android:layout_width="match_parent"

   android:layout_height="match_parent"/>


  



在MainActivity.java中来使用OkHttp


package com.example.test0405;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import okhttp3.OkHttpClient;

import okhttp3.Request;

import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

   TextView responseText;

   Button send_request;

   @Override

   protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

       //对控件的实例化

       responseText=findViewById(R.id.request_text);

       send_request=findViewById(R.id.send_request);

       send_request.setOnClickListener(this);

   }

   @Override

   public void onClick(View view) {

       if (view.getId()==R.id.send_request){

           sendRequestWithOkHttp(); //点击button按钮使用sendRequestWithOkHttp()方法

       }

   }

   private void sendRequestWithOkHttp() {

       new Thread(new Runnable() {

           @Override

           public void run() {

               try {

                   OkHttpClient client = new OkHttpClient();//创建一个OkHttpClient实例

                   //想要发起一条Http请求,需要创建一个Request对象

                   Request request=new Request.Builder()//创建一个Request对象

                           .url("http:www.baidu.com")//需要请求的网址                           .post(requestBody)

                           .get()//请求方式

                           .build();

                   //调用OkHttpClient的newCall()方法来创建一个Call对象,

                   //并调用它的execute()方法,来发送请求并获取服务器返回的数据

                   Response response=client.newCall(request).execute();

                   //其中Response对象就是服务器返回的数据,我们可以使用如下写法得到返回的具体内容

                   String responseData=response.body().string();

                   //将服务器返回的数据进行读取,并将结果传入到showResponse()方法中

                   showResponse(responseData);

               }catch (Exception e){

                   e.printStackTrace();

               }

           }

           private void showResponse(final String responseData) {

               runOnUiThread(new Runnable() {

                   @Override

                   public void run() {

                       //将结果显示到界面上

                       responseText.setText(responseData);

                   }

               });

           }

       }).start();

   }

}


 


AndroidManifest.xml中添加允许用户联网,并指示应用程序使用明文网络流量


android:usesCleartextTraffic="true" //指示应用程序是否打算使用明文网络流量,

<uses-permission android:name="android.permission.INTERNET"/> //允许用户联网


 



运行程序,并点击Send Request按钮,结果如下图所示。


注意:上述是get请求,如果是post请求,我们需要先构造一个RequestBody对象来存放待提交的参数


然后在Request.Builder中调用一下post()方法,并将RequestBody对象传入:



接下来的操作就和get请求一样了。