如何从HttpClient切换到HttpUrlConnection? [英] How to switch from HttpClient to HttpUrlConnection?

查看:121
本文介绍了如何从HttpClient切换到HttpUrlConnection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Android应用程序,我通过HttpClient将数据从Android应用程序发送到servlet。我使用HttpPost方法。

I am creating an Android application and I send data from Android application to servlet through HttpClient. I use HttpPost method.

我在Android开发者网站上读到Apache HttpClient库在Android Froyo 2.2中有一些错误,毕竟使用HttpUrlConnection而不是HttpPost是一个好习惯。所以我想将我的HttpPost代码转换为HttpUrlConnectio,但不知道如何。

I read in Android developer site that Apache HttpClient library has some bug in Android Froyo 2.2 and after all it's good practice to use HttpUrlConnection instead HttpPost. So I want to convert my HttpPost code to HttpUrlConnectio but don't know how.

我在这里发布我的Android代码以及servlet代码

I am posting my Android code as well as servlet code here

Android代码

private String postData(String valueIWantToSend[]) 
    {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        try 
        {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("param1",valueIWantToSend[0]));
            nameValuePairs.add(new BasicNameValuePair("param2", valueIWantToSend[1]));
            nameValuePairs.add(new BasicNameValuePair("param3", valueIWantToSend[2]));
            nameValuePairs.add(new BasicNameValuePair("param4", valueIWantToSend[3]));
            nameValuePairs.add(new BasicNameValuePair("param5", valueIWantToSend[4]));
            nameValuePairs.add(new BasicNameValuePair("param6", valueIWantToSend[5]));
            nameValuePairs.add(new BasicNameValuePair("param7", valueIWantToSend[6]));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            /* execute */
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity rp = response.getEntity();

            //origresponseText=readContent(response);
        }
        catch (ClientProtocolException e) 
        {
            // TODO Auto-generated catch block
        } 
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
        }
        return null;
    }

这是我的servlet代码

and here is my servlet code

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.setContentType("text/html");
    ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
    Enumeration paramNames = request.getParameterNames();
    String params[] = new String[7];
    int i=0;

    while(paramNames.hasMoreElements())
    {
        String paramName = (String) paramNames.nextElement();
        System.out.println(paramName);


        String[] paramValues = request.getParameterValues(paramName);
        params[i] = paramValues[0];

        System.out.println(params[i]);

        i++;
    }

}


推荐答案

你绝对应该使用 HttpUrlConnection


对于Gingerbread等等, HttpURLConnection是最好的选择......新应用程序应该使用HttpURLConnection ......

For Gingerbread and better, HttpURLConnection is the best choice... New applications should use HttpURLConnection...

- Google(大约2011年)

然而,没有简单的方法来切换。 API完全不同。您将不得不重写您的网络代码。 文档中提供了有关如何提交GET和POST的完美示例请求以及SDK示例应用程序。

However, there is no easy way just to "switch". The APIs are totally different. You are going to have to rewrite your networking code. There are perfect examples in the documentation on how to submit a GET and POST requests as well as in the SDK sample apps.

这篇关于如何从HttpClient切换到HttpUrlConnection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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