博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OKHttp基本用法
阅读量:2145 次
发布时间:2019-04-30

本文共 2926 字,大约阅读时间需要 9 分钟。

首先记得在build.gradle 和 配置文件分别加上依赖 和 网络权限

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

下面是具体代码

package com.example.okhttpdemo;import android.os.Bundle;import android.view.View;import android.widget.TextView;import androidx.appcompat.app.AppCompatActivity;import java.io.IOException;import okhttp3.Call;import okhttp3.Callback;import okhttp3.FormBody;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;public class MainActivity extends AppCompatActivity {    private TextView tvResult;    //测试接口    private String strGet = "http://www.baidu.com/";    private String strPost = "http://api.apiopen.top/likePoetry";    //1.拿到okHttpClient对象    OkHttpClient okHttpClient = new OkHttpClient();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tvResult = findViewById(R.id.tv_result);    }    public void doGet(View view) {        //1.拿到okHttpClient对象        //2.构造Request        Request.Builder builder = new Request.Builder();        Request request = builder.get().url(strGet).build();        //3.将Request封装位call        Call call = okHttpClient.newCall(request);        //4.执行call        //Response response = call.execute();//同步        call.enqueue(new Callback() {//异步            @Override            public void onFailure(Call call, IOException e) {                L.e("onFailure:" + e.getMessage());            }            @Override            public void onResponse(Call call, Response response) throws IOException {                final String res = response.body().string();                L.e("onResponse:" + res);                runOnUiThread(new Runnable() {//异步在子线程,不允许直接显示                    @Override                    public void run() {                        tvResult.setText(res);                    }                });            }        });    }    public void doPost(View view) {        FormBody formBody = new FormBody.Builder().add("name", "李白").build();        //1.拿到okHttpClient对象        //2.构造Request        Request.Builder builder = new Request.Builder();        Request request = builder.url(strPost).post(formBody).build();        //3.将Request封装位call        Call call = okHttpClient.newCall(request);        //4.执行call        call.enqueue(new Callback() {            @Override            public void onFailure(Call call, IOException e) {                L.e("onFailure:" + e.getMessage());            }            @Override            public void onResponse(Call call, Response response) throws IOException {                final String res = response.body().string();                L.e("onResponse:" + res);                runOnUiThread(new Runnable() {//异步在子线程,不允许直接显示                    @Override                    public void run() {                        tvResult.setText(res);                    }                });            }        });    }}

 

 

转载地址:http://egegf.baihongyu.com/

你可能感兴趣的文章
【虫师】【selenium】参数化
查看>>
【Python练习】文件引用用户名密码登录系统
查看>>
学习网站汇总
查看>>
【Python】用Python打开csv和xml文件
查看>>
【Loadrunner】性能测试报告实战
查看>>
【自动化测试】自动化测试需要了解的的一些事情。
查看>>
【selenium】selenium ide的安装过程
查看>>
【手机自动化测试】monkey测试
查看>>
【英语】软件开发常用英语词汇
查看>>
Fiddler 抓包工具总结
查看>>
【雅思】雅思需要购买和准备的学习资料
查看>>
【雅思】雅思写作作业(1)
查看>>
【雅思】【大作文】【审题作业】关于同不同意的审题作业(重点)
查看>>
【Loadrunner】通过loadrunner录制时候有事件但是白页无法出来登录页怎么办?
查看>>
【English】【托业】【四六级】写译高频词汇
查看>>
【托业】【新东方全真模拟】01~02-----P5~6
查看>>
【托业】【新东方全真模拟】03~04-----P5~6
查看>>
【托业】【新东方托业全真模拟】TEST05~06-----P5~6
查看>>
【托业】【新东方托业全真模拟】TEST09~10-----P5~6
查看>>
【托业】【新东方托业全真模拟】TEST07~08-----P5~6
查看>>