android开发第七天网络连接的四种方式

HttpURLConnection是java的标准类

HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient,更多使用 HttpClient 的应用可以参见http://wiki.apache.org/jakarta-httpclient/HttpClientPowered。HttpClient 项目非常活跃,使用的人还是非常多的。目前 HttpClient 版本是在 2005.10.11 发布的 3.0 RC4 。

HttpClient是个很不错的开源框架,封装了访问http的请求头,参数,内容体,响应等等,

HttpURLConnection是java的标准类,什么都没封装,用起来太原始,不方便,比如重访问的自定义,以及一些高级功能等。

1.HttpURLConnection get方式

try {

                URL url = new URL("http://www.baidu.com/img/baidu_sylogo1.gif");

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

                conn.setConnectTimeout(5000);

                conn.setRequestMethod("GET");

                //获得请求的响应码

                int code = conn.getResponseCode();

                if(code == HttpStatus.SC_OK){

                    InputStream inputStream = conn.getInputStream();

                    mIV.setImageBitmap(BitmapFactory.decodeStream(inputStream));

                    Toast.makeText(this, "GET访问成功", Toast.LENGTH_SHORT).show();

                }

            } catch (Exception e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

2.HttpURLConnection post方式

try {

                URL url = new URL("http://www.baidu.com/img/baidu_sylogo1.gif");

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

                conn.setConnectTimeout(5000);

                conn.setRequestMethod("POST");

                //获得请求的响应码

                int code = conn.getResponseCode();

                if(code == HttpStatus.SC_OK){

                    InputStream inputStream = conn.getInputStream();

                    mIV.setImageBitmap(BitmapFactory.decodeStream(inputStream));

                    Toast.makeText(this, "POST访问成功", Toast.LENGTH_SHORT).show();

                }

            } catch (Exception e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

3.HttpClient get方式

try {

                DefaultHttpClient httpclient = new DefaultHttpClient();

                HttpParams params = httpclient.getParams();

                HttpGet httpget = new HttpGet("http://www.baidu.com/img/baidu_sylogo1.gif");

                HttpConnectionParams.setConnectionTimeout(params, 5000);

                HttpResponse response = httpclient.execute(httpget);

                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

                    byte[] byteArray = EntityUtils.toByteArray(response.getEntity());

                    mIV.setImageBitmap(BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length));

                    Toast.makeText(this, "GET访问成功", Toast.LENGTH_SHORT).show();

                }

            } catch (Exception e) {

                e.printStackTrace();

            }

4.HttpClient post方式

try {

                DefaultHttpClient httpclient = new DefaultHttpClient();

                HttpParams params = httpclient.getParams();

                HttpPost httppost = new HttpPost("http://www.baidu.com/img/baidu_sylogo1.gif");

                HttpConnectionParams.setConnectionTimeout(params, 5000);

                HttpResponse response = httpclient.execute(httppost);

                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

                    byte[] byteArray = EntityUtils.toByteArray(response.getEntity());

                    mIV.setImageBitmap(BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length));

                    Toast.makeText(this, "POST访问成功", Toast.LENGTH_SHORT).show();

                }

            } catch (Exception e) {

                e.printStackTrace();

            }

 移动开发qq群:59516399

csdn下载链接:http://download.csdn.net/detail/wenwei19861106/4981424

你可能感兴趣的:(Android开发)