移动应用开发实践-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.运行结果

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值