安卓 天气预报
来源:互联网 发布:vb正则表达式在线测试 编辑:程序博客网 时间:2024/06/07 17:57
我想说我刚写的博客不见了,于是打算去玩的计划取消,滚回来重写了
首先呢是设置权限
<uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
然后呢我们要导入包
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile files('libs/httpclient-4.4.1.jar') compile files('libs/httpcore-4.4.1.jar')}
导完之后不要忘记加这个
packagingOptions {//导入http包的时候必须要加的以下这段话 exclude 'META-INF/DEPENDENCIES.txt' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' exclude 'META-INF/NOTICE' exclude 'META-INF/LICENSE' exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/notice.txt' exclude 'META-INF/license.txt' exclude 'META-INF/dependencies.txt' exclude 'META-INF/LGPL2.1'}
好啦 让我们再一次愉快地敲起代码来
首先当然是加线程啦,像访问网络这种复杂的事情,哀家是不会自己做的,发配给我的子民去完成吧
private class thread implements Runnable{ @Override public void run() { String todayStart = "http://api.k780.com:88/?app=weather.today&weaid="; String todayCity = local.getText().toString(); String todayEnd = "&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json"; String todayWeather = getURLConnection(todayStart + todayCity + todayEnd); System.out.println("11111"+todayWeather); Message msg = new Message(); Bundle bundle = new Bundle(); bundle.putString("todayWeather", todayWeather); msg.setData(bundle); //getBitmap(URL); handler.sendMessage(msg); }}
然后线程中有一个方法是获取接口中的内容
private String getURLConnection(String path){ String xml = ""; try { HttpClient client = new DefaultHttpClient();//创建一个http对象 HttpGet get = new HttpGet(path); HttpResponse response = client.execute(get); int code = response.getStatusLine().getStatusCode();//得到http响应结果的状态代码 Log.d("http", "code"); if(code==200){ //getEntity()是获取实体,getContent()是获取数据流 InputStream reader = response.getEntity().getContent(); BufferedReader bf = new BufferedReader(new InputStreamReader(reader)); String list = bf.readLine();//读一行 while(list!=null){ xml += list; list = bf.readLine(); } } } catch (IOException e) { e.printStackTrace(); } System.out.println("22222" + xml); return xml;}做完之后呢 要使用handler处理一下
public class Myhandler extends Handler { public void handleMessage(Message msg) { String todayWeather = msg.getData().getString("todayWeather"); if(!todayWeather.equals("")){ try { todayWeather = String.valueOf(new JSONObject(todayWeather).getJSONObject("result")); JSONObject json = new JSONObject(todayWeather); time.setText(json.getString("days")); temp.setText(json.getString("temperature")); weather.setText(json.getString("weather")); wind.setText(json.getString("wind")); URL = json.getString("weather_icon"); thread2 t2 = new thread2(); t2.start(); } catch (JSONException e) { e.printStackTrace(); } } }}在handler里面通过json解析,获取我们要的东西啦
如果宝宝们还想通过天气更改图标的话就要再加一个线程啦
private class thread2 extends Thread{ @Override public void run() { try{ URL myUrl = new URL(URL); HttpURLConnection conn = (HttpURLConnection)myUrl.openConnection(); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestMethod("GET"); conn.connect(); Log.d("abc",String.valueOf(conn.getResponseCode())); InputStream is = conn.getInputStream(); Log.d("is", String.valueOf(is)); bitmap = BitmapFactory.decodeStream(is); Log.d("nine", "true"); is.close(); Message m = Message.obtain(); m.what=2; phandler.sendMessage(m); Log.d("is","true"); } catch (Exception e) { e.printStackTrace(); } Log.d("bitmap", "true"); }}然后处理,将获取的图片添加到ui中
public class Phandler extends Handler { public void handleMessage(Message msg) { if(msg.what==2){ pic.setImageBitmap(bitmap); } }}由于我一开始对线程不太理解,所以在这里浪费了很多时间,其实就是主线程呢自己不想做太复杂的事情,然后他就把事情交给苦逼的子线程去做,子线程做完了呢,主线程使用handler处理一下就ok了.
好啦,终于把总结写完了,安卓学了这么久也还是不太会,有啥理解错的就告诉我吧,这次我真的要开心的去玩耍了
0 0
- 安卓 天气预报
- 安卓实现天气预报功能
- 基于安卓WebServicw天气预报demo
- 安卓Web Service实现天气预报功能
- 安卓-天气预报(逐渐整理)
- 用百度天气接口的实现安卓天气预报(json数据)
- 学习安卓开发过程之中,自己倒腾的两个小项目,一个是音乐播放器,一个是天气预报。
- 天气预报
- 天气预报
- 天气预报
- 天气预报
- 天气预报
- 天气预报
- 天气预报
- 天气预报
- 天气预报
- 天气预报
- 天气预报
- 开源项目circular-progress-button源码解析
- uml的关联关系中聚合与组合的区别
- RSA加密和解密实现
- java泛型(一)、泛型的基本介绍和使用
- Android Studio多渠道打包
- 安卓 天气预报
- 不规则图形 尖角转圆角
- Android 非常好用的万能BaseAdapter
- hdoj 5665 Lucky 【水】
- JavaScript 对象(1)
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- adb 查看wifi命令
- 图片突出显示(jquery)
- 卡尔曼滤波器跟踪