OKHttp基本用法

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

implementation'com.squareup.okhttp3:okhttp:3.9.1'
<uses-permission android:name="android.permission.INTERNET" />

下面是具体代码

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);
                    }
                });
            }
        });
    }
}

 

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="doGet"
        android:text="GET" />

 <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="doPost"
        android:text="POST" />

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"/>
</LinearLayout>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值