android下使用HttpClient发起get和post请求

HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。android的sdk中已经附带了HttpClient,下面我们来看一下,如何使用HttpClient发起GET和POST请求:

GET请求:

import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.apache.http.message.BasicNameValuePair;
import java.io.*;
import java.util.*;

public class HttpClientTest
{
    public static String GetDemo()
    {
        String url = "http://localhost/test/test.php?username=test&password=test";
        HttpClient httpClient = new DefaultHttpClient();
        //此句类似于打开一个浏览器,HttpClient是接口类型
        HttpGet httpGet = new HttpGet(url);
        try{
            HttpResponse response = httpClient.execute(httpGet);
			//得到状态码
			int code = response.getStatusLine().getStatusCode();
			if(code == 200){
				//得到一个输入流
				InputStream is = response.getEntity().getContent();
				BufferedReader bfr = new BufferedReader(new InputStreamReader(is));//使用BufferdReader读取数据
				String line = null;
				StringBuilder sb = new StringBuilder();
				while((line=bfr.readLine())!=null){
				    sb.append(line);
				}
				if(sb==null){
				    return null;
				}else{
				    return sb.toString();
				}
			}else {
				return null;
			}			
        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException("请求失败!");
            return null;
        }
    }
}


POST请求:

import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.apache.http.message.BasicNameValuePair;
import java.io.*;
import java.util.*;

public class HttpClientTest
{
    public static String PostDemo()
    {
        String url = "http:/localhost/android/login.php";
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost hPost = new HttpPost(url);
		
		//设置数据实体
		List
  
  
   
    parameters = new ArrayList
   
   
    
    ();
		//NameValuePair是一个接口类型,它的实现类BasicNameValuePair
		parameters.add(new BasicNameValuePair("username",username));
		parameters.add(new BasicNameValuePair("password",password));		
		UrlEncodedFormEntity entity;
		try {
			entity = new UrlEncodedFormEntity(parameters, "utf-8");
			//指定要提交的数据实体,并设置编码格式
			hPost.setEntity(entity);
			HttpResponse response = httpClient.execute(hPost);//执行post请求
			int code = response.getStatusLine().getStatusCode();
			if(code==200){
				//得到输入流
				InputStream is = response.getEntity().getContent();
				BufferedReader bfr = new BufferedReader(new InputStreamReader(is));
				String line = null;
				StringBuilder sb = new StringBuilder();
				while((line=bfr.readLine())!=null){
				    sb.append(line);
				}
				if(sb==null){
				    return null;
				}else{
				    return sb.toString();
				}
			}else{
				return null;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
    }
}

   
   
  
  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HttpClient是Java中的一个开源库,用于支持HTTP协议的客户端编程。它是一个用于发送HTTP请求和接收HTTP响应的包装工具类。HttpClient可以被用于执行GET和POST请求等HTTP方法。走看看是一个基于Web的应用程序,包含了各种常见的网站功能,如搜索、资讯、体育、财经、购物等多个频道。下面将分别介绍HttpClient发送GET和POST请求时的一些重要知识点。 HttpClient发送GET请求时,需要构造一个HttpGet对象,并指定请求的URL。调用HttpClient.execute方法,并且将HttpGet对象传递给该方法。接下来,HttpClient会发送GET请求到指定的URL,然后将响应内容作为一个HttpResponse对象返回给程序。可以从HttpResponse对象中获取响应状态、响应头和响应体等信息。 HttpClient发送POST请求时,需要首先构造一个HttpPost对象,并指定请求的URL。调用HttpPost.setEntity方法来设置请求体内容,然后调用HttpClient.execute方法,并将HttpPost对象传递给该方法。接下来,HttpClient会将POST请求数据发送到指定的URL,然后将响应内容作为一个HttpResponse对象返回给程序。与GET请求相似,可以从HttpResponse对象中获取响应状态、响应头和响应体等信息。 总的来说,HttpClient是一个十分强大和方便的网络编程工具类,可以方便地实现HTTP请求和响应的处理。可以根据自己的需求选择GET和POST请求发送,然后获取响应内容和各种信息。使用HttpClient能够简化开发,提高编程效率,是Java网络编程开发中非常重要的一种库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值