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

nil*_*ani 5 php android json web-services

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

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

  • 在Froyo上它完美地工作(在localhost和服务器上).
  • 在JellyBean工作但是过了一段时间后就没有了
  • HttpClient在localhost上工作正常,但在服务器上调用werbservice有问题.

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

  • On Froyo无法正常工作(在localhost上)
  • 第二点与HttpClient的第二点相同
  • 我无法将php webservice页面重定向到另一个php页面.

当我调用webservice abc.php(在localhost和服务器上)并从这里重定向到另一个页面,如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;
}
Run Code Online (Sandbox Code Playgroud)


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;
Run Code Online (Sandbox Code Playgroud)

}
请有人建议我如果有错上面的代码,并告诉我打电话给web服务完美的方式.我尝试了很多,但没有实现我的目标.如果您不理解上述问题,请咨询我.

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

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

哪个客户最好?

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

Raj*_*dav 1

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

使用httpclient或Androidhttpclient

祝你好运