发送一个HTTP-POST请求 [英] Sending an HTTP-post request

查看:276
本文介绍了发送一个HTTP-POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想送从我的Andr​​oid客户端的HTTP / HTTPS post请求。

问题

为什么我的code失败?

到目前为止

我创建了一个apache类/ HttpClient的电话。一切优良的工作:

  HttpClient的HttpClient的=新DefaultHttpClient();
 

我读过,这种方法已经去precated,所以我切换到新的推荐方法:

 的HttpClient的HttpClient = HttpClientBuilder.create()建立()。
 

Eclipse中没有这个类,所以我不得不下载Apache的HttpClient 4.3.3。我导入到我的项目通过复制到了文件夹并将其添加到我的构建路径(进口HttpClient的,HttpClient的缓存,的HttpCore,httpmime,流畅-HC,公-logging,commons- codeC)。

错误信息

  2月6号至五日:15:26.946:W / dalvikvm(29587):类的链接Lorg /阿帕奇/ HTTP / IMPL /康涅狄格州/ PoolingHttpClientConnectionManager;失败
二月六日至五日:15:26.946:E / dalvikvm(29587):找不到类的org.apache.http.impl.conn.PoolingHttpClientConnectionManager,从法引用org.apache.http.impl.client.HttpClientBuilder.build
 

最近code

 静态私人字符串insertJson(JSON字符串,字符串URL){
    HttpClient的HttpClient的= HttpClientBuilder.create()建立()。

    字符串responseString =;
    尝试 {
        HttpPost请求=新HttpPost(URL);
        StringEntity PARAMS =新StringEntity(JSON,UTF-8);
        request.addHeader(内容类型,应用/ JSON);
        request.setEntity(PARAMS);
        HTT presponse响应= httpClient.execute(要求);
        HttpEntity实体= response.getEntity();
        responseString = EntityUtils.toString(实体,UTF-8);

    }赶上(例外前){
        ex.printStackTrace();
        //这里处理异常
    } 最后 {
        。httpClient.getConnectionManager()关闭();
    }
    返回responseString;
}
 

解决方案

问题是,Android的已经包含了一个旧版本(<一href="http://stackoverflow.com/questions/2618573/what-version-of-apache-http-client-is-bundled-in-android-1-6/4818714#4818714">it's目前还不清楚到底是哪中的Apache的HttpClient的,而是围绕4.0beta2)。

当你添加新版本的应用程序库的罐子,重复类的APK加载时忽略。由于一些的的类HttpClient的依赖的修改的就这些其他类的Dalvik做什么就可以(如删除引用,和C),但除非他们是有条件地使用,这很可能导致崩溃。

例如,你可以看到像这样在logcat的消息:

  06-05 00:46:39.083:I / dalvikvm(6286):找不到方法org.apache.http.client.protocol.RequestDefaultHeaders&LT; INIT&gt;中引用。从方法org.apache.http.impl.client.HttpClientBuilder.build
06-05 00:46:39.083:W / dalvikvm(6286):VFY:无法解决直接的方法22794:Lorg /阿帕奇/ HTTP /客户/协议/ RequestDefaultHeaders;&LT; INIT&GT; (Ljava / UTIL /集;)V
06-05 00:46:40.434:D / dalvikvm(6286):DexOpt:找不到静态字段Lorg /阿帕奇/ HTTP / IMPL /客户/ DefaultHtt prequestRetryHandler; .INSTANCE
06-05 00:46:40.434:W / dalvikvm(6286):VFY:无法在Lorg解决静态字段8420(实例)/阿帕奇/ HTTP / IMPL /客户/ DefaultHtt prequestRetryHandler;
 

这特别的消息,因为 DefaultHtt prequestRetryHandler <一个href="http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/DefaultHtt$p$pquestRetryHandler.html"相对=nofollow>有一个新的实例静中有4.3 领域,其中<一href="http://developer.android.com/reference/org/apache/http/impl/client/DefaultHtt$p$pquestRetryHandler.html"相对=nofollow>它并没有在Android中。还有更多的。

让我们回到你原来的问题,用新的HttpClient将重命名的所有的类,以便这些名称不会发生冲突的唯一途径。这是 httpclientandroidlib 一样。

去进一步回:DefaultHttpClient <一href="http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/DefaultHttpClient.html"相对=nofollow>确实已经去$ P $ 4.3 pcated,但它的不是德$ P $,除非你考虑的对使用趋势的HttpURLConnection 的德precation一个隐形的形式 - 在这种情况下,新的HttpClient不会是preferred替代其一)。到底为什么你想/需要改变呢?

I'm trying to send an HTTP/HTTPS post request from my Android client.

Question

Why does my code fail?

So far

I created an apache class/HttpClient call. Everything worked fine:

HttpClient httpClient = new DefaultHttpClient();

I've read that this method has been deprecated, so I've switched to the new recommended method:

HttpClient httpClient = HttpClientBuilder.create().build();

Eclipse didn't have this class, so I had to download Apache HttpClient 4.3.3. I imported it to my project by copying it into the libs folder and adding them to my build path (imported httpclient, httpclient-cache, httpcore, httpmime, fluent-hc, commons-logging, commons-codec).

Error message

06-05 02:15:26.946: W/dalvikvm(29587): Link of class 'Lorg/apache/http/impl/conn/PoolingHttpClientConnectionManager;' failed
06-05 02:15:26.946: E/dalvikvm(29587): Could not find class 'org.apache.http.impl.conn.PoolingHttpClientConnectionManager', referenced from method org.apache.http.impl.client.HttpClientBuilder.build

Most recent code

static private String insertJson(String json,String url){
    HttpClient httpClient = HttpClientBuilder.create().build();

    String responseString = "";
    try {
        HttpPost request = new HttpPost(url);
        StringEntity params =new StringEntity(json, "UTF-8");
        request.addHeader("content-type", "application/json");
        request.setEntity(params);
        HttpResponse response = httpClient.execute(request);
        HttpEntity entity = response.getEntity();
        responseString = EntityUtils.toString(entity, "UTF-8");

    }catch (Exception ex) {
        ex.printStackTrace();
        // handle exception here
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
    return responseString;
}

解决方案

The problem is that Android already includes an older version (it's unclear exactly which, but around 4.0beta2) of Apache HttpClient.

When you add the jars of the new version as libraries of your application, duplicate classes are ignored when the APK is loaded. Since some new classes in HttpClient depend on modifications made on these other classes, dalvik does what it can (e.g. removing references, &c) but unless they are used conditionally, this is likely to cause crashes.

For example, you can see messages like these in logcat:

06-05 00:46:39.083: I/dalvikvm(6286): Could not find method org.apache.http.client.protocol.RequestDefaultHeaders.<init>, referenced from method org.apache.http.impl.client.HttpClientBuilder.build
06-05 00:46:39.083: W/dalvikvm(6286): VFY: unable to resolve direct method 22794: Lorg/apache/http/client/protocol/RequestDefaultHeaders;.<init> (Ljava/util/Collection;)V
06-05 00:46:40.434: D/dalvikvm(6286): DexOpt: couldn't find static field Lorg/apache/http/impl/client/DefaultHttpRequestRetryHandler;.INSTANCE
06-05 00:46:40.434: W/dalvikvm(6286): VFY: unable to resolve static field 8420 (INSTANCE) in Lorg/apache/http/impl/client/DefaultHttpRequestRetryHandler;

This particular message is because DefaultHttpRequestRetryHandler has a new INSTANCE static field in 4.3, which it does not have in Android. There are many more.

Going back to your original question, the only way to use a newer httpclient would be to rename all classes so these name conflicts don't occur. This is what httpclientandroidlib does.

Going even further back: DefaultHttpClient has indeed been deprecated in 4.3, but it is not deprecated in Android (unless you consider the trend towards using HttpUrlConnection a stealth form of deprecation -- in which case a newer HttpClient wouldn't be the preferred alternative either). Why exactly would you want/need to change it?

这篇关于发送一个HTTP-POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆