您的位置:首页 > 移动开发 > Android开发

Android通过异步任务获取天气信息

2013-11-29 10:55 435 查看
//异步任务获取天气信息的类

package com.kj.control.task;

import org.json.JSONException;

import org.json.JSONObject;

import com.kj.control.base.Common;

import com.kj.util.WeatherUtils;

import com.kj.util.DB.DBHelper;

import com.kj.util.DB.DBManager;

import android.content.Context;

import android.database.Cursor;

import android.os.AsyncTask;

import android.os.Handler;

import android.os.Message;

import android.util.Log;

public class GetWeatherDataTask extends AsyncTask<Object, Integer, String> {

private Handler handler;

private Context context;

public GetWeatherDataTask(Handler handler, Context context) {

this.handler = handler;

this.context = context;

}

@Override

protected String doInBackground(Object... params) {

// TODO Auto-generated method stub

if(params.length==1){

DBHelper helper = new DBHelper(context);

DBManager manager = new DBManager(context);

manager.copyDatabase();

String cityCode = null;

//params[0]表示传入参数数组的第一个元素

String sql = "select * from city_table where CITY =" + "'" + params[0] + "'"

+ ";";

Cursor cursor = helper.getReadableDatabase().rawQuery(sql, null);

if (cursor.getCount()>0) {

cursor.moveToFirst();

//这里通过城市名查询数据库找到对应的城市ID(获取天气数据需要有城市ID)

cityCode = cursor.getString(cursor.getColumnIndex("WEATHER_ID"));

}

cursor.close();

helper.close();

// 一周内的天气信息链接uri

String weatherUrl = "http://m.weather.com.cn/data/" + cityCode

+ ".html";

String weatherInfo = WeatherUtils.connServerForResult(weatherUrl);

try {

JSONObject jsonObject = new JSONObject(weatherInfo)

.getJSONObject("weatherinfo");

//Common只是一个自己定义的类用来存放获取到的天气信息

Common.index = jsonObject.get("index").toString();

Common.index_d = jsonObject.get("index_d").toString();

Common.index_uv = jsonObject.get("index_uv").toString();

Common.index_xc = jsonObject.get("index_xc").toString();

Common.index_tr = jsonObject.get("index_tr").toString();

Common.index_co = jsonObject.get("index_co").toString();

Common.index_cl = jsonObject.get("index_cl").toString();

Common.index_ls = jsonObject.get("index_ls").toString();

Common.index_ag = jsonObject.get("index_ag").toString();

Common.city = jsonObject.get("city").toString();//获取城市名

Common.date_y = jsonObject.get("date_y").toString();//获取当天日期

Common.week = jsonObject.get("week").toString();//获取星期几

Common.weather1=jsonObject.getString("weather1").toString();//获取当天天气情况

Common.wind1=jsonObject.getString("wind1").toString();//获取当天风力情况

// 六天的温度

Common.temp1 = jsonObject.get("temp1").toString();

Common.temp2 = jsonObject.get("temp2").toString();

Common.temp3 = jsonObject.get("temp3").toString();

Common.temp4 = jsonObject.get("temp4").toString();

Common.temp5 = jsonObject.get("temp5").toString();

Common.temp6 = jsonObject.get("temp6").toString();

// 图片对应六天的天气

Common.img1 = jsonObject.get("img1").toString();

Common.img2 = jsonObject.get("img2").toString();

Common.img3 = jsonObject.get("img3").toString();

Common.img4 = jsonObject.get("img4").toString();

Common.img5 = jsonObject.get("img5").toString();

Common.img6 = jsonObject.get("img6").toString();

Common.img7 = jsonObject.get("img7").toString();

Common.img8 = jsonObject.get("img8").toString();

Common.img9 = jsonObject.get("img9").toString();

Common.img10 = jsonObject.get("img10").toString();

Common.img11 = jsonObject.get("img11").toString();

Common.img12 = jsonObject.get("img12").toString();

// 分别对应图片的描述

Common.img_title1 = jsonObject.get("img_title1").toString();

Common.img_title2 = jsonObject.get("img_title2").toString();

Common.img_title3 = jsonObject.get("img_title3").toString();

Common.img_title4 = jsonObject.get("img_title4").toString();

Common.img_title5 = jsonObject.get("img_title5").toString();

Common.img_title6 = jsonObject.get("img_title6").toString();

Common.img_title7 = jsonObject.get("img_title7").toString();

Common.img_title8 = jsonObject.get("img_title8").toString();

Common.img_title9 = jsonObject.get("img_title9").toString();

Common.img_title10 = jsonObject.get("img_title10")

.toString();

Common.img_title11 = jsonObject.get("img_title11")

.toString();

Common.img_title12 = jsonObject.get("img_title12")

.toString();

} catch (JSONException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return weatherInfo;

}

return null;

}

@Override

protected void onPostExecute(String result) {

// TODO Auto-generated method stub

super.onPostExecute(result);

if (null != handler) {

Message msg = handler.obtainMessage(Common.GET_DATA_COMPLETE,

result);

msg.sendToTarget();

}

}

@Override

protected void onPreExecute() {

// TODO Auto-generated method stub

super.onPreExecute();

if (null != handler) {

handler.sendEmptyMessage(Common.GET_DATA_START);

}

}

}

//这是数据库辅助类

package com.kj.util.DB;

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

public static final int DATABASE_VERSION = 1;

public DBHelper(Context context) {

super(context, DBManager.DB_NAME, null, DATABASE_VERSION);

}

@Override

public void onCreate(SQLiteDatabase db) {

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}

//这是数据库管理类(注意:需要下载citychina.db这个存储中国所有城市名称和对应ID的文件,并在res下新建文件raw,将citychina.db放在raw目录下)

package com.kj.util.DB;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import com.kj.app.R;

import android.content.Context;

import android.os.Environment;

public class DBManager {

private final int BUFFER_SIZE = 400000;

public static final String PACKAGE_NAME = "com.kj.app";

public static final String DB_NAME = "myapp.db";

public static final String DB_PATH = "/data"

+ Environment.getDataDirectory().getAbsolutePath() + "/"

+ PACKAGE_NAME + "/databases";

private Context context;

public DBManager(Context context) {

this.context = context;

}

/** copy the database under raw*/

public void copyDatabase() {

File file = new File(DB_PATH);

if (!file.isDirectory())

file.mkdir();

String dbfile = DB_PATH + "/" + DB_NAME;

try {

if (new File(dbfile).length() == 0) {

FileOutputStream fos = new FileOutputStream(dbfile);

byte[] buffer = new byte[BUFFER_SIZE];

readDB(fos, buffer, R.raw.citychina);

fos.close();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

private void readDB(FileOutputStream fos, byte[] buffer, int db_id)

throws IOException {

int count;

InputStream is;

is = this.context.getResources().openRawResource(db_id);

while ((count = is.read(buffer)) > 0) {

fos.write(buffer, 0, count);

}

is.close();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐