I am trying to make the networking part of my app more stable, more optimal, since I recieved some issues, stating it takes too much time for the queries to run. Before I share the pieces of my code, here are some infos:
- it is an android app, so a lot of the users may use it on weak mobile data connection
- currently, there are 50k active users, so the server may have high load during peak hours
- a third party develops the server, so I can make improvements on the client side only
Most likely, a lot of the issues are coming from weak mobile data connection in the peak hours, but still, if I can do anything to improve the connection, then I would do it.
I am managing the queries in an AsyncTask. In onPreExecute()
I am doing some initialization only, and showing a Dialog, nothing really happens here. I am doing every important piece in doInBackGround(Void... arg)
, here is the exact code:
try
{
HttpClient hc = new DefaultHttpClient();
hc.getParams().setParameter("http.protocol.content-charset", "UTF-8");
HttpParams hp = hc.getParams();
HttpConnectionParams.setConnectionTimeout(hp, Stat.TIMEOUT);
HttpConnectionParams.setSoTimeout(hp, Stat.TIMEOUT);
HttpProtocolParams.setUserAgent(hp, ActMain.userAgent);
httpGet=new HttpGet(item.buildQuery());
HttpResponse resp = hc.execute(httpGet);
Stat.result=_result=EntityUtils.toString(resp.getEntity(), HTTP.UTF_8);
}
catch(MalformedURLException e){}
catch(IOException e){}
return null;
After this, in onPostExecute(Void param)
I am processing the result, setting up the needed variables, then launching the Activity
to show the results to the users, nothing really important part here.
The weird error which happens quite a few times is: instead of a pure JSON formatted result, sometimes the server responds with an almost plaintext formatted 404 error message. I am guessing it is server related, so I cannot do anything to solve it, maybe I could relaunch the query 2-3 times, to make sure the user would recieve results.
My real question is: can I improve the pasted code snippet, so it would be a bit more stable/optimal on the client side? Thanks in advance!
EDIT: I have managed to trace the issue back to the server, my app has a bigger active user count than I thought, they simply overload the server at times, so everything is fine on my end of the things.