0

I have noticed that my http requests tend to take alot of time compared apps communicating with same server. It makes my app feel sluggish and I was wondering if there is a better way of making these requests and updating the UI.

At the moment I use this method to make post requests

public String postRequest(List<NameValuePair> nameValuePairs, String method_name) {

    String result = "";
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.mysite.com/api/"+method_name);
    httppost.setHeader("Accept", "application/json");
    httppost.setHeader("Authorization", "Basic somestuff");

    try {
        // Add your data           
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        BufferedReader rd = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent()));
        result = rd.readLine();

        return result;
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }
    return null;
}

And in my UI thread (i.e my Fragment classes) I use this in an Async Task like this

class MakeRequest extends AsyncTask<Integer, Integer, String> {

    protected String doInBackground(Integer... counter) {           
        String result = "";
        String method_name = "";

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("id", value));
            nameValuePairs.add(new BasicNameValuePair("name", name));
            method_name = "petition/setPetition";

            result = fixr.postRequest(nameValuePairs, method_name);

            JSONObject jsonFile = new JSONObject(result);
            if(!jsonFile.has("error")){ 

                //Parse JSON using GSON

                return "success";
            }else{
                return jsonFile.getString("error");
            }
        } catch (Exception e) {
            e.printStackTrace();                
        }

        return null;
    }

    protected void onPostExecute(String jsonResult) {

        try {   

            if(jsonResult != null){                 
                //update UI
            }else{
                //Error message
            }               
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I'd like to optimize this so users can have a really smooth experience on my application. I'm open to using third party http libraries or is there also an argument against using AysncTasks and maybe the runOnUiThread() instead.

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
GrayStatic
  • 117
  • 3
  • 9

2 Answers2

0

Try Volley mate! I changed from AsyncTasks to Volley library and i am pretty pleased from the overall experience!

Volley Library

Pavlos
  • 2,183
  • 2
  • 20
  • 27
0

Volley Library is better, http, https etc.
https://developers.google.com/live/shows/474338138
very mini sample here:https://github.com/ogrebgr/android_volley_examples/blob/master/src/com/github/volley_examples/Act_SimpleRequest.java

TeeTracker
  • 7,064
  • 8
  • 40
  • 46
  • Volley seems to be the way to go and I'm about to try it but can't find a .jar file for this. Are there any available for some reason I can't install the GIT plugin on Eclipse so can't get the GIT clone – GrayStatic Aug 16 '13 at 08:45
  • I've a jar version, but it could be older, but I think you can use it to fill your common tasks. https://app.box.com/s/h7a8db233ydx0orlwf0y – TeeTracker Aug 16 '13 at 08:51