展会信息港展会大全

android通过google API获取天气信息
来源:互联网   发布日期:2015-09-24 20:30:37   浏览:1931次  

导读:[代码][Java]代码001publicclassWeatherActivityextendsActivity {002privateTextView txCity;003privateButton btnSearch;004privateHandler weatherhandler;005privateDialog progressDialog;006privateT......

[代码] [Java]代码

001

public class WeatherActivity extends Activity {

002

private TextView txCity;

003

private Button btnSearch;

004

private Handler weatherhandler;

005

private Dialog progressDialog;

006

private Timer timer;

007

/** Called when the activity is first created. */

008

@Override

009

public void onCreate(Bundle savedInstanceState) {

010

super.onCreate(savedInstanceState);

011

setContentView(R.layout.main);

012

timer = new Timer();

013

txCity = (TextView)findViewById(R.id.txCity);

014

btnSearch = (Button)findViewById(R.id.btnSearch);

015

progressDialog = new AlertDialog.Builder(this)

016

.setTitle("读取数据中")

017

.setMessage("正在加载数据,请稍等")

018

.create();

019

020

weatherhandler = new Handler(){

021

public void handleMessage(Message msg){

022

final String cityName = txCity.getText().toString().trim();

023

searchWeather(cityName);

024

progressDialog.hide();

025

}

026

};

027

028

btnSearch.setOnClickListener(new OnClickListener() {

029

030

@Override

031

public void onClick(View v) {

032

progressDialog.show();

033

timer.schedule(new TimerTask() {

034

@Override

035

public void run() {

036

Message msg = new Message();

037

msg.setTarget(weatherhandler);

038

msg.sendToTarget();

039

}

040

},100);

041

}

042

});

043

}

044

private void searchWeather(String city){

045

SAXParserFactory spf = SAXParserFactory.newInstance();

046

try {

047

SAXParser sp = spf.newSAXParser();

048

XMLReader reader = sp.getXMLReader();

049

XmlHandler handler = new XmlHandler();

050

reader.setContentHandler(handler);

051

URL url = new URL("http://www.google.com/ig/api?hl=zh-cn&weather="+URLEncoder.encode(city));

052

InputStream is = url.openStream();

053

InputStreamReader isr = new InputStreamReader(is, "GBK");

054

InputSource source = new InputSource(isr);

055

reader.parse(source);

056

List<Weather>weatherList = handler.getWeatherList();

057

TableLayout table = (TableLayout)findViewById(R.id.table);

058

table.removeAllViews();

059

for(Weather weather:weatherList){

060

TableRow row = new TableRow(this);

061

row.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

062

row.setGravity(Gravity.CENTER_VERTICAL);

063

ImageView img = new ImageView(this);

064

img.setImageDrawable(loadImage(weather.getImageUrl()));

065

img.setMinimumHeight(80);

066

row.addView(img);

067

TextView day = new TextView(this);

068

day.setText(weather.getDay());

069

day.setGravity(Gravity.CENTER_HORIZONTAL);

070

row.addView(day);

071

TextView temp = new TextView(this);

072

temp.setText(weather.getLowTemp()+"℃-"+weather.getHighTemp()+"℃");

073

temp.setGravity(Gravity.CENTER_HORIZONTAL);

074

row.addView(temp);

075

TextView condition = new TextView(this);

076

condition.setText(weather.getCondition());

077

condition.setGravity(Gravity.CENTER_HORIZONTAL);

078

row.addView(condition);

079

table.addView(row);

080

}

081

} catch (Exception e) {

082

e.printStackTrace();

083

new AlertDialog.Builder(this)

084

.setTitle("解析错误")

085

.setMessage("获取天气数据失败,请稍候再试。")

086

.setNegativeButton("确定", null)

087

.show();

088

}

089

090

}

091

private Drawable loadImage(String imageUrl) {

092

try {

093

return Drawable.createFromStream((InputStream) newURL("http://www.google.com/"+imageUrl).getContent(), "test");

094

} catch (MalformedURLException e) {

095

e.printStackTrace();

096

} catch (IOException e) {

097

e.printStackTrace();

098

}

099

return null;

100

}

101

}

[图片] 未命名.jpg

赞助本站

人工智能实验室

相关热词: 天气 API

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港