资源简介
【核心代码】
package com.example.weather; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import com.example.weather.adapter.WeatherAdapter; import com.example.weather.moder.Weather; import com.example.weather.util.HttpCallbackListener; import com.example.weather.util.HttpUtil; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.LayoutAnimationController; import android.view.animation.ScaleAnimation; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ListView; import android.widget.Toast; public class WeatherActivity extends Activity { private EditText ecCity; private ImageButton btnQuery; private ListView lvFutureWeather; public static final int SHOW_RESPONSE = 1; private List<Weather> data; private Handler handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case SHOW_RESPONSE: String response = (String) msg.obj; if (response != null) { parseWithJSON(response); WeatherAdapter weatherAdapter = new WeatherAdapter( WeatherActivity.this, R.layout.activity_weather_listitem, data); lvFutureWeather.setAdapter(weatherAdapter); ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1); scaleAnimation.setDuration(1000); LayoutAnimationController animationController = new LayoutAnimationController( scaleAnimation, 0.6f); lvFutureWeather.setLayoutAnimation(animationController); } break; default: break; } } private void parseWithJSON(String response) { // TODO Auto-generated method stub data = new ArrayList<Weather>(); JsonParser parser = new JsonParser();// json解析器 JsonObject obj = (JsonObject) parser.parse(response); // 获取返回状态吗 String resultcode = obj.get("resultcode").getAsString(); // 状态码如果是200说明数据返回成功 if (resultcode != null && resultcode.equals("200")) { JsonObject resultObj = obj.get("result").getAsJsonObject(); JsonArray futureWeatherArray = resultObj.get("future") .getAsJsonArray(); for (int i = 0; i < futureWeatherArray.size(); i ) { Weather weather = new Weather(); JsonObject weatherObject = futureWeatherArray.get(i) .getAsJsonObject(); weather.setDayOfWeek(weatherObject.get("week") .getAsString()); weather.setDate(weatherObject.get("date").getAsString()); weather.setTemperature(weatherObject.get("temperature") .getAsString()); weather.setWeather(weatherObject.get("weather") .getAsString()); data.add(weather); } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_weather); initViews(); setListeners(); } public void initViews() { ecCity = (EditText) findViewById(R.id.etCity); btnQuery = (ImageButton) findViewById(R.id.btnQuery); lvFutureWeather = (ListView) findViewById(R.id.lvFutureWeather); data = new ArrayList<Weather>(); } private void setListeners() { btnQuery.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub String cityName = ecCity.getText().toString(); try { if (cityName.equals("")) { cityName = URLEncoder.encode("广州", "utf-8"); } else { cityName = URLEncoder.encode(cityName, "utf-8"); } } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } System.out.println("lvFutureWeather=" lvFutureWeather); // Toast.makeText(WeatherActivity.this, "success", // Toast.LENGTH_LONG).show(); String weatherUrl = "http://v.juhe.cn/weather/index?format=2&cityname=" cityName "&key=b6cbbdfa81653757c2fe9a343420cee6"; // Toast.makeText(WeatherActivity.this, // "success" weatherUrl,Toast.LENGTH_LONG).show(); HttpUtil.sendHttpRequest(weatherUrl, new HttpCallbackListener() { public void onFinish(String response) { // TODO Auto-generated method stub Message message = new Message(); message.what = SHOW_RESPONSE; // 将服务器返回的结果存放到Message中 message.obj = response.toString(); handler.sendMessage(message); } public void onError(Exception e) { // TODO Auto-generated method stub System.out.println("请求失败"); } }); } }); } }
代码片段和文件信息
/** Automatically generated file. DO NOT MODIFY */
package com.example.weather;
public final class BuildConfig {
public final static boolean DEBUG = true;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 475 2016-11-07 08:52 Weather\Weather\.classpath
文件 843 2016-11-06 23:58 Weather\Weather\.project
文件 1273 2016-11-07 09:01 Weather\Weather\AndroidManifest.xm
文件 51394 2016-11-06 23:58 Weather\Weather\ic_launcher-web.png
文件 781 2016-11-06 23:58 Weather\Weather\proguard-project.txt
文件 563 2016-11-07 08:52 Weather\Weather\project.properties
文件 177 2016-11-06 23:58 Weather\Weather\.settings\org.eclipse.jdt.core.prefs
文件 1273 2016-11-07 11:10 Weather\Weather\bin\AndroidManifest.xm
文件 120 2016-11-07 11:09 Weather\Weather\bin\jarlist.cache
文件 161 2016-11-07 11:10 Weather\Weather\gen\com\example\weather\BuildConfig.java
文件 17236 2016-11-07 11:10 Weather\Weather\gen\com\example\weather\R.java
文件 385685 2016-11-06 23:58 Weather\Weather\libs\android-support-v4.jar
文件 0 2016-11-06 23:58 Weather\Weather\libs\gson-2.1-javadoc.jar
文件 124849 2016-11-06 23:58 Weather\Weather\libs\gson-2.1-sources.jar
文件 180110 2016-11-06 23:58 Weather\Weather\libs\gson-2.1.jar
文件 378 2016-11-06 23:58 Weather\Weather\res\anim\weather_list_animation.xm
文件 229 2016-11-06 23:58 Weather\Weather\res\anim\weather_list_layout_animation.xm
文件 419745 2016-11-06 23:58 Weather\Weather\res\drawable-hdpi\city.jpg
文件 465 2016-11-06 23:58 Weather\Weather\res\drawable-hdpi\etcity.png
文件 7658 2016-11-06 23:58 Weather\Weather\res\drawable-hdpi\ic_launcher.png
文件 115239 2016-11-06 23:58 Weather\Weather\res\drawable-hdpi\op.jpg
文件 1737 2016-11-06 23:58 Weather\Weather\res\drawable-hdpi\qqq.php.png
文件 1883 2016-11-06 23:58 Weather\Weather\res\drawable-hdpi\serch.png
文件 30083 2016-11-06 23:58 Weather\Weather\res\drawable-hdpi\weather.png
文件 205 2016-11-06 23:58 Weather\Weather\res\drawable-ldpi\list_item_shape.xm
文件 3777 2016-11-06 23:58 Weather\Weather\res\drawable-mdpi\ic_launcher.png
文件 12516 2016-11-06 23:58 Weather\Weather\res\drawable-xhdpi\ic_launcher.png
文件 24777 2016-11-06 23:58 Weather\Weather\res\drawable-xxhdpi\ic_launcher.png
文件 1833 2016-11-06 23:58 Weather\Weather\res\layout\activity_weather.xm
文件 1651 2016-11-07 09:00 Weather\Weather\res\layout\activity_weather_listitem.xm
............此处省略54个文件信息
- 上一篇:基于地图的计步器
- 下一篇:二维码扫描 支持扫描手机内图片
相关资源
- android 息屏亮屏 (呼吸效果)
- 精仿微信程序android源码
- 《kotlin-for-android-developers-zh》(kotli
- android bluetooth 搜尋距離
- Android应用源码追加的6中图片特效处理
- 完美解决android_gif_drawable_demo
- android 调整屏幕亮度 (无需root)
- android通过ajax上传文件到php服务器
- android 联动滚轮(选择控件)
-
android_自定义ti
tleBar - android 异常崩溃后 重启app(进程守护
- QQSlidingMenu(QQ5.0的侧滑菜单特性)
- SwipeRefreshLayoutDemo(下拉刷新)
- TurntableDemo(圆形菜单)
- ViewPagerAnimator(ViewPager动画切换)
- DialogPlusDemo(对话框组件,可做上下弹
- 最完整的伺服培训教程(图文并茂)
- 2017年数模国赛优秀论文
- android 字母导航
- android 日期选择控件(可选择小时和分
- 电力电子技术册_第五版_王兆安_课后
- Android滑动解锁
- 安卓JNA例程
- Android 分贝测试仪(多媒体-10_091)
- Android 弹出底部弹窗(5.0 +效果(Mat
- Android 炫酷引导页(启动页合集-21_2
- Android 调用系统文件(网络编程-13_1
- Android 多线程腾讯新闻客户端(网络编
- Android 选择头像的注册(App 应用开发
- Android 语音发短信(语音识别-11_100)
评论
共有 条评论