安卓获取ApiStore天气数据失败
2017-01-19 12:48
127 查看
Android Studio获取信息失败,但是myeclipse中可以获取到的
public static String request(String httpUrl, String httpArg) {
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
httpUrl = httpUrl + "?" + httpArg;
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
// 填入apikey到HTTP header
connection.setRequestProperty("apikey", "3ace713028f4abe9de414dc98ade98bf");
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
System.out.println("我执行了!!!!");
}
reader.close();
result = sbf.toString();
System.out.println("我执行了!!!!");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String httpUrl = "http://apis.baidu.com/thinkpage/weather_api/suggestion";
String httpArg = "location=beijing&language=zh-Hans&unit=c&start=0&days=3";
String jsonResult = request(httpUrl, httpArg);
System.out.println(jsonResult);
public static String request(String httpUrl, String httpArg) {
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
httpUrl = httpUrl + "?" + httpArg;
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
// 填入apikey到HTTP header
connection.setRequestProperty("apikey", "3ace713028f4abe9de414dc98ade98bf");
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
System.out.println("我执行了!!!!");
}
reader.close();
result = sbf.toString();
System.out.println("我执行了!!!!");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String httpUrl = "http://apis.baidu.com/thinkpage/weather_api/suggestion";
String httpArg = "location=beijing&language=zh-Hans&unit=c&start=0&days=3";
String jsonResult = request(httpUrl, httpArg);
System.out.println(jsonResult);
相关文章推荐
- 安卓获取ApiStore天气数据失败
- PHP获取新浪天气数据
- 【Android应用开发】-(17)获取Google天气预报通过Xml和JSON解析数据
- Android使用"中国天气网"API数据通过城市名称获取天气情况
- python获取城市天气数据案例
- 利用Web Services获取简单的天气数据(三)
- webpart获取数据失败
- struts2+ajax获取jason数据失败
- android天气查询(二)之网络json数据的获取
- Android 天气预报 接口数据获取 from 中国气象网
- [开发笔记]-获取天气数据接口
- 免费获取欧洲ECMWF两年的天气预报数据:
- window phone7.1 天气预报教程(二)WebClient获取天气和json数据解析显示
- Android 获取新浪天气预报数据
- android天气查询(二)之网络json数据的获取
- 简单应用:通过经纬度获取Google天气数据
- <WP7>(三)手把手教你写天气预报程序:运用WebClient获取天气和json数据解析
- android获取全国城市数据和天气,工程结构及代码十分规范
- Android使用"中国天气网"API数据通过城市名称获取天气情况
- 用百度天气接口的实现安卓天气预报(json数据)