为了账号安全,请及时绑定邮箱和手机立即绑定

使用httpclient-> http.client(apache)的Android项目

使用httpclient-> http.client(apache)的Android项目

茅侃侃 2019-11-27 14:02:52
我正在为一个Android项目做一个Get and 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 HttpUriRequestmethod.releaseConnection(); --> conn.disconnect(); //conn is a HttpURLConnectionstate.clearCookies(); --> cookieStore.clear(); //cookieStore is a BasicCookieStoreHttpClient 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(...,...); --> ?我不知道那是不是正确的。这引起了问题,因为这些软件包的名称和某些方法的名称都不相同。我只需要文档(我还没有找到)和很少的帮助。
查看完整描述

3 回答

?
喵喔喔

TA贡献1735条经验 获得超5个赞

回答我的问题的最简单方法是向您展示我的课程:


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;

                }

}


查看完整回答
反对 回复 2019-11-27
  • 3 回答
  • 0 关注
  • 519 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信