Android HTTP操作(三)

来源:互联网 发布:淘宝卖家学院光云科技 编辑:程序博客网 时间:2024/05/01 06:06

1.HTTP请求的方法


2.使用Get方法发送请求

private HttpResponse httpResponse = null;
private HttpEntity httpEntity = null;
private String baseUrl = "Http://192.168.0.1:8080/serverside/name";



public void onClick(View v) {
// TODO Auto-generated method stub
String name = nameView.getText().toString();
String age = ageView.getText().toString();
String url = baseUrl + "?" + "name=" + name + "age=" + age;


//生成一个请求对象
HttpGet httpGet = new HttpGet("url");
//生成一个HTTP客户端对象
HttpClient httpClient = new DefaultHttpClient();
//使用HTTP客户端发送请求对象
 
InputStream inputStream = null;
 
try {
httpResponse = httpClient.execute(httpGet);   //调用客户端对象的execute方法把请求对象传进去,就相当于向服务端发送了一次请求,然后服务器端会给一个相遇HttpResponse
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();
}
 
}


3.使用Post放发发送请求

private String baseUrl = "Http://192.168.0.1:8080/serverside/name";

private HttpResponse httpResponse = null;
private HttpEntity httpEntity = null;


public void onClick(View v) {
// TODO Auto-generated method stub
String name = nameView.getText().toString();
String age = ageView.getText().toString();
NameValuePair nameValuePair1 = new BasicNameValuePair("name", name);
                NameValuePair nameValuePair2 = new BasicNameValuePair("age", age);
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(nameValuePair1);
                nameValuePairs.add(nameValuePair2);
                
                try{
                HttpEntity requestEntity = new UrlEncodedFormEntity(nameValuePairs);
                HttpPost httpPost = new HttpPost(baseUrl);
                HttpPost.setEntity(requestEntity);
                HttpClient httpClient = new DefaultHttpClient();
                InputStream inputStream = null;
                try {
httpResponse = httpClient.execute(httpPost);   //调用客户端对象的execute方法把请求对象传进去,就相当于向服务端发送了一次请求,然后服务器端会给一个相遇HttpResponse
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();
}
 
}
                   }catch(Exception e) {
                e.printStackTrace();
               
                }
                
};




Get和Post方法的区别

在于传递数据方式的区别

Get方法

String name = nameView.getText().toString();                                                
String age = ageView.getText().toString();
String url = baseUrl + "?" + "name=" + name + "age=" + age;

                                 HttpGet httpGet = new HttpGet("url");
//生成一个HTTP客户端对象
HttpClient httpClient = new DefaultHttpClient();
//使用HTTP客户端发送请求对象
 
InputStream inputStream = null;

Post方法

String name = nameView.getText().toString();
String age = ageView.getText().toString();
NameValuePair nameValuePair1 = new BasicNameValuePair("name", name);
                NameValuePair nameValuePair2 = new BasicNameValuePair("age", age);
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(nameValuePair1);
                nameValuePairs.add(nameValuePair2);

                try{
                HttpEntity requestEntity = new UrlEncodedFormEntity(nameValuePairs);
                HttpPost httpPost = new HttpPost(baseUrl);
                HttpPost.setEntity(requestEntity);
                HttpClient httpClient = new DefaultHttpClient();
                InputStream inputStream = null;
              
0 0
原创粉丝点击