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.