首页 > 代码库 > Android 读取网络数据

Android 读取网络数据

通过HttpResponse  实现

首先在AndroidManifest中配置允许网络访问    

<uses-permissionandroid:name="android.permission.INTERNET"/>

然后实现代码

 

 示例代码如下

 

String url = "http://www.baidu.com";

HttpGet request = new HttpGet(url);

 

              try {

                     HttpResponseresponse = new DefaultHttpClient().execute(request);

                     HttpEntityentity = response.getEntity();

                     inputStream= entity.getContent();

                    

                     System.out.println("连接成功");

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

                    

                     Stringline= "";

                     line= reader.readLine();

                     while(line!=null){

                           

                            System.out.println(line);

                            Line=reader.readLine();

 

}

                    

                    

                     System.out.println("下载结束");