目录
Android获取网页信息,天气_桃子不出的博客-CSDN博客
接着上一篇文章继续,上篇文章讲到了如何获取数据,这篇来讲如何将json数据解析并展示到屏幕中。
一、效果
日志里:
手机截屏:
二、思路
【1】创建布局文件用于展示天气信息的空间
【2】用户点击触发对应天气的数据接口查询
【3】获取到信息后进行数据类解析并通过toString方法拿到string对象展示到view上
三、具体实现
【1】XML文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
<EditText
android:id="@+id/edit_city"
android:layout_width="182dp"
android:layout_height="wrap_content"
android:hint="please input your city"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.45"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.03" />
<Button
android:id="@+id/getdata"
android:layout_width="185dp"
android:layout_height="wrap_content"
android:onClick="send"
android:text="query"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#ffff"
android:background="#669999 "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.457"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.138" />
<TextView
android:id="@+id/data_show"
android:layout_width="213dp"
android:layout_height="345dp"
android:textSize="19sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.675" />
</android.support.constraint.ConstraintLayout>
【2】Activity
package com.example.tzbc.weather_demo;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.URL;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button getData;
private TextView webDataShow;
private String html;
private EditText edit_city;
private String path;
@SuppressLint("HandlerLeak")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getData = findViewById(R.id.getdata);
webDataShow = findViewById(R.id.data_show);
edit_city=findViewById(R.id.edit_city);
getData.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.getdata:
Thread thread=new Thread(new Runnable() {
@Override
public void run() {
try {
String city=edit_city.getText().toString().trim();
Log.d("001",city);
path="http://wthrcdn.etouch.cn/weather_mini?city="+city;
Log.d("002",path);
//html = HtmlService.getHtml("http://wthrcdn.etouch.cn/weather_mini?city=%E9%93%B6%E5%B7%9D");
html = HtmlService.getHtml(path);
Log.d("003",html);
//GetInfo();
analyze_json_data();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
}
public void analyze_json_data(){
//new一个weatherTools类的实例
weatherTools weatherTools=new weatherTools();
//以下代码为对JSon数据解析的操作
try{
//new 一个JSONObject,解析的是jsonText传的数据
JSONObject jsonObject=new JSONObject(html);
//创建一个JSONObject details 解析key值为data后的value
JSONObject detail=jsonObject.getJSONObject("data");
//创建temp,wear_advice,city来存储相应key值下的value
String temp=detail.getString("wendu");
String wear_advice=detail.getString("ganmao");
String city =detail.getString("city");
Log.d("091002",city+" , "+temp+" , "+wear_advice);
String fengli;
String fengxiang;
String max;
String weather;
String min;
String date;
//创建JSONArray来解析forecast下的数组中的数据
JSONArray ja=(JSONArray)detail.get("forecast");
//创建JSONObject来解析数组中各元素的数据,其中get(int Index)是用来确定解析元素的位置
JSONObject jo=(JSONObject)ja.get(0);
fengli=jo.getString("fengli");
fengxiang=jo.getString("fengxiang");
max=jo.getString("high");
weather=jo.getString("type");
min=jo.getString("low");
date=jo.getString("date");
Log.d("091003",fengli+" , "+fengxiang+" , "+max+" , "+min+" , "+weather+" , "+date);
//这里对weatherTools里的变量进行赋值
/**变量含义:
* Current_temp--->实时气温
* Advice---->衣着推荐
* City---->当前城市
* Wind_power---->风力
* Wind_dir---->风向
* Max---->最高气温
* Min---->最低气温
* Weather---->天气状况
* Date---->日期
*/
weatherTools.setCurrent_temp(temp);
weatherTools.setAdvice(wear_advice);
weatherTools.setCity(city);
weatherTools.setWind_power(fengli);
weatherTools.setWind_dir(fengxiang);
weatherTools.setMax(max);
weatherTools.setMin(min);
weatherTools.setWeather(weather);
weatherTools.setDate(date);
}catch (JSONException e){
e.printStackTrace();
}
String message=weatherTools.toString();
Log.d("091007","message is : "+message);
webDataShow.setText(message);
}
}