哪个客户端最好在Android中使用HttpURLConnection或HttpClient?

时间:2013-11-11 10:34:57

标签: php android json web-services

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

使用 HttpClient

调用网络服务时会发生什么
  • 在Froyo上,它完美地工作(在localhost和服务器上)。
  • 关于JellyBean工作但过了一段时间后就没有了
  • HttpClient在localhost上工作正常但在服务器上调用werbservice时出现问题。

使用 HttpURLConnection

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

当我调用webservice abc.php(在localhost和server上)时,从这里我重定向到另一个页面,如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;
}


HttpURLConnection代码

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上的错误更少。这是这些版本的最佳选择。

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

哪个客户最好?

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

3 个答案:

答案 0 :(得分:1)

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

使用httpclient或Androidhttpclient

祝你好运

答案 1 :(得分:1)

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

答案 2 :(得分:0)

显然是Google changed their mind

HttpClient和AndroidHttpClient只能用于FROYO及更早版本。

从GINGERBREAD开始,建议使用HttpUrlConnection

我按照他们的建议,经过很短的时间本地使用HUC(又名HttpUrlConnection)后,我写了一个小型库(〜12kb jar),它更容易使用。

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