日志里
手机截屏:
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>
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); } }
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是一个个人学习交流的平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽,造成漏登,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。