HttpURLConnection请求数据

public static String getstr(String path) {
        try {
            URL url = new URL(path);

            HttpURLConnection conn = (HttpURLConnection) url
                    .openConnection();

            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);

            if (conn.getResponseCode() == 200) {
                InputStream inputStream = conn.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
                StringBuffer sf = new StringBuffer();
                String str = "";
                while ((str = br.readLine()) != null) {
                    sf.append(str);
                }

                String info = sf.toString();
                Log.e("TAG", info);
                return info;
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

你可能感兴趣的:(HttpURLConnection请求数据)