PageRenderTime 3093ms CodeModel.GetById 34ms RepoModel.GetById 3ms app.codeStats 0ms

/src/com/sinonetwork/zhonghua/Qixiang.java

https://gitlab.com/zwp/Zhonghua
Java | 612 lines | 448 code | 85 blank | 79 comment | 46 complexity | 6ae81b2c398d8637cb162fc5014abcd7 MD5 | raw file
  1. package com.sinonetwork.zhonghua;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.text.DecimalFormat;
  7. import java.text.SimpleDateFormat;
  8. import java.util.ArrayList;
  9. import java.util.Date;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. import org.apache.http.util.EncodingUtils;
  13. import org.json.JSONArray;
  14. import org.json.JSONException;
  15. import org.json.JSONObject;
  16. import android.content.Intent;
  17. import android.location.Location;
  18. import android.location.LocationManager;
  19. import android.os.Bundle;
  20. import android.text.format.Time;
  21. import android.util.Log;
  22. import android.view.MotionEvent;
  23. import android.view.View;
  24. import android.view.View.OnClickListener;
  25. import android.widget.AdapterView;
  26. import android.widget.AdapterView.OnItemClickListener;
  27. import android.widget.ImageView;
  28. import android.widget.ListView;
  29. import android.widget.TextView;
  30. import android.widget.Toast;
  31. import com.alibaba.fastjson.JSON;
  32. import com.amap.api.location.AMapLocation;
  33. import com.amap.api.location.AMapLocationListener;
  34. import com.amap.api.location.LocationManagerProxy;
  35. import com.amap.api.location.LocationProviderProxy;
  36. import com.android.volley.Request.Method;
  37. import com.android.volley.Response;
  38. import com.android.volley.error.VolleyError;
  39. import com.android.volley.request.FastJsonRequest;
  40. import com.sinonetwork.zhonghua.R.string;
  41. import com.sinonetwork.zhonghua.adapter.QixiangAdapter;
  42. import com.sinonetwork.zhonghua.model.TAwss;
  43. import com.sinonetwork.zhonghua.model.Warning;
  44. import com.sinonetwork.zhonghua.net.RequestManager;
  45. import com.sinonetwork.zhonghua.parser.LandInfoParser;
  46. import com.sinonetwork.zhonghua.utils.HttpUtil;
  47. import com.sinonetwork.zhonghua.utils.URLAddress;
  48. import com.sinonetwork.zhonghua.utils.log.Logger;
  49. public class Qixiang extends LandBaseActivity implements AMapLocationListener,
  50. Runnable {
  51. private string[] listt;
  52. private ListView listView;
  53. private QixiangAdapter adapter;
  54. private AMapLocation aMapLocation;// 用于判断定位超时
  55. private int index;
  56. private double getLat;
  57. private double getLng;
  58. private ImageView back;
  59. private ImageView qixiang_warm;
  60. private ImageView qixiang_solid;
  61. private ImageView qixiang_query;
  62. private TextView qixiang_temp_content; // 温度
  63. private TextView qixiang_humidity_content; // 湿度
  64. private TextView qixiang_precipitation_content; // 降水量
  65. private TextView qixiang_wushuangqi_content; // 无霜期
  66. private TextView qixiang_sunlight_content; // 日照时长
  67. private TextView qixiang_summarize1; // 风向
  68. private TextView qixiang_summarize2; // 风力
  69. private TextView qixiang_time; // 发布日期
  70. private TextView qixiang_address; // 地区
  71. private TextView qixiang_temperature; // 摄氏度
  72. private LocationManagerProxy aMapLocManager = null;
  73. private double latitude = 0.0;
  74. private double longitude = 0.0;
  75. private LocationManager locationManager;
  76. protected ArrayList<Warning> list;
  77. protected JSONObject jsonObj;
  78. protected JSONObject jsonObjAwss;
  79. private String cityCode = "";
  80. private String cityma;
  81. private String cityquyu = "";
  82. protected void onCreate(Bundle savedInstanceState) {
  83. super.onCreate(savedInstanceState);
  84. setContentView(R.layout.qixiang);
  85. initView();
  86. index = 0;
  87. init();
  88. // 灾害预警
  89. qixiang_warm.setOnClickListener(new OnClickListener() {
  90. @Override
  91. public void onClick(View v) {
  92. if (null != list) {
  93. Intent intent = new Intent(Qixiang.this, QixiangWarm.class);
  94. Bundle bundle = new Bundle();
  95. bundle.putSerializable("data", list);
  96. intent.putExtras(bundle);
  97. startActivity(intent);
  98. }
  99. }
  100. });
  101. // 土壤墒情
  102. qixiang_solid.setOnClickListener(new OnClickListener() {
  103. @Override
  104. public void onClick(View v) {
  105. Intent intent = new Intent(Qixiang.this, QixiangSolid.class);
  106. intent.putExtra("data", cityma);
  107. startActivity(intent);
  108. }
  109. });
  110. // 积温查询
  111. qixiang_query.setOnClickListener(new OnClickListener() {
  112. @Override
  113. public void onClick(View v) {
  114. Intent intent = new Intent(Qixiang.this, QixiangQuery.class);
  115. intent.putExtra("data", cityma);
  116. startActivity(intent);
  117. }
  118. });
  119. //返回键
  120. back.setOnClickListener(new OnClickListener() {
  121. @Override
  122. public void onClick(View v) {
  123. finish();
  124. }
  125. });
  126. }
  127. //初始化控件
  128. private void initView() {
  129. listView = (ListView) findViewById(R.id.qixiang_lv);
  130. back = (ImageView) findViewById(R.id.back);
  131. qixiang_warm = (ImageView) findViewById(R.id.qixiang_warm);
  132. qixiang_solid = (ImageView) findViewById(R.id.qixiang_solid);
  133. qixiang_query = (ImageView) findViewById(R.id.qixiang_query);
  134. qixiang_temp_content = (TextView) findViewById(R.id.qixiang_temp_content);
  135. qixiang_humidity_content = (TextView) findViewById(R.id.qixiang_humidity_content);
  136. qixiang_precipitation_content = (TextView) findViewById(R.id.qixiang_precipitation_content);
  137. qixiang_wushuangqi_content = (TextView) findViewById(R.id.qixiang_wushuangqi_content);
  138. qixiang_sunlight_content = (TextView) findViewById(R.id.qixiang_sunlight_content);
  139. qixiang_summarize1 = (TextView) findViewById(R.id.qixiang_summarize1);
  140. qixiang_summarize2 = (TextView) findViewById(R.id.qixiang_summarize2);
  141. qixiang_time = (TextView) findViewById(R.id.qixiang_time);
  142. qixiang_address = (TextView) findViewById(R.id.qixiang_address);
  143. qixiang_temperature = (TextView) findViewById(R.id.qixiang_temperature);
  144. }
  145. @Override
  146. protected void onStart() {
  147. // init();
  148. super.onStart();
  149. }
  150. // 初始化定位
  151. private void init() {
  152. aMapLocManager = LocationManagerProxy.getInstance(this);
  153. /*
  154. * mAMapLocManager.setGpsEnable(false);//
  155. * 1.0.2版本新增方法,设置true表示混合定位中包含gps定位,false表示纯网络定位,默认是true Location
  156. * API定位采用GPS和网络混合定位方式
  157. * ,第一个参数是定位provider,第二个参数时间最短是2000毫秒,第三个参数距离间隔单位是米,第四个参数是定位监听者
  158. */
  159. aMapLocManager.requestLocationData(LocationProviderProxy.AMapNetwork,
  160. 2000, 10, this);
  161. aMapLocManager.setGpsEnable(false);//纯网络定位
  162. handler.postDelayed(this, 12000);// 设置超过12秒还没有定位到就停止定位
  163. }
  164. /**
  165. * 混合定位回调函数
  166. */
  167. @Override
  168. public void onLocationChanged(AMapLocation location) {
  169. if (location != null) {
  170. // mListener.onLocationChanged(location);// 显示系统小蓝点
  171. this.aMapLocation = location;// 判断超时机制
  172. getLat = location.getLatitude();
  173. getLng = location.getLongitude();
  174. String desc = "";
  175. Bundle locBundle = location.getExtras();
  176. if (locBundle != null) {
  177. cityCode = locBundle.getString("citycode");
  178. desc = locBundle.getString("desc");
  179. cityquyu = location.getDistrict();// 定位到区域 套转到积温查询界面时
  180. }
  181. String str = ("定位成功:(" + getLat + "," + getLng + ")"
  182. + "\n精 度 :" + location.getAccuracy() + "米"
  183. + "\n定位方式:" + location.getProvider() + "\n定位时间:"
  184. + "\n城市编码:" + cityCode + "\n位置描述:" + desc + "\n省:"
  185. + location.getProvince() + "\n市:" + location.getCity()
  186. + "\n区(县):" + location.getDistrict() + "\n区域编码:" + location
  187. .getAdCode());
  188. Log.i("gxx", str);
  189. initDingWei();
  190. // 初次定位成功去接口请求数据
  191. if (index == 0) {
  192. index++;
  193. try {
  194. loadWarningData(location.getProvince(), location.getCity(),
  195. location.getDistrict());
  196. loadCityData(location.getProvince(), location.getCity(),
  197. location.getDistrict());
  198. } catch (JSONException e) {
  199. e.printStackTrace();
  200. }
  201. }
  202. }
  203. }
  204. /**
  205. * 获取气象数据
  206. *
  207. * @param method
  208. * 方法名
  209. * @param latitude
  210. * 纬度
  211. * @param longitude
  212. * //经度
  213. */
  214. public void RiZhaoShiChang(String method, String latitude, String longitude) {
  215. Map<String, String> map = new HashMap<String, String>();
  216. map.put("method", method);
  217. map.put("latitude", latitude);
  218. map.put("longitude", longitude);
  219. //使用Volley框架获取信息
  220. FastJsonRequest<TAwss> fastJson = new FastJsonRequest<TAwss>(
  221. Method.POST, URLAddress.TAwssURL, TAwss.class, null, map,
  222. new Response.Listener<TAwss>() {
  223. @Override
  224. public void onResponse(TAwss weather) {
  225. if (weather.getResultdata() == null) {
  226. Toast.makeText(Qixiang.this, "没有日照数据",
  227. Toast.LENGTH_SHORT).show();
  228. } else {
  229. // qixiang_wushuangqi_content.setText(weather.getResultdata());
  230. // //无霜期
  231. qixiang_sunlight_content.setText(weather
  232. .getResultdata().getSunShine().toString()
  233. + "小时"); // 日照时长
  234. }
  235. }
  236. }, new Response.ErrorListener() {
  237. @Override
  238. public void onErrorResponse(VolleyError arg0) {
  239. Toast.makeText(Qixiang.this, "服务器异常!",
  240. Toast.LENGTH_SHORT).show();
  241. }
  242. });
  243. RequestManager.addRequest(fastJson, this);
  244. }
  245. /**
  246. * 获取灾害预警列表
  247. */
  248. private void loadWarningData(final String provinceName,
  249. final String cityName, final String countyName) {
  250. showLoadProgressBar();
  251. new Thread(new Runnable() {
  252. @Override
  253. public void run() {
  254. try {
  255. list = LandInfoParser.getWarningList(provinceName,
  256. cityName, countyName);
  257. } catch (Exception e) {
  258. success = false;
  259. e.printStackTrace();
  260. }
  261. handler.post(new Runnable() {
  262. @Override
  263. public void run() {
  264. if (success) {
  265. setView();
  266. } else {
  267. showShortToast("获取数据失败");
  268. }
  269. hideLoadProgressBar();
  270. }
  271. });
  272. }
  273. }).start();
  274. }
  275. /**
  276. * 显示灾害预警列表的信息
  277. */
  278. private void setView() {
  279. if (null != list) {
  280. if (list.size() > 0) {
  281. adapter = new QixiangAdapter(Qixiang.this, list);
  282. listView.setAdapter(adapter);
  283. listView.setOnItemClickListener(new OnItemClickListener() {
  284. @Override
  285. public void onItemClick(AdapterView<?> arg0, View arg1,
  286. int arg2, long arg3) {
  287. Intent intent = new Intent(Qixiang.this,
  288. WarningDetailActivity.class);
  289. Bundle bundle = new Bundle();
  290. bundle.putSerializable("data", list.get(arg2));
  291. intent.putExtras(bundle);
  292. startActivity(intent);
  293. }
  294. });
  295. } else {
  296. showShortToast("当前地区没有灾害预警信息");
  297. }
  298. }
  299. }
  300. private void initDingWei() {
  301. String shuju = getJson("siteInfo.json");// 把文件中的json信息读取为json数据
  302. String a = new String(shuju);
  303. StringBuffer sb = new StringBuffer();
  304. sb.append(cityquyu);
  305. sb.delete(sb.length() - 1, sb.length());
  306. String diqu2 = sb.toString();
  307. // 使用自己的类来对数据进行解析
  308. JiWenCityCode code = JSON.parseObject(a, JiWenCityCode.class);
  309. for (int i = 0; i < code.getCity().size(); i++) {
  310. if (diqu2.equals(code.getCity().get(i).getC())) {
  311. cityma = code.getCity().get(i).getA();
  312. // Log.i("gxx", "城市的编码需要传递的:"+cityma);
  313. }
  314. }
  315. }
  316. private String getJson(String fileName) {
  317. StringBuilder stringBuilder = new StringBuilder();
  318. try {
  319. BufferedReader bf = new BufferedReader(new InputStreamReader(
  320. getAssets().open(fileName)));
  321. String line;
  322. while ((line = bf.readLine()) != null) {
  323. stringBuilder.append(line);
  324. }
  325. } catch (IOException e) {
  326. e.printStackTrace();
  327. }
  328. return stringBuilder.toString();
  329. }
  330. private void loadCityData(String province, String city, String district)
  331. throws JSONException {
  332. String siteInfo = getFromAssets("siteInfo.json");
  333. // Log.v(TAG, "province--" + province);
  334. // Log.v(TAG, "city--" + city);
  335. // Log.v(TAG, "district--" + district);
  336. // district="昌平";
  337. JSONObject obj = new JSONObject(siteInfo);
  338. JSONArray cityArray = obj.getJSONArray("city");
  339. int size = cityArray.length();
  340. String code = "";
  341. for (int i = 0; i < size; i++) {
  342. JSONObject o = cityArray.getJSONObject(i);
  343. if (province.indexOf(o.getString("g")) > -1) {
  344. if (city.indexOf(o.getString("e")) > -1) {
  345. if (district.indexOf(o.getString("c")) > -1) {
  346. code = o.getString("a");
  347. } else {
  348. }
  349. }
  350. }
  351. }
  352. if (code.length() == 0) {
  353. for (int i = 0; i < size; i++) {
  354. JSONObject o = cityArray.getJSONObject(i);
  355. if (province.indexOf(o.getString("g")) > -1) {
  356. if (city.indexOf(o.getString("e")) > -1) {
  357. if (city.indexOf(o.getString("c")) > -1) {
  358. code = o.getString("a");
  359. }
  360. }
  361. }
  362. }
  363. }
  364. final String cityCode = code;
  365. SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
  366. Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
  367. final String dateStr = formatter.format(curDate);
  368. Time t = new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
  369. t.setToNow(); // 取得系统时间。
  370. int year = t.year;
  371. int month = t.month + 1;
  372. int date = t.monthDay;
  373. int hour = t.hour; // 0-23
  374. int minute = t.minute;//0-60
  375. int second = t.second;
  376. //时间的处理
  377. if(minute>=0 && minute<10){
  378. qixiang_time.setText("" + year + "年" + month + "月" + date + "日"
  379. + " " + hour + ":" +"0"+ minute);
  380. }else{
  381. qixiang_time.setText("" + year + "年" + month + "月" + date + "日"
  382. + " " + hour + ":" + minute);}
  383. showLoadProgressBar();
  384. new Thread(new Runnable() {
  385. @Override
  386. public void run() {
  387. try {
  388. String url = "http://product.xn121.com/api/?areaid="
  389. + cityCode
  390. + "&type=observe&date="
  391. + dateStr
  392. + "&appid=ade570&key=41a6e865052ade5702d2e773f2164a5f";
  393. String urlAwss = "http://product.xn121.com/api/?areaid="
  394. + cityCode
  395. + "&type=awss&date="
  396. + dateStr
  397. + "&appid=ade570&key=41a6e865052ade5702d2e773f2164a5f";
  398. // Log.i("gxx", "url--" + url);
  399. // Log.i("gxx", "url2--" + url2);
  400. // Log.i("gxx", "urlAwss--" + urlAwss);
  401. // Log.i("gxx", "urlAwss2--" + urlAwss2);
  402. // Log.i("gxx", "datastr:"+dateStr);
  403. // Log.i("gxx", "codecity:"+cityCode);
  404. jsonObj = HttpUtil.getJSONObjFromUrlByGet(url, false);
  405. jsonObjAwss = HttpUtil.getJSONObjFromUrlByGet(urlAwss,
  406. false);
  407. // Log.i("gxx", "jsonObj:"+jsonObj);
  408. // Log.i("gxx", "jsonObjAwss:"+jsonObjAwss);
  409. // Log.v("zhong", "jsonObjAwss.getString--" +
  410. // jsonObjAwss.toString());
  411. success = true;
  412. } catch (Exception e) {
  413. success = false;
  414. e.printStackTrace();
  415. }
  416. handler.post(new Runnable() {
  417. @Override
  418. public void run() {
  419. if (success) {
  420. try {
  421. setCityView();
  422. } catch (JSONException e) {
  423. // TODO Auto-generated catch block
  424. e.printStackTrace();
  425. }
  426. } else {
  427. showShortToast("获取数据失败");
  428. }
  429. hideLoadProgressBar();
  430. }
  431. });
  432. }
  433. }).start();
  434. }
  435. private void setCityView() throws JSONException {
  436. // Log.i("gxx", "setcityview方法");
  437. if (null != jsonObj) {
  438. if (jsonObj.getJSONArray("datas").length() > 0) {
  439. JSONObject obj = (JSONObject) jsonObj.getJSONArray("datas")
  440. .get(0);
  441. qixiang_precipitation_content.setText(obj.getString("d17")
  442. + "mm"); // 降水量
  443. qixiang_address.setText(obj.getString("d5")); // 地址
  444. // Log.i("gxx", "set的获取的地址:" + obj.getString("d5"));
  445. qixiang_temp_content.setText(obj.getString("d13") + "℃"); // 温度
  446. // Log.i("gxx", "温度:" + obj.getString("d13") + "℃");
  447. // Log.i("gxx", "shuju:" + obj);
  448. qixiang_temperature.setText(obj.getString("d13") + "℃"); // 温度
  449. qixiang_humidity_content.setText(obj.getString("d16") + "%"); // 湿度
  450. // Log.i("gxx", "湿度:"+obj.getString("d16") + "%");
  451. qixiang_summarize2.setText("风力" + obj.getString("d15") + "级"); // 风
  452. }
  453. }
  454. if (null != jsonObjAwss) {
  455. // Log.v("zhong", "jsonObjAwss.getString--" +
  456. // jsonObjAwss.toString());
  457. if (jsonObjAwss.getJSONArray("datas").length() > 0) {
  458. JSONObject o = (JSONObject) jsonObjAwss.getJSONArray("datas")
  459. .get(0);
  460. // if(o.getString("d18").equals("0")){
  461. // qixiang_sunlight_content.setText( "0小时"); // 时长
  462. // }else{
  463. Double time = Double.valueOf(o.getString("d18")) * 0.1;
  464. DecimalFormat df = new DecimalFormat("######0.00");
  465. String t = df.format(time);
  466. qixiang_sunlight_content.setText("" + t + "小时"); // 时长
  467. // }
  468. }
  469. }
  470. }
  471. @Override
  472. public void run() {
  473. if (aMapLocation == null) {
  474. stopLocation();// 销毁掉定位12秒内还没有定位成功,停止定位
  475. }
  476. }
  477. @Override
  478. public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
  479. // TODO Auto-generated method stub
  480. }
  481. public String getFromAssets(String fileName) {
  482. String result = "";
  483. try {
  484. InputStream in = getResources().getAssets().open(fileName);
  485. // 获取文件的字节数
  486. int lenght = in.available();
  487. // 创建byte数组
  488. byte[] buffer = new byte[lenght];
  489. // 将文件中的数据读到byte数组中
  490. in.read(buffer);
  491. result = EncodingUtils.getString(buffer, "UTF-8");// 你的文件的编码
  492. } catch (Exception e) {
  493. e.printStackTrace();
  494. }
  495. return result;
  496. }
  497. /**
  498. * 方法必须重写
  499. */
  500. @Override
  501. protected void onPause() {
  502. super.onPause();
  503. stopLocation();// 停止定位
  504. }
  505. /**
  506. * 销毁定位
  507. */
  508. private void stopLocation() {
  509. if (aMapLocManager != null) {
  510. aMapLocManager.removeUpdates(this);
  511. aMapLocManager.destroy();
  512. }
  513. aMapLocManager = null;
  514. }
  515. @Override
  516. public void onLocationChanged(Location arg0) {
  517. // TODO Auto-generated method stub
  518. }
  519. @Override
  520. public void onProviderDisabled(String arg0) {
  521. // TODO Auto-generated method stub
  522. }
  523. @Override
  524. public void onProviderEnabled(String arg0) {
  525. // TODO Auto-generated method stub
  526. }
  527. }