Android_请求网络数据

try {
    //获取url  URL url = new URL("网络地址");
    //请求网络  HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    int code = urlConnection.getResponseCode();
    //判断是否返回成功  if(code==200){
        //获取网络信息  InputStream inputStream = urlConnection.getInputStream();
        BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
        builder = new StringBuilder();
        String s = "";
        //拼接  while((s=bf.readLine())!=null){
            builder.append(s);
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}
return builder.toString();

你可能感兴趣的:(Android_请求网络数据)