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

查看:30
本文介绍了如何从 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

安卓代码

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天全站免登陆