Android studio天气

导入第三方jar包
在这里插入图片描述

    implementation 'com.alibaba:fastjson:1.2.49'
    implementation 'com.alibaba:fastjson:1.1.70.android'

创建Http类
在这里插入图片描述

public class HttpConnection {


    public static String getHttpRequest(String urlString){
        URL url;
        InputStream in = null;
        HttpURLConnection conn = null;

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            url = new URL(urlString);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            conn.setDoInput(true);
            conn.connect();

            if(conn.getResponseCode() == HttpsURLConnection.HTTP_OK){
                in = conn.getInputStream();
                int len =0;
                byte[] buffer = new byte[1024];
                while((len= in.read(buffer))!=-1){
                    out.write(buffer, 0, len);
                }
                return out.toString();
            }
        } catch (MalformedURLException e) {

            e.printStackTrace();
        } catch (ProtocolException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }finally{
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(conn!=null){
                conn.disconnect();
            }

            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return "kong";
    }
}

天气部分的xml
在这里插入图片描述

<RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginRight="2dp"
            android:layout_weight="3">

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true">

                <ImageView
                    android:id="@+id/pic_7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:background="@drawable/rain1"></ImageView>

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/pic_7"
                    android:layout_centerHorizontal="true"
                    android:text="123"
                    android:textSize="20sp" />

                <TextView
                    android:id="@+id/wether_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/textView"
                    android:layout_centerHorizontal="true"
                    android:text="20C"
                    android:textColor="#ffffff"
                    android:textSize="25sp"></TextView>

                <TextView
                    android:id="@+id/air_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/wether_text"
                    android:layout_centerHorizontal="true"
                    android:text="空气质量(47)"
                    android:textColor="#ffffff"
                    android:textSize="20sp"></TextView>

            </RelativeLayout>

界面上关于天气的代码

    private TextView tianqi;
    private TextView weather_text;
    private TextView air_text;
    private ImageView pic_7;

在这里插入图片描述

 air_text=findViewById(R.id.air_text);
        tianqi=findViewById(R.id.textView);
        pic_7=findViewById(R.id.pic_7);
           weather_text = (TextView) findViewById(R.id.wether_text);
        two();
         showDateInfo();

在这里插入图片描述

 private void weather() {
        new Thread(  ){
            String a;
            String b;
            @Override
            public void run(){
                a= HttpConnection.getHttpRequest("https://devapi.qweather.com/v7/weather/now?location=101044000&key=你申请的key");
                b= HttpConnection.getHttpRequest("https://devapi.qweather.com/v7/air/now?location=101044000&key=你申请的key");
             runOnUiThread(new Runnable( ) {
                 @Override
                 public void run() {
                     JSONObject jsonObject = JSON.parseObject(a);
                     JSONObject jsonObject1 = JSON.parseObject(b);
                     tianqi.setText((String) jsonObject.getJSONObject("now").get("text"));
                     air_text.setText(" 空气质量"+(String) jsonObject1.getJSONObject("now").get("aqi"));
                     weather_text.setText((String) jsonObject.getJSONObject("now").get("temp")+" ℃");

                 }
             });
            }
        }.start();
    }

key可以去和风天气申请

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值