Android 访问网络系列(二)

来源:互联网 发布:java socket连接 编辑:程序博客网 时间:2024/06/05 16:37

上篇文章是采用HttpUrlConnection接口进行访问,实际上Android已经为我们封装了一个HttpClient接口,这是由Apache提供的。GET方法的操作代码如下:

<pre name="code" class="java">private void surfInternet(){//HttpGet连接对象HttpGet httpRequest = new HttpGet("http://photocdn.sohu.com/20111123/Img326603573.jpg");//取得HttpClient对象HttpClient httpClient = new DefaultHttpClient();try {//请求HttpClient,取得HttpResponseHttpResponse httpResponse = httpClient.execute(httpRequest);//请求成功if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){//取得相关信息 取得HttpEntiyHttpEntity httpEntity = httpResponse.getEntity();InputStream is = httpEntity.getContent();bt = BitmapFactory.decodeStream(is);is.close();}} catch (Exception e) {// TODO: handle exception}runOnUiThread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubiv.setImageBitmap(bt);}});}



0 0
原创粉丝点击