[Android]Http通信,HttpService服务端通过getEntity获取HttpClient客户端Post上来的数据

基于Apache公司的httpclient和httpservice的使用
先看httpservice对请求处理的RequestHandler方法

@Deprecated
public interface HttpRequestHandler {
    void handle(HttpRequest var1, HttpResponse var2, HttpContext var3) throws HttpException, IOException;
}

想要httpservice对client端的请求作出处理,则需要实现上边的接口,重写方法
如下

    @Override
    public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {

        BasicHttpEntityEnclosingRequest httpPost = (BasicHttpEntityEnclosingRequest) httpRequest;
        InputStream is = httpPost.getEntity().getContent();

HttpRequest 只有一个getRequestLine方法,不能像response一样getEntity啊,这让服务端咋解析嘛。所以就进源码一通乱点,找到了这里用的BasicHttpEntityEnclosingRequest这个类
查看源码如下

public class BasicHttpEntityEnclosingRequest extends BasicHttpRequest implements HttpEntityEnclosingRequest {...}
public class BasicHttpRequest extends AbstractHttpMessage implements HttpRequest {...}

最最重要的是,这个BasicHttpEntityEnclosingRequest 有getEntity方法

    public HttpEntity getEntity() {
        throw new RuntimeException("Stub!");
    }

    public void setEntity(HttpEntity entity) {
        throw new RuntimeException("Stub!");
    }

所以把 HttpService把HttpClient传来的Post请求通过向下转型成BasicHttpEntityEnclosingRequest ,再调用getEntity,即可得到Post的请求流内容
我是在用客户端Post文件流给服务器时用到了该方法,完毕~

你可能感兴趣的:(apache,android,http,post)