手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >android通过google api获取天气信息示例
android通过google api获取天气信息示例
摘要:android通过googleAPI获取天气信息复制代码代码如下:publicclassWeatherActivityextendsActi...

android通过google API获取天气信息

复制代码 代码如下:

public class WeatherActivity extends Activity {

private TextView txCity;

private Button btnSearch;

private Handler weatherhandler;

private Dialog progressDialog;

private Timer timer;

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

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

timer = new Timer();

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

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

progressDialog = new AlertDialog.Builder(this)

.setTitle("读取数据中")

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

.create();

weatherhandler = new Handler(){

public void handleMessage(Message msg){

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

searchWeather(cityName);

progressDialog.hide();

}

};

btnSearch.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

progressDialog.show();

timer.schedule(new TimerTask() {

@Override

public void run() {

Message msg = new Message();

msg.setTarget(weatherhandler);

msg.sendToTarget();

}

},100);

}

});

}

private void searchWeather(String city){

SAXParserFactory spf = SAXParserFactory.newInstance();

try {

SAXParser sp = spf.newSAXParser();

XMLReader reader = sp.getXMLReader();

XmlHandler handler = new XmlHandler();

reader.setContentHandler(handler);

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

InputStream is = url.openStream();

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

InputSource source = new InputSource(isr);

reader.parse(source);

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

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

table.removeAllViews();

for(Weather weather:weatherList){

TableRow row = new TableRow(this);

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

row.setGravity(Gravity.CENTER_VERTICAL);

ImageView img = new ImageView(this);

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

img.setMinimumHeight(80);

row.addView(img);

TextView day = new TextView(this);

day.setText(weather.getDay());

day.setGravity(Gravity.CENTER_HORIZONTAL);

row.addView(day);

TextView temp = new TextView(this);

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

temp.setGravity(Gravity.CENTER_HORIZONTAL);

row.addView(temp);

TextView condition = new TextView(this);

condition.setText(weather.getCondition());

condition.setGravity(Gravity.CENTER_HORIZONTAL);

row.addView(condition);

table.addView(row);

}

} catch (Exception e) {

e.printStackTrace();

new AlertDialog.Builder(this)

.setTitle("解析错误")

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

.setNegativeButton("确定", null)

.show();

}

}

private Drawable loadImage(String imageUrl) {

try {

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

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

}

【android通过google api获取天气信息示例】相关文章:

Android开发技巧之永不关闭的Toast信息框(长时间显示而非系统关闭)

Android不读入内存获取图像宽高信息的方法

android 大图片拖拽并缩放实现原理

android开发基础教程—三种方式实现xml文件解析

android连接wifi时获取广播地址代码

android权限大全

在Android中 获取正在运行的Service 实例

Android开场动画实例类Java代码

android通过http协议获得图片

Android 通过onDraw实现在View中绘图操作的示例

精品推荐
分类导航