Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which client is best HttpURLConnection or HttpClient in android?

I am totally confused which one is better for calling php webservice using Restful Protocol.I did Used both API(HttpClient and HttpURLConnection) for calling webservice.

What happen when calling a webservice using HttpClient

  • On Froyo its working perfectly(on localhost and server).
  • On JellyBean Working but after some time is gone then not working
  • HttpClient is working fine on localhost but problem with calling werbservice on server.

What happen when calling a webservice using HttpURLConnection

  • On Froyo not working properly(on localhost)
  • the second point is same as second point of HttpClient
  • I can not redirect the php webservice page to another php page.

When i calling webservice abc.php(on localhost and server) and from here i redirect to another page like xyz.php. from xyz.php actually return data to the android project in json form but what happen when i using HttpClient is working fine but this redirection not work with HttpURLConnection.

HttpClient code

//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 code

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;

}
please anybody suggest me if any wrong with above code and tell me perfect way to call webservice. I tried lot but not achive my goal. If you do not understand above question so please ask me.

Apache HTTP client has fewer bugs on Eclair and Froyo. It is the best choice for these releases.

google recommend using HttpURLConnection for applications targeted at Gingerbread and higher Prior to Froyo, HttpURLConnection had some frustrating bugs. then what about Froyo. My apps run on froyo and higher versoin.

Which client is best?

Any help will be of great use.Thank you.

like image 430
nilesh wani Avatar asked Nov 02 '22 11:11

nilesh wani


1 Answers

HttpUrlConnection is not good as told by android google engineer in 2010 IO conferences. According to them this create adverse effect on network. ref: http://www.youtube.com/watch?v=xHXn3Kg2IQE

use httpclient or Androidhttpclient

Best of luck

like image 167
Rajiv yadav Avatar answered Nov 12 '22 20:11

Rajiv yadav