Android项目使用httpclient --> http.client (apache),post/get 方法
我正在为 android 项目执行 Get 和 Post 方法,我需要将 HttpClient 3.x“翻译”为 HttpClient 4.x(由 android 使用)。 我的问题是我不确定我做了什么,并且我没有找到某些方法的“翻译”...
这是我所做的 HttpClient 3.x 和 (-->) HttpClient 4 .x“翻译”如果我找到了它(仅限向我提出问题的各方):
HttpState state = new HttpState (); --> ?
HttpMethod method = null; --> HttpUriRequest httpUri = null;
method.abort(); --> httpUri.abort(); //httpUri is a HttpUriRequest
method.releaseConnection(); --> conn.disconnect(); //conn is a HttpURLConnection
state.clearCookies(); --> cookieStore.clear(); //cookieStore is a BasicCookieStore
HttpClient client = new HttpClient(); --> DefaultHttpClient client = new DefaultHttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(SOCKET_TIMEOUT) --> HttpConnectionParams.setConnectionTimeout(param, SOCKET_TIMEOUT);
client.setState(state); --> ?
client.getParams().setCookiePolicy(CookiePolicy.RFC_2109); --> HttpClientParams.setCookiePolicy(param, CookiePolicy.RFC_2109);
PostMethod post = (PostMethod) method; --> ?
post.setRequestHeader(...,...); --> conn.setRequestProperty(...,...);
post.setFollowRedirects(false); --> conn.setFollowRedirects(false);
RequestEntity tmp = null; --> ?
tmp = new StringRequestEntity(...,...,...); --> ?
int statusCode = client.executeMethod(post); --> ?
String ret = method.getResponsBodyAsString(); --> ?
Header locationHeader = method.getResponseHeader(...); --> ?
ret = getPage(...,...); --> ?
我不知道这是否正确。 这导致了问题,因为包的命名不相似,一些方法也是如此。 我只需要文档(我还没有找到)和很少的帮助。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里是 HttpClient 4 文档,这就是 Android 正在使用的( 4,而不是 3,从 1.0->2.x 开始)。 这些文档很难找到(感谢 Apache;)),因为 HttpClient 现在是 HttpComponents 的一部分(如果您只是寻找 HttpClient,您通常会得到 3.x 的东西)。
另外,如果您执行任意数量的请求,您不希望一遍又一遍地创建客户端。 相反,正如 HttpClient 说明教程,创建客户一次并保留它。 从那里使用 ThreadSafeConnectionManager。
我使用辅助类,例如 HttpHelper (这仍然是一个移动目标 - 我计划在某个时候将其移动到自己的 Android util 项目,并支持二进制数据,但还没有实现),帮忙解决这个问题。 帮助器类创建客户端,并具有用于 get/post/等的便捷包装方法。 无论您在 Activity 中使用此类,都应该创建一个内部内部 AsyncTask (这样你在创建时就不会阻塞 UI 线程)请求),例如:
Here are the HttpClient 4 docs, that is what Android is using (4, not 3, as of 1.0->2.x). The docs are hard to find (thanks Apache ;)) because HttpClient is now part of HttpComponents (and if you just look for HttpClient you will normally end up at the 3.x stuff).
Also, if you do any number of requests you do not want to create the client over and over again. Rather, as the tutorials for HttpClient note, create the client once and keep it around. From there use the ThreadSafeConnectionManager.
I use a helper class, for example something like HttpHelper (which is still a moving target - I plan to move this to its own Android util project at some point, and support binary data, haven't gotten there yet), to help with this. The helper class creates the client, and has convenience wrapper methods for get/post/etc. Anywhere you USE this class from an Activity, you should create an internal inner AsyncTask (so that you do not block the UI Thread while making the request), for example:
回答我的问题的最简单方法是向您展示我制作的课程:
我使用 本教程来执行我的帖子方法和那些示例
The easiest way to answer my question is to show you the class that I made :
I used this tutorial to do my post method and thoses examples
好吧,您可以在此处找到有关该版本
HTTPClient
的文档; 浏览它们提供的示例场景特别有用。不幸的是,我不知道
HTTPClient
的版本 3,所以我无法给出直接的等价物; 我建议您采取您正在尝试做的事情并查看他们的示例场景。Well, you can find documentation on that version of
HTTPClient
here; it's especially useful to go through the example scenarios they present.I unfortunately don't know version 3 of
HTTPClient
so I can't give direct equivalences; I suggest you take what you're trying to do and look through their example scenarios.