package dongua.listsql;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Weather extends Activity {
ImageView pngtpye;
TextView text_wd;
TextView text_fx;
TextView text_gm;
TextView firdayinfo;
TextView secdayinfo;
TextView thrdayinfo;
TextView foudayinfo;
TextView fifdayinfo;
String CityID;
String CityNameEn;
String WeatherUrl;
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 1:
String text1 = msg.obj.toString();
text_wd.setText("温度:"+text1);
break;
case 2:
String text2 = msg.obj.toString();
text_fx.setText("风向:"+text2);
break;
case 3:
String text3 = msg.obj.toString();
text_gm.setText("小贴士:"+text3);
break;
case 4:
String text4 = msg.obj.toString();
if(text4.equals("晴"))
pngtpye.setImageResource(R.drawable.qingtian);
else if(text4.equals("多云"))
pngtpye.setImageResource(R.drawable.duoyun);
else if(text4.equals("中雨"))
pngtpye.setImageResource(R.drawable.zhongyu);
else if(text4.equals("小雨"))
pngtpye.setImageResource(R.drawable.xiaoyu);
break;
case 5:
String text5 = msg.obj.toString();
firdayinfo.setText(text5);
break;
case 6:
String text6 = msg.obj.toString();
secdayinfo.setText(text6);
break;
case 7:
String text7 = msg.obj.toString();
thrdayinfo.setText(text7);
break;
case 8:
String text8 = msg.obj.toString();
foudayinfo.setText(text8);
break;
case 9:
String text9 = msg.obj.toString();
fifdayinfo.setText(text9);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weather);
text_wd =(TextView)findViewById(R.id.text_wendu);
text_fx =(TextView)findViewById(R.id.text_fengxiang);
text_gm =(TextView)findViewById(R.id.text_ganmao);
pngtpye=(ImageView)findViewById(R.id.pngtype);
firdayinfo=(TextView)findViewById(R.id.firdayinfo);
secdayinfo=(TextView)findViewById(R.id.secdayinfo);
thrdayinfo=(TextView)findViewById(R.id.thrdayinfo);
foudayinfo=(TextView)findViewById(R.id.foudayinfo);
fifdayinfo=(TextView)findViewById(R.id.fifdayinfo);
Intent intent = getIntent();
CityID = intent.getStringExtra("CityID");
WeatherUrl ="http://wthrcdn.etouch.cn/weather_mini?citykey="+"101010200";
//WeatherUrl ="http://www.weather.com.cn/adat/sk/"+CityID+".html";
//
// CityNameEn = intent.getStringExtra("CityNameEn");
// WeatherUrl ="https://api.thinkpage.cn/v3/weather/now.json?key=ryualdb6d8cgv8zd&location="+CityNameEn+"&language=zh-Hans&unit=c";
sendRequestWithHttpURLConnection();
}
private void sendRequestWithHttpURLConnection(){
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
try {
URL url = new URL(WeatherUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
if (connection.getResponseCode() == 200) {// 判断请求码是否200,否则为失败
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
parseJSONWithJSONObject(response.toString());
}
}
catch ( Exception e){
e.printStackTrace();
}
finally {
if(connection!=null);
connection.disconnect();
}
}
}).start();
}
private void parseJSONWithJSONObject(String jsonData){
try{
JSONObject jsonObject = new JSONObject(jsonData);
JSONObject data = jsonObject.getJSONObject("data");
JSONArray forecast = data.getJSONArray("forecast");
JSONObject firstday =forecast.optJSONObject(0);
JSONObject secondday =forecast.optJSONObject(1);
JSONObject thirdday =forecast.optJSONObject(2);
JSONObject fourthdday =forecast.optJSONObject(3);
JSONObject fifthday =forecast.optJSONObject(4);
String wendu = data.getString("wendu");
String fengxiang = firstday.getString("fengxiang");
String ganmao = data.getString("ganmao");
String flowt = firstday.getString("low");
String fhight = firstday.getString("high");
String ftype = firstday.getString("type");
String fdate = firstday.getString("date");
String finfo =fdate+" "+ftype+"\n"+flowt+"~"+fhight;
String slowt = secondday.getString("low");
String shight = secondday.getString("high");
String stype = secondday.getString("type");
String sdate = secondday.getString("date");
String sinfo =sdate+" "+stype+"\n"+slowt+"~"+shight;
String tlowt = thirdday.getString("low");
String thight = thirdday.getString("high");
String ttype = thirdday.getString("type");
String tdate = thirdday.getString("date");
String tinfo =tdate+" "+ttype+"\n"+tlowt+"~"+thight;
String folowt = fourthdday.getString("low");
String fohight = fourthdday.getString("high");
String fotype = fourthdday.getString("type");
String fodate = fourthdday.getString("date");
String foinfo =fodate+" "+fotype+"\n"+folowt+"~"+fohight;
String filowt = fifthday.getString("low");
String fihight = fifthday.getString("high");
String fitype = fifthday.getString("type");
String fidate = fifthday.getString("date");
String fiinfo =fidate+" "+fitype+"\n"+filowt+"~"+fihight;
if(wendu==null)
{wendu ="error";}
else {
Message message1 = new Message();
message1.what = 1;
message1.obj =wendu;
mHandler.sendMessage(message1);
Message message2 = new Message();
message2.what = 2;
message2.obj =fengxiang;
mHandler.sendMessage
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ListviewSql源码.rar (21个子文件)
main
AndroidManifest.xml 898B
java
dongua
listsql
SelectCity.java 5KB
MainActivity.java 3KB
Weather.java 9KB
res
values
styles.xml 207B
strings.xml 183B
dimens.xml 216B
mipmap-xxhdpi
ic_launcher.png 8KB
layout
activity_main.xml 1005B
item.xml 1KB
selectcity.xml 795B
weather.xml 6KB
mipmap-xhdpi
ic_launcher.png 5KB
mipmap-hdpi
ic_launcher.png 3KB
drawable
zhongyu.png 45KB
duoyun.png 31KB
qingtian.png 59KB
xiaoyu.png 42KB
mipmap-mdpi
ic_launcher.png 2KB
menu
menu_main.xml 367B
values-w820dp
dimens.xml 364B
共 21 条
- 1
资源评论
pxM_Wxd
- 粉丝: 14
- 资源: 24
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功