-1

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.

hundeva
  • 1,028
  • 15
  • 30
  • 2
    You need to measure where the cause of the delays are and solve that problem. Trying to only solve the problems which are obvious to you are likely to be either a) a waste of time b) make matters worse. For example, making the client faster if the server is being overloaded may make the problem worse. – Peter Lawrey May 05 '13 at 15:16
  • I agree, that is why I wonder if the snippet I gave provides a good networking, or is there a weak point in it. If everything seems fine on my side, than I will try to talk with the server developer. – hundeva May 05 '13 at 15:21
  • The only worry I would have is ignoring exceptions. This won't affect performance unless you are getting lots of errors and you don't know it. – Peter Lawrey May 05 '13 at 16:05
  • Do you call it a lot per device? – flup May 06 '13 at 23:00
  • It is a train schedule checker app, so each session may have two or three query, depending on the user behaviour. 1 query = 1 route, that is the rule. – hundeva May 07 '13 at 07:18

1 Answers1

1

Either your client is slow or it is fast. It should be consistently slow or fast. If you are seeing slow behaviour sometimes and faster behaviour with the same mobile device, the same software, then this is not the cause.

The most likely cause of variation is the network (as you have said), and the load on the server.

Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075