您的位置:首页 > 其它

WANGLUOqingqiuGj

2017-11-05 18:43 141 查看
public class NetUtil {

    private static String tag = "getNetJson";

    public static String getNetJson(String urlString) {

        try {

            //url对象封装接口字符串

            URL url = new URL(urlString);

            //用url打开连接, 返回值我们用HttpURLConnection

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

            urlConnection.setConnectTimeout(8000);//设置链接超时时间

            int responseCode = urlConnection.getResponseCode(); //获取状态码

            if (responseCode == 200) {

                InputStream inputStream = urlConnection.getInputStream();

                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

                //可拼接的字符串

                StringBuilder stringBuilder = new StringBuilder();

                String temp = "";

                while ((temp = bufferedReader.readLine()) != null) {

                    stringBuilder.append(temp);

                }

                String jsonString = stringBuilder.toString();

                return jsonString;

            } else {

            }

        } catch (MalformedURLException e) {

            e.printStackTrace();

            Log.e(tag, "getNetJson: " + e.toString());

        } catch (IOException e) {

            e.printStackTrace();

            Log.e(tag, "getNetJson: " + e.toString());

        }

        return "";

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: