其他分享
首页 > 其他分享> > 移动应用开发实践-Task1-OkHttp的基础使用

移动应用开发实践-Task1-OkHttp的基础使用

作者:互联网

移动应用开发实践-Task1-OkHttp的基础使用

目标:获取下图的json对象

在这里插入图片描述

1.配置OkHttp(这里用的3.8.1版本)在这里插入图片描述

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

2.做一个简单的获取页面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请在下方输入URL" />

    <EditText
        android:id="@+id/et_url"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text="http://guolin.tech/api/china" />

    <Button
        android:id="@+id/bt_ack"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Get Data" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tv_ack"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

在这里插入图片描述

3.主要函数(OkHttp的应用)

package com.example.fyn_weather_task1_0;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    EditText editText;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_main);

        editText = findViewById(R.id.et_url);
        textView = findViewById(R.id.tv_ack);
        Button button = findViewById(R.id.bt_ack);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建一个client
                OkHttpClient client = new OkHttpClient();
                //创建一个请求build,请求对象是editText中的网址
                Request build = new Request.Builder().url(editText.getText().toString().trim()).get().build();
                //将请求build设置为client的call对象
                Call call = client.newCall(build);
                //进入队列发送请求,根据不同的response响应
                call.enqueue(new Callback() {
                    @Override
                    //响应失败
                    public void onFailure(Call call, IOException e) {
                        showTextView(e.toString());
                    }

                    @Override
                    //响应成功
                    public void onResponse(Call call, Response response) throws IOException {
                        String s = response.body().string();
                        showTextView(s);
                    }
                });
            }
        });
    }

    //将结果显示在textView
    private void showTextView(final String toString) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textView.setText(toString);
            }
        });
    }
}


4.运行结果

在这里插入图片描述

标签:Task1,void,实践,okhttp3,call,new,OkHttp,import,public
来源: https://blog.csdn.net/Biu_Destiny/article/details/114659231