【问题标题】:which client is best HttpURLConnection or HttpClient in android?哪个客户端是 android 中最好的 HttpURLConnection 或 HttpClient?
【发布时间】:2013-11-23 03:01:35
【问题描述】:

我很困惑哪一个更适合使用 Restful Protocol.I 调用 php webservice 确实使用了 API(HttpClient 和 HttpURLConnection) 来调用 webservice。

使用 HttpClient 调用 Web 服务时会发生什么

  • 在 Froyo 上完美运行(在本地主机和服务器上)。
  • 在 JellyBean 上工作,但一段时间后就无法工作
  • HttpClient 在 localhost 上运行良好,但在服务器上调用 werbservice 时出现问题。

使用 HttpURLConnection

调用网络服务时会发生什么
  • 在 Froyo 上无法正常工作(在 localhost 上)
  • 第二点与HttpClient的第二点相同
  • 我无法将 php webservice 页面重定向到另一个 php 页面。

当我调用 webservice abc.php(在本地主机和服务器上)并从这里我重定向到另一个页面,如 xyz.php。从 xyz.php 实际上以 json 形式将数据返回到 android 项目,但是当我使用 HttpClient 工作正常但此重定向不适用于 HttpURLConnection 时会发生什么。

HttpClient 代码

//calling the webservice using AsyncTask
public String makeHttpReqToSrvr(String url,String requestType,List<NameValuePair> params) {

    HttpEntity httpEntity=null;
    HttpResponse httpResp = null;

    try {

        if (requestType == "GET") {

            //connection time out
            HttpParams httpParameters = new BasicHttpParams();
            int timeout1 = 1000*8;
                int timeout2 = 1000*8;
            HttpConnectionParams.setConnectionTimeout(httpParameters, timeout1);
            HttpConnectionParams.setSoTimeout(httpParameters, timeout2);

            HttpClient httpClient = new DefaultHttpClient(httpParameters);
            String paramString =URLEncodedUtils.format(params, "utf-8");
            HttpGet httpGet = new HttpGet(url+"?"+paramString);
            httpResp = httpClient.execute(httpGet);
            httpEntity = httpResp.getEntity();

        }
        if (requestType == "POST") {
            // connection time out
            HttpParams httpParameters = new BasicHttpParams();
            int timeout1 = 1000*8;
                int timeout2 = 1000*8;
            HttpConnectionParams.setConnectionTimeout(httpParameters, timeout1);
            HttpConnectionParams.setSoTimeout(httpParameters, timeout2);

            HttpClient  httpClient = new DefaultHttpClient(httpParameters);
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            httpResp = httpClient.execute(httpPost);
            httpEntity = httpResp.getEntity();

        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } 
    // -- End and Start read the data using bufferreader
    try {
        if(httpEntity != null)
            json = EntityUtils.toString(httpEntity);
            json = strBuilder.toString();
                Log.v("JSON", "data"+json);

    } catch (Exception e) {
        e.printStackTrace();
    } 
    return json;
}


HttpURL连接代码

public String makeHttpReqToSrvr(String url,String requestType,List<NameValuePair> params) {

    try {
        URL urlPath= null;
        String paramString = URLEncodedUtils.format(params, "utf-8");

        if (requestType == "GET") {
            urlPath = new URL(url+"?"+paramString);
        }
        if (requestType == "POST") {
            urlPath = new URL(url);
        }

        conn = (HttpURLConnection) urlPath.openConnection();
        conn.setReadTimeout(10000); 
            conn.setConnectTimeout(15000); 
        conn.setDoInput(true);
        conn.setDoOutput(true);

        if (requestType == "GET") {
            conn.setRequestMethod("GET");
        }
        if (requestType == "POST") {
            conn.setRequestMethod("POST");

        }
        //send the data to the server using post
        dos = new DataOutputStream(conn.getOutputStream());
        dos.writeBytes(paramString);
        dos.flush();

        // Starts the query
        conn.connect();
        int response = conn.getResponseCode();

        is = conn.getInputStream();

        // Convert the InputStream into a string
        json = readIt(is);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (is != null) {
                is.close();
                dos.close();
                conn.disconnect();
            }
        } catch (IOException e) {

            e.printStackTrace();
        } 
    }
    return json;

}
如果上面的代码有任何问题,请任何人建议我,并告诉我完美的调用方式 网络服务。我尝试了很多,但没有达到我的目标。如果您不明白上述问题,请 问我。

Apache HTTP 客户端在 Eclair 和 Froyo 上的错误更少。这是这些版本的最佳选择。

google 建议针对 Gingerbread 及更高版本的应用程序使用 HttpURLConnection 在 Froyo 之前,HttpURLConnection 有一些令人沮丧的错误。那么弗罗约呢?我的应用 在 froyo 和更高版本上运行。

哪个客户端最好?

任何帮助都会很有用。谢谢。

【问题讨论】:

  • 你什么时候调用这些函数?!如果它不在asyncTask 中,那么这就是你的问题。因为在早期版本中,您可以在没有线程的情况下使用 httpClient,但在 4.0 中,您需要使用 asynctask
  • 我在整个应用程序中都使用了 AsyncTask。只是我正在从活动的 doInBackground 方法调用 makeHttpReqToSrvr 方法。我知道 AsyncTask。我使用两个客户端类进行检查

标签: php android json web-services


【解决方案1】:

你可以试试 Square 的OKHTTP。查看github页面here

【讨论】:

  • OKHTTP 是否适用于所有版本的 android?你用OKHHTTP成功了吗?
  • OKHTTP 是开源的吗?
  • 是的,它的开源 (Apache 2.0) - 请参阅 github 页面了解更多信息。我已经在一些生产项目中成功使用了它——与HttpUrlConnection 相比,我更喜欢使用它,因为你不会像它的外部库那样得到任何(或同样多)平台特​​定的错误。它还实现了HttpUrlConnection 接口
  • 谢谢多莉。我将在我的应用中使用。
【解决方案2】:

显然是Google changed their mind

HttpClient 和 AndroidHttpClient 只能用于 FROYO 及以上版本。

从 GINGERBREAD 开始,建议使用HttpUrlConnection

我听从了他们的建议,在很短的时间内原生使用 HUC(又名 HttpUrlConnection)后,我编写了一个更易于使用的小型库(~12kb jar)。

该库名为 DavidWebb,您可以在其中找到许多指向其他 HTTP/REST 库的链接,特别是针对 Android 的。

【讨论】:

    【解决方案3】:

    HttpUrlConnection 并不像 android google 工程师在 2010 年 IO 会议上所说的那样好。 据他们说,这会对网络产生不利影响。 参考:http://www.youtube.com/watch?v=xHXn3Kg2IQE

    使用 httpclient 或 Androidhttpclient

    祝你好运

    【讨论】:

    • 但是在这个 url 上谷歌推荐使用 HttpURLConnection。但是 HttpClient 的数量有问题。
    • 请去扔 Virgil Dobjanschi 先生的讲座。在此他推荐 apache httpclient 而不是 java httpurlconnection。我大部分时间都使用httpclient。
    • 对不起,这是 google 推荐的网址 android-developers.blogspot.in/2011/09/…
    • 你需要做什么?文件上传下载还是简单的数据共享?
    • 我觉得你应该选择httpclient。
    猜你喜欢
    • 1970-01-01
    • 2011-05-29
    • 2013-07-21
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    相关资源
    最近更新 更多