天气预报开发文档 - 图文(3)

2019-08-31 20:46

android:orientation=\ >

android:background=\ android:visibility=\ />

android:background=\ />

android:orientation=\>

android:id=\ android:layout_width=\ android:layout_height=\ android:src=\ android:layout_gravity=\ />

android:layout_height=\ android:textSize=\ android:textColor=\ android:background=\ android:text=\生活指数:\ />

android:background=\ android:text=\未来六天天气预报\/>

android:id=\

android:layout_width=\ android:layout_height=\ android:layout_above=\ android:text=\

android:textColor=\/>

android:id=\

android:layout_width=\ android:layout_height=\ android:layout_below=\ android:orientation=\>

3.2获得和解析数据

首先是httpget 主要代码如下:

private String connServerForResult(String strUrl) {

}

if (strResult.charAt(0) != '{')

strResult = '{' + strResult; strResult = strResult + '}';

if (strResult.charAt(strResult.length() - 1) != '}') return strResult;

} catch (ClientProtocolException e) { }

e.printStackTrace(); e.printStackTrace(); } catch (IOException e) {

// 获取HttpGet对象

HttpGet httpRequest = new HttpGet(strUrl); // httpRequest.

String strResult = \; try {

// HttpClient对象

HttpClient httpClient = new DefaultHttpClient();

// 获得HttpResponse对象 HttpResponse httpResponse = httpClient.execute(httpRequest); if (httpResponse.getStatusLine().getStatusCode() == }

// 取得返回的数据 strResult =

HttpStatus.SC_OK) {

EntityUtils.toString(httpResponse.getEntity());

然后对获得的json数据进行解析,主要代码:

// 普通Json数据解析 privatevoid parseJson(String strResult) {

try {

JSONObject jsonObj = new JSONObject(strResult)

.getJSONObject(\);

// JSONArray jsonArray =

jsonObj.getJSONArray(\

today = jsonObj.getString(\); // 当前日期 dayofweek = jsonObj.getString(\); // 当前星期 city = jsonObj.getString(\); // 城市名称

ftime = jsonObj.getInt(\);// 更新时间(整点)【更新时间确定

// 清空原数据 lifeV.clear(); tempes.clear(); weathers.clear(); winds.clear(); fls.clear();

// 打开SharedPreferences更新数据 Editor write = dataofday.edit();

temp1属于哪天】

write.putString(\, cityid);// 城市ID write.putInt(\, ftime);// 更新时间 write.putString(\, city); write.putString(\, today);

write.putString(\, dayofweek); for (int i = 1; i <= 6; i++) { }

// 生活指数

lifeV.add(jsonObj.getString(\));

// 六天天气的图片Id

images[i - 1] = jsonObj.getInt(\ + (i * 2 - 1)); write.putInt(\ + (i * 2 - 1),

jsonObj.getInt(\ + (i * 2 - 1)));

// 六天的气温(摄氏度)

tempes.add(jsonObj.getString(\ + i));

write.putString(\ + i, jsonObj.getString(\ + // 六天的天气描述

weathers.add(jsonObj.getString(\ + i)); write.putString(\ + i,

// 六天的风力大小及风向

winds.add(jsonObj.getString(\ + i));

write.putString(\ + i, jsonObj.getString(\ + // 六天的风力

fls.add(jsonObj.getString(\ + i));

write.putString(\ + i, jsonObj.getString(\ + i));

i));

jsonObj.getString(\ + i));

i));

lifeV.add(jsonObj.getString(\)); lifeV.add(jsonObj.getString(\)); lifeV.add(jsonObj.getString(\)); lifeV.add(jsonObj.getString(\)); lifeV.add(jsonObj.getString(\)); lifeV.add(jsonObj.getString(\)); lifeV.add(jsonObj.getString(\)); lifeV.add(jsonObj.getString(\)); lifeV.add(jsonObj.getString(\)); lifeV.add(jsonObj.getString(\)); lifeV.add(jsonObj.getString(\)); // 写入保存数据

write.putString(\, lifeV.get(0)); write.putString(\, lifeV.get(1)); write.putString(\, lifeV.get(2)); write.putString(\, lifeV.get(3)); write.putString(\, lifeV.get(4)); write.putString(\, lifeV.get(5)); write.putString(\, lifeV.get(6)); write.putString(\, lifeV.get(7)); write.putString(\, lifeV.get(8)); write.putString(\, lifeV.get(9)); write.putString(\, lifeV.get(10)); write.putString(\, lifeV.get(11)); // 提交数据

write.commit();

} catch (JSONException e) { }

return;

3.3 在UI上呈现天气信息 主要代码如下:

protectedvoidonCreate(Bundle savedInstanceState) { // 天气预报页面 super.onCreate(savedInstanceState);

setContentView(R.layout.activity_weather);

cityname = (TextView) findViewById(R.id.cityname); test = (TextView) findViewById(R.id.test); date = (TextView) findViewById(R.id.tdate); week = (TextView) findViewById(R.id.tweek); temp = (TextView) findViewById(R.id.tempe);// 温度 weather = (TextView) findViewById(R.id.weather);


天气预报开发文档 - 图文(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:RB281凿岩台车设备作业指导书 - 图文

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: