【问题标题】:Android : apache HttpComponents CoreAndroid : apache HttpComponents 核心
【发布时间】:2014-09-08 06:40:53
【问题描述】:

我想将org.apache.http 添加到我的项目中,我下载并将其添加到我的项目中,我开发了一些测试代码,但是当我从我的项目中删除org.apache.http 时,我的代码工作正常,添加了@987654323 android SDK默认@@,不需要添加到项目中?

public class ElementalHttpGet {

    public static void main(String[] args) throws Exception {
        HttpProcessor httpproc = HttpProcessorBuilder.create()
            .add(new RequestContent())
            .add(new RequestTargetHost())
            .add(new RequestConnControl())
            .add(new RequestUserAgent("Test/1.1"))
            .add(new RequestExpectContinue(true)).build();

        HttpRequestExecutor httpexecutor = new HttpRequestExecutor();

        HttpCoreContext coreContext = HttpCoreContext.create();
        HttpHost host = new HttpHost("localhost", 8080);
        coreContext.setTargetHost(host);

        DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(8 * 1024);
        ConnectionReuseStrategy connStrategy = DefaultConnectionReuseStrategy.INSTANCE;

        try {
            String[] targets = {
                "/",
                "/servlets-examples/servlet/RequestInfoExample",
                "/somewhere%20in%20pampa"};

            for (int i = 0; i < targets.length; i++) {
                if (!conn.isOpen()) {
                    Socket socket = new Socket(host.getHostName(), host.getPort());
                    conn.bind(socket);
                }
                BasicHttpRequest request = new BasicHttpRequest("GET", targets[i]);
                System.out.println(">> Request URI: " + request.getRequestLine().getUri());

                httpexecutor.preProcess(request, httpproc, coreContext);
                HttpResponse response = httpexecutor.execute(request, conn, coreContext);
                httpexecutor.postProcess(response, httpproc, coreContext);

                System.out.println("<< Response: " + response.getStatusLine());
                System.out.println(EntityUtils.toString(response.getEntity()));
                System.out.println("==============");
                if (!connStrategy.keepAlive(response, coreContext)) {
                    conn.close();
                } else {
                    System.out.println("Connection kept alive...");
                }
            }
        } finally {
            conn.close();
        }
    }
}

【问题讨论】:

    标签: java android androidhttpclient


    【解决方案1】:

    是的,org.apache.http 是 Android API 的一部分。

    http://developer.android.com/reference/org/apache/http/package-summary.html

    但它似乎是 Apache HttpClient 过时版本的一个分支:
    https://hc.apache.org/httpcomponents-client-4.3.x/android-port.html

    【讨论】:

    • 这是在 SDK 20 中添加的?
    • 不,好像从第一个版本就已经存在了。参考 API 表示“在 API 级别 1 中添加”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 2017-07-22
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多