package com.example.weather;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static final int MSG_DOWN_OK=1;
public static final int MSG_DOWN_ERROR=2;
private EditText m_cityEdit;
private Button m_queryBtn;
private ListView m_listview;
private static String m_data="";
private List<Weather> m_datalist=null;//未来5天的数据
private Weather m_nowWeather;//实时数据
private DataAdapter listAdapter;
//uiHandler
private Handler uiHandler =new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what){
case MSG_DOWN_OK:{
//更新ui
System.out.println("main thread get essage from down thread.");
displayDataToUI();
break;
}
case MSG_DOWN_ERROR:{
Toast.makeText(MainActivity.this,"下载天气预报失败!",Toast.LENGTH_LONG).show();
break;
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_weather);
m_cityEdit=(EditText)findViewById(R.id.edit_city);
m_queryBtn=(Button)findViewById(R.id.btn_query);
m_listview=(ListView)findViewById(R.id.dataListview);
//设置背景透明度,需存在背景
View view_city=findViewById(R.id.layout_city);
//view_city.getBackground().setAlpha(100);
View view_now=findViewById(R.id.layout_nowweather);
view_now.getBackground().setAlpha(100);
View view_future=findViewById(R.id.layout_future);
view_future.getBackground().setAlpha(100);
View view_icon=findViewById(R.id.layout_icon);
view_icon.getBackground().setAlpha(100);
m_queryBtn.setOnClickListener(this);
//默认天气为北京
String defaultcity=m_cityEdit.getText().toString().trim();//默认为北京
//在子线程下载数据
DownThread mythread=new DownThread(defaultcity);
mythread.start();
}//end of onCreate
@Override
public void onClick(View v) {
try {
switch (v.getId()){
case R.id.btn_query:{
String cityname=m_cityEdit.getText().toString().trim();
//输入有效性
if(cityname.equals("")){
Toast.makeText(this,"请输入要查询的城市!",Toast.LENGTH_LONG).show();
return;
}
//String citynameutf8= URLEncoder.encode(cityname,"UTF-8");
//在子线程下载数据
DownThread mythread=new DownThread(cityname);
mythread.start();
break;
}
default:
break;
}
}catch (Exception e){
e.printStackTrace();
}
}//end of onClick
/*
显示数据到ui界面
*/
private void displayDataToUI(){
System.out.println("main:"+m_data);
if (parseJsonData()==false){
//提示
Toast.makeText(this,"下载的数据有错误!",Toast.LENGTH_LONG).show();
return;
}
displayNowWeather();
listAdapter=new DataAdapter(this,R.layout.list_weather,m_datalist,m_listview);
m_listview.setAdapter(listAdapter);
}//end of displayDataToUI
@SuppressLint("SetTextI18n")
private void displayNowWeather(){
ImageView image_nowweather=(ImageView)findViewById(R.id.image_nowweather);
TextView text_nowtemp=(TextView)findViewById(R.id.text_tempnow);//温度
TextView text_nowweather=(TextView)findViewById(R.id.text_situation);//天气情况
TextView text_nowdirection=(TextView)findViewById(R.id.text_direction);//风向
TextView text_nowpower=(TextView)findViewById(R.id.text_power);//风力
TextView text_nowquality=(TextView)findViewById(R.id.text_quality);//空气质量
TextView text_city=(TextView)findViewById(R.id.text_city);//当前城市
TextView text_humidity=(TextView)findViewById(R.id.text_humidity);//湿度
image_nowweather.setImageResource(DataAdapter.getIcon(m_nowWeather.getWid()));
text_nowtemp.setText(m_nowWeather.getTemperature()+"\u2103");
text_nowweather.setText(m_nowWeather.getWeather());
text_nowdirection.setText(m_nowWeather.getDirct());
text_nowpower.setText(m_nowWeather.getPower());
text_nowquality.setText(m_nowWeather.getAqi());
text_city.setText(m_cityEdit.getText().toString().trim());
text_humidity.setText(m_nowWeather.getHumidity());
}//end of displayNowWeather
/*
解析数据
in:m_data
out:m_datalist,m_nowWeather
*/
private boolean parseJsonData(){
try {
JSONObject object=new JSONObject(m_data);
if (object.getInt("error_code")!=0){
System.out.println(object.get("error_code")+":"+object.get("reason"));
return false;
}
m_datalist=new ArrayList<Weather>();
//实时数据
m_nowWeather=new Weather();
JSONObject nowObject=object.getJSONObject("result").getJSONObject("realtime");
m_nowWeather.setTemperature(nowObject.getString("temperature"));
m_nowWeather.setWid(nowObject.getString("wid"));
m_nowWeather.setWeather(nowObject.getString("info"));
m_nowWeather.setPower(nowObject.getString("power"));
m_nowWeather.setAqi(nowObject.getString("aqi"));
m_nowWeather.setDirct(nowObject.getString("direct"));
m_nowWeather.setHumidity(nowObject.getString("humidity"));
//未来5天的天气
JSONArray futureArray=object.getJSONObject("result").getJSONArray("future");
for (int i=0;i<futureArray.length();i++){
Weather tempWeather=new Weather();
JSONObject weatherObject=futureArray.getJSONObject(i);
tempWeather.setDatastr(weatherObject.getString("date"));
tempWeather.setTemperature(weatherObject.getString("temperature"));
tempWeather.setWid(weatherObject.getJSONObject("wid").getString("day"));
tempWeather.setWeather(weatherObject.getString("weather"));
tempWeather.setDirct(weatherObject.getString("direct"));
m_datalist.add(te