您的位置:首页 > 其它

安卓获取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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: