使用HttpClient的Andr​​oid项目 - > http.client(阿帕奇),交/ get方法 [英] Android project using httpclient --> http.client (apache), post/get method

查看:107
本文介绍了使用HttpClient的Andr​​oid项目 - > http.client(阿帕奇),交/ get方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个获取和Post方法的Andr​​oid项目,我需要翻译的HttpClient 3.x中以HttpClient的4.x版(搭载Android使用)。 我的问题是我不知道什么我做了,我不找的一些方法的翻译......

I'm doing a Get and Post method for an android project and I need to "translate" HttpClient 3.x to HttpClient 4.x (using by android). My problem is that I'm not sure of what I have done and I don't find the "translation" of some methods...

这是HttpClient的3.X我所做的和( - >)HttpClient的4.x的翻译如果我发现它(只问我问题,谁当事人):

This is the HttpClient 3.x I have done and (-->) the HttpClient 4.x "translation" if I have found it (Only parties who ask me problems) :

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(...,...); --> ?

我不知道这是否是正确的。 这已经引起了问题,因为产品的不命名类似地,有些方法太。 我只是需要的文件(我还没有发现),并帮助不大。

I don't know if that is correct. This has caused problems because the packages are not named similarly, and some methods too. I just need documentation (I haven't found) and little help.

感谢您在您的帮助。 迈克尔

Thank you in advance for your help. Michaël

推荐答案

回答我的问题,最简单的办法就是告诉你,我做了类:

The easiest way to answer my question is to show you the class that I made :


public class HTTPHelp{

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    private boolean abort;
    private String ret;

    HttpResponse response = null;
    HttpPost httpPost = null;

    public HTTPHelp(){

    }

    public void clearCookies() {

    	httpClient.getCookieStore().clear();

    }

    public void abort() {

    	try {
    		if(httpClient!=null){
    			System.out.println("Abort.");
    			httpPost.abort();
    			abort = true;
    		}
    	} catch (Exception e) {
    		System.out.println("HTTPHelp : Abort Exception : "+e);
    	}
    }

    public String postPage(String url, String data, boolean returnAddr) {

    	ret = null;

    	httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);

    	httpPost = new HttpPost(url);
    	response = null;

    	StringEntity tmp = null;		

    	httpPost.setHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux " +
    		"i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)");
    	httpPost.setHeader("Accept", "text/html,application/xml," +
    		"application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
    	httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

    	try {
    		tmp = new StringEntity(data,"UTF-8");
    	} catch (UnsupportedEncodingException e) {
    		System.out.println("HTTPHelp : UnsupportedEncodingException : "+e);
    	}

    	httpPost.setEntity(tmp);

    	try {
    		response = httpClient.execute(httpPost,localContext);
    	} catch (ClientProtocolException e) {
    		System.out.println("HTTPHelp : ClientProtocolException : "+e);
    	} catch (IOException e) {
    		System.out.println("HTTPHelp : IOException : "+e);
    	} 
                ret = response.getStatusLine().toString();

                return ret;
                }
}

我用这个教程做我的帖子方法:<一href="http://wiki.apache.org/HttpComponents/HttpClientTutorial">http://wiki.apache.org/HttpComponents/HttpClientTutorial并放入系统的例子:<一href="http://hc.apache.org/httpcomponents-client/examples.html">http://hc.apache.org/httpcomponents-client/examples.html (感谢丹尼尔) 感谢您的帮助。

I used this tutorial to do my post method : http://wiki.apache.org/HttpComponents/HttpClientTutorial and thoses examples : http://hc.apache.org/httpcomponents-client/examples.html (Thanks Daniel) Thank your for your help.

这篇关于使用HttpClient的Andr​​oid项目 - &GT; http.client(阿帕奇),交/ get方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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