Android天氣預報(Json解析)
阿新 • • 發佈:2019-01-24
很久之前寫的一個練手專案 可以進行首頁下方的滑動 載入一週內的天氣
附上程式碼
首頁
MainActivity.java
package tianqiyubao.black.tianqiyubao;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.view.GestureDetector ;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json .JSONObject;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends FragmentActivity implements GestureDetector.OnGestureListener {
public static GestureDetector detector;//手勢識別器
public String url0;
static public int j=0;
public JSONObject jsonObj;
static public String dqcity;
final public static String url1 = "http://wthrcdn.etouch.cn/weather_mini?city=";
private Spinner spinner;
private TextView city, pm, wr, wd, qh, xq;
private ImageView image_main;
private ArrayAdapter<String> adapter;
private TextView xq1, wd1, qh1;
private ImageView image1;
private TextView xq2, wd2, qh2;
private ImageView image2;
private TextView xq3, wd3, qh3;
private ImageView image3;
private ImageView dian1,dian2;
final private int zuiduan=50;
static int n=0;
public static List<TianQi> list;
private TianQi tianqi;
final static public int YES = 1, NO = 0;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case YES:
JSONArray data = (JSONArray) msg.obj;
try {
list=new ArrayList<TianQi>();
for (int i=0;i<5;i++)
{
JSONObject object= (JSONObject) data.get(i);
String wd=object.getString("high"); //溫度
String xq=object.getString("date"); //星期
String qh=object.getString("type"); //氣候
String dd="";// 地點
String zd=object.getString("low"); //最低溫度
String img=""; // 圖片
String fl=object.getString("fengli"); //風力
tianqi=new TianQi(wd,xq,qh,dd,zd,fl,img);
list.add(tianqi);
}
JSONObject jsondata=jsonObj.getJSONObject("data");
JSONObject data1=jsondata.getJSONObject("yesterday");
String wd0=data1.getString("high"); //最高溫度
String xq0=data1.getString("date"); //星期
String qh0=data1.getString("type"); //氣候
String dd0="";// 地點
String zd0=data1.getString("low"); //最低溫度
String img0=""; // 圖片
tianqi=new TianQi(wd0,xq0,qh0,dd0,zd0,null,img0);
list.add(tianqi);
//更新首頁城市
city.setText(dqcity);
//首頁東西設定文字
//city
pm.setText(list.get(0).fl);
wr.setText(list.get(0).zd);
wd.setText(list.get(0).wd);
qh.setText(list.get(0).qh);
xq.setText(list.get(0).xq);
if(qh.getText().equals("多雲"))
{
image_main.setImageResource(R.drawable.duoyun);
}
if(qh.getText().equals("小雨"))
{
image_main.setImageResource(R.drawable.xiaoyu);
}
if(qh.getText().equals("陰"))
{
image_main.setImageResource(R.drawable.yintian);
}
if(qh.getText().equals("大雨"))
{
image_main.setImageResource(R.drawable.dayu);
}
//+1天
xq1.setText(list.get(1).xq);
wd1.setText(list.get(1).wd);
qh1.setTextColor(Color.WHITE);
qh1.setText(list.get(1).qh);
if(qh1.getText().equals("多雲"))
{
image1.setImageResource(R.drawable.duoyun);
}
if(qh1.getText().equals("小雨"))
{
image1.setImageResource(R.drawable.xiaoyu);
}
if(qh1.getText().equals("陰"))
{
image1.setImageResource(R.drawable.yintian);
}
if(qh1.getText().equals("大雨"))
{
image1.setImageResource(R.drawable.dayu);
}
//+2天
xq2.setText(list.get(2).xq);
qh2.setTextColor(Color.WHITE);
wd2.setText(list.get(2).wd);
qh2.setText(list.get(2).qh);
if(qh2.getText().equals("多雲"))
{
image2.setImageResource(R.drawable.duoyun);
}
if(qh2.getText().equals("小雨"))
{
image2.setImageResource(R.drawable.xiaoyu);
}
if(qh2.getText().equals("陰"))
{
image2.setImageResource(R.drawable.yintian);
}
if(qh2.getText().equals("大雨"))
{
image2.setImageResource(R.drawable.dayu);
}
xq3.setText(list.get(3).xq);
wd3.setText(list.get(3).wd);
qh3.setTextColor(Color.WHITE);
qh3.setText(list.get(3).qh);
if(qh3.getText().equals("多雲"))
{
image3.setImageResource(R.drawable.duoyun);
}
if(qh3.getText().equals("小雨"))
{
image3.setImageResource(R.drawable.xiaoyu);
}
if(qh3.getText().equals("陰"))
{
image3.setImageResource(R.drawable.yintian);
}
if(qh3.getText().equals("大雨"))
{
image3.setImageResource(R.drawable.dayu);
}
} catch (Exception e) {
e.printStackTrace();
}
break;
case NO:
Toast.makeText(MainActivity.this, "城市資料獲取異常", Toast.LENGTH_LONG).show();
break;
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
newDate();
}
public void init(){
dian1= (ImageView) findViewById(R.id.dian1);
dian2= (ImageView) findViewById(R.id.dian2);
city= (TextView) findViewById(R.id.Activity_main_city);
pm= (TextView) findViewById(R.id.Activity_main_pm);
wr= (TextView) findViewById(R.id.Activity_main_wr);
wd= (TextView) findViewById(R.id.Activity_main_wd);
qh= (TextView) findViewById(R.id.Activity_main_qh);
xq= (TextView) findViewById(R.id.Activity_main_xq);
xq1= (TextView)findViewById(R.id.Tianqi_main_xq1);
wd1= (TextView)findViewById(R.id.Tianqi_main_wd1);
qh1= (TextView)findViewById(R.id.Tianqi_main_qh1);
xq2= (TextView)findViewById(R.id.Tianqi_main_xq2);
wd2= (TextView)findViewById(R.id.Tianqi_main_wd2);
qh2= (TextView)findViewById(R.id.Tianqi_main_qh2);
xq3= (TextView)findViewById(R.id.Tianqi_main_xq3);
wd3= (TextView)findViewById(R.id.Tianqi_main_wd3);
qh3= (TextView)findViewById(R.id.Tianqi_main_qh3);
detector=new GestureDetector(this);
image_main = (ImageView) findViewById(R.id.Activity_main_image);
image1= (ImageView) findViewById(R.id.Tianqi_main_image1);
image2= (ImageView) findViewById(R.id.Tianqi_main_image2);
image3= (ImageView) findViewById(R.id.Tianqi_main_image3);
}
public void newDate(){
spinner= (Spinner) findViewById(R.id.Activity_main_Spinner);
adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,getlist());
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
dqcity=spinner.getSelectedItem().toString();
jxDate(dqcity);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public List<String> getlist() {
List<String> list = new ArrayList<String>();
list.add("成都");
list.add("上海");
list.add("廣州");
list.add("深圳");
list.add("天津");
list.add("杭州");
list.add("南京");
list.add("濟南");
list.add("重慶");
list.add("青島");
list.add("大連");
list.add("廈門");
list.add("成都");
list.add("武漢");
list.add("哈爾濱");
list.add("瀋陽");
list.add("西安");
list.add("長春");
list.add("長沙");
list.add("福州");
list.add("鄭州");
list.add("石家莊");
list.add("佛山");
list.add("東莞");
return list;
}
//path傳城市名
public void jxDate(String path)
{
try {
url0=url1+URLEncoder.encode(path, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
new Thread(){
public void run() {
try {
URL ul=new URL(url0);
HttpURLConnection coon= (HttpURLConnection) ul.openConnection();
coon.setConnectTimeout(5000);
coon.setRequestMethod("GET");
int code=coon.getResponseCode();
if(code==200)
{
InputStream in=coon.getInputStream();
String data=StreamTool.decodeStream(in);
jsonObj=new JSONObject(data);
String result =jsonObj.getString("desc");
if ("OK".equals(result))
{
//成功
JSONObject jsondata=jsonObj.getJSONObject("data");
JSONArray jsonArray=jsondata.getJSONArray("forecast");
Message msg=Message.obtain();
msg.obj=jsonArray;
msg.what=YES;
handler.sendMessage(msg);
}else
{
//失敗
Message msg=Message.obtain();
msg.what=NO;
handler.sendMessage(msg);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return detector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > zuiduan && Math.abs(velocityX) > velocityY) {
//左邊滑動
dian1.setImageResource(R.drawable.dian1);
dian2.setImageResource(R.drawable.dian2);
getSupportFragmentManager().beginTransaction().replace(R.id.frament1, new BoomActivity_two()).commit();
//重新固定邊界
}
if (e2.getX() - e1.getX() > zuiduan && Math.abs(velocityX) > velocityY) {
//右滑動
dian1.setImageResource(R.drawable.dian2);
dian2.setImageResource(R.drawable.dian1);
getSupportFragmentManager().beginTransaction().replace(R.id.frament1, new BoomActivity_one()).commit();
}
return false;
}
}
JavaBean
city.java(城市名)
package tianqiyubao.black.tianqiyubao;
/**
* Created by Administrator on 2016/4/28 0028.
*/
public class city {
private String city;
city(String city) {
this.city = city;
}
public String setcity() {
return this.city = city;
}
public String getcity() {
return city;
}
}
TianQi.java(天氣屬性)
package tianqiyubao.black.tianqiyubao;
/**
* Created by Administrator on 2016/4/28 0028.
*/
public class TianQi {
public String wd;
public String xq;
public String qh;
public String dd;
public String zd;
public String fl;
public String img;
@Override
public String toString() {
return "TianQi{" +
"wd='" + wd + '\'' +
", xq='" + xq + '\'' +
", qh='" + qh + '\'' +
", dd='" + dd + '\'' +
", zd='" + zd + '\'' +
", fl='" + fl + '\'' +
", img='" + img + '\'' +
'}';
}
public TianQi(String wd, String xq, String qh, String dd, String zd, String fl, String img) {
this.wd = wd;
this.xq = xq;
this.qh = qh;
this.dd = dd;
this.zd = zd;
this.fl = fl;
this.img = img;
}
}
首頁初始滑動頁(預設載入) 利用Frament實現
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class BoomActivity_one extends Fragment {
public View view;
private TextView xq1, wd1, qh1;
private ImageView image1, image2, image3;
private TextView xq2, wd2, qh2;
private TextView xq3, wd3, qh3;
private MainActivity a;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_boom_activity_one, container, false);
xq1 = (TextView) view.findViewById(R.id.Tianqi_main_xq1);
wd1 = (TextView) view.findViewById(R.id.Tianqi_main_wd1);
qh1 = (TextView) view.findViewById(R.id.Tianqi_main_qh1);
xq2 = (TextView) view.findViewById(R.id.Tianqi_main_xq2);
wd2 = (TextView) view.findViewById(R.id.Tianqi_main_wd2);
qh2 = (TextView) view.findViewById(R.id.Tianqi_main_qh2);
xq3 = (TextView) view.findViewById(R.id.Tianqi_main_xq3);
wd3 = (TextView) view.findViewById(R.id.Tianqi_main_wd3);
qh3 = (TextView) view.findViewById(R.id.Tianqi_main_qh3);
image1 = (ImageView) view.findViewById(R.id.Tianqi_main_image1);
image2 = (ImageView) view.findViewById(R.id.Tianqi_main_image2);
image3 = (ImageView) view.findViewById(R.id.Tianqi_main_image3);
if(MainActivity.j++>0) {
xq1.setText(MainActivity.list.get(5).xq);
wd1.setText(MainActivity.list.get(5).wd);
qh1.setText(MainActivity.list.get(5).qh);
xq2.setText(MainActivity.list.get(1).xq);
wd2.setText(MainActivity.list.get(1).wd);
qh2.setText(MainActivity.list.get(1).qh);
xq3.setText(MainActivity.list.get(2).xq);
wd3.setText(MainActivity.list.get(2).wd);
qh3.setText(MainActivity.list.get(2).qh);
if (qh1.getText().equals("多雲")) {
image1.setImageResource(R.drawable.duoyun);
}
if (qh1.getText().equals("小雨")) {
image1.setImageResource(R.drawable.xiaoyu);
}
if (qh1.getText().equals("陰")) {
image1.setImageResource(R.drawable.yintian);
}
if (qh1.getText().equals("大雨")) {
image1.setImageResource(R.drawable.dayu);
}
if (qh2.getText().equals("多雲")) {
image2.setImageResource(R.drawable.duoyun);
}
if (qh2.getText().equals("小雨")) {
image2.setImageResource(R.drawable.xiaoyu);
}
if (qh2.getText().equals("陰")) {
image2.setImageResource(R.drawable.yintian);
}
if (qh2.getText().equals("大雨")) {
image2.setImageResource(R.drawable.dayu);
}
if (qh3.getText().equals("多雲")) {
image3.setImageResource(R.drawable.duoyun);
}
if (qh3.getText().equals("小雨")) {
image3.setImageResource(R.drawable.xiaoyu);
}
if (qh3.getText().equals("陰")) {
image3.setImageResource(R.drawable.yintian);
}
if (qh3.getText().equals("大雨")) {
image3.setImageResource(R.drawable.dayu);
}
}
return view;
}
}
右側滑動頁
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class BoomActivity_two extends Fragment {
private TextView xq4, wd4, qh4;
private ImageView image4,image5;
private TextView xq5, wd5, qh5;
private View view;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_boom_activity_two, container, false);
xq4 = (TextView) view.findViewById(R.id.Tianqi_main_xq4);
wd4 = (TextView) view.findViewById(R.id.Tianqi_main_wd4);
qh4 = (TextView) view.findViewById(R.id.Tianqi_main_qh4);
xq5 = (TextView) view.findViewById(R.id.Tianqi_main_xq5);
wd5 = (TextView) view.findViewById(R.id.Tianqi_main_wd5);
qh5 = (TextView) view.findViewById(R.id.Tianqi_main_qh5);
image4 = (ImageView) view.findViewById(R.id.Tianqi_main_image4);
image5 = (ImageView) view.findViewById(R.id.Tianqi_main_image5);
xq4.setText(MainActivity.list.get(3).xq);
wd4.setText(MainActivity.list.get(3).wd);
qh4.setText(MainActivity.list.get(3).qh);
// 總計後5天
xq5.setText(MainActivity.list.get(4).xq);
wd5.setText(MainActivity.list.get(4).wd);
qh5.setText(MainActivity.list.get(4).qh);
if(qh4.getText().equals("多雲"))
{
image4.setImageResource(R.drawable.duoyun);
}
if(qh4.getText().equals("小雨"))
{
image4.setImageResource(R.drawable.xiaoyu);
}
if(qh4.getText().equals("陰"))
{
image4.setImageResource(R.drawable.yintian);
}
if (qh4.getText().equals("大雨")) {
image4.setImageResource(R.drawable.dayu);
}
if(qh5.getText().equals("多雲"))
{
image5.setImageResource(R.drawable.duoyun);
}
if(qh5.getText().equals("小雨"))
{
image5.setImageResource(R.drawable.xiaoyu);
}
if(qh5.getText().equals("陰"))
{
image5.setImageResource(R.drawable.yintian);
}
if (qh5.getText().equals("大雨")) {
image5.setImageResource(R.drawable.dayu);
}
return view;
}
}