Android studio获取服务器端json数据——天气预报(仅读取了json文件尚未分析)

Android studio获取服务器端json数据——天气预报

在这里插入图片描述

MainActivity.java



import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    EditText city_name;
    TextView set_tq;
    Button get_tq;

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




        city_name=findViewById(R.id.city_name);
        get_tq=findViewById(R.id.get_tq);
        set_tq=findViewById(R.id.set_tq);
        get_tq.setOnClickListener(this);



    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.get_tq:
                city_name.setText("");
                try {
                    //方法一

                    new Thread(new Runnable() {
                        String url=" http://wthrcdn.etouch.cn/weather_mini?city=灵川";
                        @Override
                        public void run() {
                            try {
                                String html = HtmlService.getHtml(url);
                                Log.i("天气信息",html);
                                set_tq.setText(html);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();




                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }


}

HtmlService.java

package com.weather;

import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HtmlService {
    public static String getHtml(String path) throws Exception {

        Log.i("path",path);

        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5 * 1000);
        InputStream inStream = conn.getInputStream();//通过输入流获取html数据
        byte[] data = readInputStream(inStream);//得到html的二进制数据
        String html = new String(data, "UTF-8");
        return html;
    }

    public static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        inStream.close();
        return outStream.toByteArray();
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@drawable/one"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="天气预报"
        android:textSize="20dp"
        android:layout_gravity="center"
        android:layout_margin="20dp"
        ></TextView>

    <EditText
        android:id="@+id/city_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入城市名称"
        android:layout_margin="20dp"
        ></EditText>

    <Button
        android:id="@+id/get_tq"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询"
        android:textColor="@color/color_white"
        android:textSize="25dp"
        android:background="#6BB618"
        android:layout_margin="20dp"
        android:layout_gravity="right"
        ></Button>


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

        <TextView
            android:id="@+id/set_tq"
            android:layout_width="match_parent"
            android:layout_height="600dp"
            android:background="@drawable/gray"
            android:layout_margin="50dp"
            android:layout_alignParentBottom="true"
            android:textSize="20dp"
            ></TextView>


    </RelativeLayout>

</LinearLayout>

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大胖东

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值