欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-22 08:43:36
Android studio获取服务器端json数据——天气预报MainActivity.javaimport 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 and...

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

Android studio获取服务器端json数据——天气预报(仅读取了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>

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

本文地址:https://blog.csdn.net/qq_42583263/article/details/107141126