java 获取天气接口

版权声明:本文为博主原创文章,未经博主允许不得转载。如果文章有问题希望在下面进行回复我一下,我再做更改。谢谢 https://blog.csdn.net/gah7788/article/details/78956337

本接口是阿里云天气接口,本接口需要一个阿里的一个工具类

下载地址:https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java前往

需要的jar:

commons-lang3-3.4.jar; 

commons-logging-1.2.jar; 

httpclient-4.5.2.jar; 

httpcore-4.4.6.jar; 

jetty-util-9.4.6.v20170531.jar; 

package test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;

/**
 * 全国天气预报查询
 */
public class weather {
	
	public static void main(String[] args) {
	   System.out.println(getWeather("杭州市"));
	}
	
	public static String getWeather(String name) {
		String host = "http://jisutqybmf.market.alicloudapi.com";
	    String path = "/weather/query";
	    String method = "GET";
	    String appcode = "你的appcode";
	    Map<String, String> headers = new HashMap<String, String>();
	    headers.put("Authorization", "APPCODE " + appcode);
	    Map<String, String> querys = new HashMap<String, String>();
	    querys.put("city", name);
	    try {
	    	HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
	    	HttpEntity httpEntity = response.getEntity();
	    	// 将HttpEntity中返回实体转化为输入流
	    	InputStream is = httpEntity.getContent();
	    	// 读取输入流,即返回文本内容
	    	BufferedReader br = new BufferedReader(new InputStreamReader(is));
	    	return br.readLine().toString();
	    } catch (Exception e) {
	    	e.printStackTrace();
	    	return "";
	    }
	}
}


展开阅读全文

没有更多推荐了,返回首页