0

I have a strange issue, calling my web services from Android device.

The first time I call a web service (REST) the ws call is executed in about 100 ms. While if I retry after few seconds, the call last about 20 seconds.

If I perfrom many subsequent calls to web service, using POSTMAN (for testing) I can't see any problem: all requests are processed in about 200 ms.

So I think the problem is in my Android client.

Below there is the code I use for calling WS:

public class DownloadTask extends AsyncTask<Void, Void, Void>{

    private Context context;

    public DownloadTask(Context context){
        this.context = context;
        contactList  = new ArrayList<Contact>();
    }

    @Override
    protected void onPreExecute() { 
        if (mCallbacks != null) {
            mCallbacks.onPreExecute();
          }
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {

        if(Utils.isOnline(context))
        {
            int resCode = -1;

            String wsURI = "myuri...";
            HttpGet request = new HttpGet(wsURI);
            request.setHeader("Content-Type", "application/json");

            try{
                // Send request to service
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpResponse response = httpClient.execute(request); 
                resCode = response.getStatusLine().getStatusCode();

                if(resCode == 200){
                    BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                    String line="";
                    StringBuffer returnFromServer = new StringBuffer();

                    while ((line=in.readLine())!=null) 
                    {
                        returnFromServer.append(line);
                    }

                    Gson gson = new GsonBuilder().create();                    
                    contactList = gson.fromJson(returnFromServer.toString(), new TypeToken<ArrayList<Contact>>(){}.getType());
                }
            }catch(Exception ex){
                ex.printStackTrace();
            }

        }

        return null;        
    }
}

EDIT: I have tried to run my app in debug, and I can see that the problem seems to be here:

HttpResponse response = httpClient.execute(request);

the first time it immediately executed, while the second (close to first) calls last about 10/20 seconds.

GVillani82
  • 17,196
  • 30
  • 105
  • 172
  • No, I'm sorry @Mobile They are privates – GVillani82 Jul 24 '14 at 11:08
  • Then try following urls..1) http://sarangasl.blogspot.in/2011/10/android-web-service-access-tutorial.html 2) http://ihofmann.wordpress.com/2013/01/23/android-sending-post-requests-with-parameters/ – Tushar Jul 24 '14 at 11:17
  • I may be wrong but i think the 1st time you get a response the next following lines are doing something that is taking time and in android same Asynctask wont execute again unless it finishes its execution the first time you execute it i.e its onPost() is done executing for the 1st call. Put a debuger and check if there is any gap in execution of 2nd call once onPost() is over for the first call – Deb Jul 24 '14 at 13:52

1 Answers1

0

I have tried some solutions proposed here, with no results. Finally I try to avoid the use of DefaultHttpClient or AndroidHttpClient, in favour of HttpURLConnection. This solves the problem.

Below the doInBackground method, that uses HttpURLConnection.

protected Void doInBackground(Void... params) {

    if(Utils.isOnline(context))
    {
        String wsURI = "myUriAddress...";
        InputStream inp=null;

        try{
            URL url = new URL(wsURI);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.addRequestProperty("Content-Type", "application/json");

            inp = new BufferedInputStream(urlConnection.getInputStream());

            if(urlConnection.getResponseCode() == 200){
                BufferedReader in = new BufferedReader(new InputStreamReader(inp, "UTF-8"));
                String line="";
                StringBuffer returnFromServer = new StringBuffer();

                while ((line=in.readLine())!=null) 
                {
                    returnFromServer.append(line);
                }

                in.close();
                inp.close();

                Log.i("doInBackground",""+returnFromServer.toString());

                Gson gson = new GsonBuilder().create();                    
                contactList = gson.fromJson(returnFromServer.toString(), new TypeToken<ArrayList<Contact>>(){}.getType());
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }

    }
    return null;            
}
Community
  • 1
  • 1
GVillani82
  • 17,196
  • 30
  • 105
  • 172