http操作

image

为什么http协议叫做超文本传输协议, 因为应用http协议不仅仅能传输文本, 还能传输图片, 音频, 视频等等.

http协议是应用层通信协议.

image

image

image

什么叫做报文, 报文就是数据

请求报文 = 请求数据, it业就爱瞎拽词

image

讲如何利用这套api, 发送一个http请求, 以及接受服务器的响应.

package org.apache.http

如何发送带参数的http请求:

1. 用Get方式发送http请求.

private String baseUrl = "http://192.168.1.100:8081/serverside/name";
public void onClick(View v) {
                String url = baseUrl + "?" + "name=" + "zhangsan" + "&age=" + "20";  //参数是键值对, 键值对用&进行连接.
                                                                                                   //  url地址是: 
http://192.168.1.100:8081/serverside/name?name=zhangsan&age=20
                private HttpResponse httpResponse = null;

                //生成一个请求对象
                HttpGet httpGet = new HttpGet(url);
                //生成一个Http客户端对象
                HttpClient httpClient = new DefaultHttpClient();
                InputStream inputStream = null;
                try {

                     //使用Http客户端发送请求对象
                    httpResponse = httpClient.execute(httpGet);
                    httpEntity = httpResponse.getEntity();          //从http响应中得到http entity.
                    inputStream = httpEntity.getContent();         //从http entity中得到内容.
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                    String result = "";
                    String line = "";
                    while((line = reader.readLine()) != null){
                        result = result + line;
                    }
                    System.out.println(result);                       //把http的响应内容打印出来.
                } catch (Exception e) {
                    e.printStackTrace();
                }
                finally{
                    try{
                        inputStream.close();
                    }
                    catch(Exception e){
                        e.printStackTrace();
                    }
                }
}

 

2. 用Post方式发送http请求.

public void onClick(View v) {
                NameValuePair nameValuePair1 = new BasicNameValuePair("name","zhangsan");  // NameValuePair对象代表一个键值对
                NameValuePair nameValuePair2 = new BasicNameValuePair("age","20");
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();          //把要发送的键值对参数放到一个list中去
                nameValuePairs.add(nameValuePair1);
                nameValuePairs.add(nameValuePair2);
                try {
                            HttpEntity requestHttpEntity = new UrlEncodeFormEntiry(nameValuePairs);  //把参数数据封装在http entity中.
                            HttpPost httpPost = new HttpPost(baseUrl);                                          //生成一个httpPost对象.
                            httpPost.setEntity(requestHttpEntity);                                                 //为http post请求设置http entity.
                            HttpClient httpClient = new DefaultHttpClient();
                            InputStream inputStream = null;
                            httpResponse = httpClient.execute(httpPost);                                       //下面的代码和使用http get方式相同.
                            httpEntity = httpResponse.getEntity();
                            inputStream = httpEntity.getContent();
                            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                            String result = "";
                            String line = "";
                            while((line = reader.readLine()) != null){
                                result = result + line;
                            }
                            System.out.println(result);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                finally{
                    try{
                        inputStream.close();
                    }
                    catch(Exception e){
                        e.printStackTrace();
                    }
                }
}

 

Browser apk中哪里用到了http操作?

class DownloadTouchIcon用到了 http get请求.

FBReader的ZLNetworkManager.java中用到了 http post请求.

posted @ 2011-12-08 14:04  ahking17  阅读(302)  评论(0编辑  收藏  举报