android通过google api获取天气信息示例

时间:2021-05-20

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:///"+imageUrl).getContent(), "test");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章