Android 端天气预报APP的实现(二)阿里云天气预报API的获取
来源:互联网 发布:阿里云10m带宽 编辑:程序博客网 时间:2024/04/28 02:48
上一章基本实现了控件的滑动效果,那么其中的数据是怎么获取的呢~
我使用的是阿里云天气预报api,使用阿里云提供的API,需要遵循以下几步:
1. 百度搜索阿里云免费天气预报API,进入阿里云云市场。
2. 这个API是免费版的,只要不是商业开发,应该可以满足我们的需求。有了API,第一步应该看的是这个API的请求方式,返回的数据格式,以及最重要的是可以请求到多少数据,比如一定要有天气质量,未来几日天气等等。下面这张图就可以看到所有你想知道的。这也是阿里云自己提供的,请求方式为get,返回类型为json,左侧还可以选择你想获取哪些类型的天气信息。这里我选的是id或地名查询7天预报。
3. 再往下拉你会发现阿里云还把每种语言下具体的请求方式示例全都列举了出来,良心网站啊!!不过我在使用它提供的示例时出现了一些问题,不知道大家会不会遇到,那么我就来说说我具体是怎么获取的吧~
1)首先,将Java请求示例中的代码拷贝下来,这里我稍微做了一下更改,因此我在使用时出现了一些问题,更改之后的是完全可以正常获取的。
final String host = "http://ali-weather.showapi.com"; final String path = "/area-to-weather"; final String method = "GET"; String appcode = "c402356e77824e7d8a23eebf6cf6d0b5"; final Map<String, String> headers = new HashMap<>(); final Map<String, String> querys = new HashMap<>(); headers.put("Authorization", "APPCODE " + appcode); querys.put("area", "济南"); querys.put("need3HourForcast", "0"); querys.put("needAlarm", "0"); querys.put("needHourData", "1"); querys.put("needIndex", "1"); querys.put("needMoreDay", "1"); String content = ""; HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "utf-8")); for (String s = reader.readLine(); s != null; s = reader.readLine()) { content += s; }
2)拷贝完这写代码,你会发现很多地方全都报红,哇是不是很兴奋。首先,先按照示例的要求去下载HttpUtils类,放到项目中。
/** * Created by zhaoxin on 17/9/15. */public class HttpUtils { /** * get * * @param host * @param path * @param method * @param headers * @param querys * @return * @throws Exception */ public static HttpResponse doGet(String host, String path, String method, Map<String, String> headers, Map<String, String> querys) throws Exception { HttpClient httpClient = wrapClient(host); HttpGet request = new HttpGet(buildUrl(host, path, querys)); for (Map.Entry<String, String> e : headers.entrySet()) { request.addHeader(e.getKey(), e.getValue()); } return httpClient.execute(request); } /** * post form * * @param host * @param path * @param method * @param headers * @param querys * @param bodys * @return * @throws Exception */ public static HttpResponse doPost(String host, String path, String method, Map<String, String> headers, Map<String, String> querys, Map<String, String> bodys) throws Exception { HttpClient httpClient = wrapClient(host); HttpPost request = new HttpPost(buildUrl(host, path, querys)); for (Map.Entry<String, String> e : headers.entrySet()) { request.addHeader(e.getKey(), e.getValue()); } if (bodys != null) { List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(); for (String key : bodys.keySet()) { nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key))); } UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8"); formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8"); request.setEntity(formEntity); } return httpClient.execute(request); } /** * Post String * * @param host * @param path * @param method * @param headers * @param querys * @param body * @return * @throws Exception */ public static HttpResponse doPost(String host, String path, String method, Map<String, String> headers, Map<String, String> querys, String body) throws Exception { HttpClient httpClient = wrapClient(host); HttpPost request = new HttpPost(buildUrl(host, path, querys)); for (Map.Entry<String, String> e : headers.entrySet()) { request.addHeader(e.getKey(), e.getValue()); } if (StringUtils.isNotBlank(body)) { request.setEntity(new StringEntity(body, "utf-8")); } return httpClient.execute(request); } /** * Post stream * * @param host * @param path * @param method * @param headers * @param querys * @param body * @return * @throws Exception */ public static HttpResponse doPost(String host, String path, String method, Map<String, String> headers, Map<String, String> querys, byte[] body) throws Exception { HttpClient httpClient = wrapClient(host); HttpPost request = new HttpPost(buildUrl(host, path, querys)); for (Map.Entry<String, String> e : headers.entrySet()) { request.addHeader(e.getKey(), e.getValue()); } if (body != null) { request.setEntity(new ByteArrayEntity(body)); } return httpClient.execute(request); } /** * Put String * * @param host * @param path * @param method * @param headers * @param querys * @param body * @return * @throws Exception */ public static HttpResponse doPut(String host, String path, String method, Map<String, String> headers, Map<String, String> querys, String body) throws Exception { HttpClient httpClient = wrapClient(host); HttpPut request = new HttpPut(buildUrl(host, path, querys)); for (Map.Entry<String, String> e : headers.entrySet()) { request.addHeader(e.getKey(), e.getValue()); } if (StringUtils.isNotBlank(body)) { request.setEntity(new StringEntity(body, "utf-8")); } return httpClient.execute(request); } /** * Put stream * * @param host * @param path * @param method * @param headers * @param querys * @param body * @return * @throws Exception */ public static HttpResponse doPut(String host, String path, String method, Map<String, String> headers, Map<String, String> querys, byte[] body) throws Exception { HttpClient httpClient = wrapClient(host); HttpPut request = new HttpPut(buildUrl(host, path, querys)); for (Map.Entry<String, String> e : headers.entrySet()) { request.addHeader(e.getKey(), e.getValue()); } if (body != null) { request.setEntity(new ByteArrayEntity(body)); } return httpClient.execute(request); } /** * Delete * * @param host * @param path * @param method * @param headers * @param querys * @return * @throws Exception */ public static HttpResponse doDelete(String host, String path, String method, Map<String, String> headers, Map<String, String> querys) throws Exception { HttpClient httpClient = wrapClient(host); HttpDelete request = new HttpDelete(buildUrl(host, path, querys)); for (Map.Entry<String, String> e : headers.entrySet()) { request.addHeader(e.getKey(), e.getValue()); } return httpClient.execute(request); } private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException { StringBuilder sbUrl = new StringBuilder(); sbUrl.append(host); if (!StringUtils.isBlank(path)) { sbUrl.append(path); } if (null != querys) { StringBuilder sbQuery = new StringBuilder(); for (Map.Entry<String, String> query : querys.entrySet()) { if (0 < sbQuery.length()) { sbQuery.append("&"); } if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) { sbQuery.append(query.getValue()); } if (!StringUtils.isBlank(query.getKey())) { sbQuery.append(query.getKey()); if (!StringUtils.isBlank(query.getValue())) { sbQuery.append("="); sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8")); } } } if (0 < sbQuery.length()) { sbUrl.append("?").append(sbQuery); } } return sbUrl.toString(); } private static HttpClient wrapClient(String host) { HttpClient httpClient = new DefaultHttpClient(); if (host.startsWith("https://")) { sslClient(httpClient); } return httpClient; } private static void sslClient(HttpClient httpClient) { try { SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] xcs, String str) { } public void checkServerTrusted(X509Certificate[] xcs, String str) { } }; ctx.init(null, new TrustManager[]{tm}, null);// SSLSocketFactory ssf = new SSLSocketFactory(ctx);// ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);// ClientConnectionManager ccm = httpClient.getConnectionManager();// SchemeRegistry registry = ccm.getSchemeRegistry();// registry.register(new Scheme("https", 443, ssf)); } catch (KeyManagementException ex) { throw new RuntimeException(ex); } catch (NoSuchAlgorithmException ex) { throw new RuntimeException(ex); } }}
3)这个时候,你会发现更多地方开始报红了,哇是不是又开始兴奋了。那么我们怎么一步步的修复呢~
首先,先把我上面注释的东西给注释了
然后,导包,因为HttpClient现在已经被丢弃了,如果我们还想使用它,那么必须手动导包。这些jar包我该怎么给你们。。。。。(等我后续研究研究)
导包步骤:
1.在project中,将jar包放入lib文件中,然后全部选中右键add as library.
4)到现在为止,应该不报红了吧,不过,要不你运行一下试试呗,是不是又出现了一些小问题。现在,你只需要将下面这段代码放入build.gradle中,然后重新make project就可以了。
在对应的“module文件.gradle”中写入android{ ....... packagingOptions { 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' } ....}
至此,阿里云的天气预报信息就可以获取到了,如果你想查看当前获取的内容,打印一下content就可以了
Log.d("data_",content);
打印结果如下:
- Android 端天气预报APP的实现(二)阿里云天气预报API的获取
- 阿里云天气预报接口代码
- 阿里云天气api调用方法。
- Android 端天气预报APP的实现(五)全国各地城市数据的获取
- Android自定义View--用贝塞尔曲线实现一加多云天气
- Android获取天气预报的两种方式(国家气象局和中央气象台天气预报API)
- Android 简易版天气预报app的实现(1)
- Android 简易版天气预报app的实现(改进版)
- Android上实现一个简单的天气预报APP(十四) 使用百度API定位城市
- Android 端天气预报APP的实现(七)实现城市定位功能
- 利用中央气象台的天气预报API实现天气预报
- Android上实现一个简单的天气预报APP(三) 获取网络数据
- Android上实现一个简单的天气预报APP(二) 配置布局
- Android 端天气预报APP的实现(一)天气显示界面之上下滑动
- Android 端天气预报APP的实现(三)JSON解析天气数据
- Android 端天气预报APP的实现(四)使用ViewPager展示天气数据
- Android 端天气预报APP的实现(六)使用DrawerLayout展示城市数据
- 基于百度地图API实现的地图形式天气预报【二】
- ubuntu如何安装libz库
- 线程同步辅助类(一)
- Java字节码框架ASM-读写字节码的用法
- 视差与深度信息
- ml_svm
- Android 端天气预报APP的实现(二)阿里云天气预报API的获取
- 自动解魔方机器人1 [算法篇]
- AD绘制不规则电路板
- struts1 跨action跳转xml配置
- js基础2-深入理解javascript之typeof和instanceof
- git window 安装
- <统计学习方法-李航第2章学习>
- 空间类型和函数不可用于此提供程序,因为未能找到程序集“Microsoft.SqlServer.Types”版本 10 或更高版本。
- 快速排序