安卓获取ApiStore天气数据失败

移动开发 码拜 8年前 (2016-09-27) 1727次浏览
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);
解决方案

40

这个跟IDE没啥关系吧,是不是网络问题?

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明安卓获取ApiStore天气数据失败
喜欢 (0)
[1034331897@qq.com]
分享 (0)