1. Download our Official Android App: Forums for Android!

Apps my application, why so SLOW?

Discussion in 'Android Development' started by tneva82, May 4, 2012.

  1. tneva82

    tneva82 Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    31
    Joined:
    Jun 20, 2011

    Jun 20, 2011
    31
    0
    15
    Can't figure why my application has such a slow response times with the button. It's not even particularly complex program and windows phone version seems to be working lot smoother.

    Here's what happens when button is pressed.

    try
    {
    HttpClient hc = new DefaultHttpClient();
    HttpPost post = new HttpPost("https://mywebpage.com/file.php");

    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    pairs.add(new BasicNameValuePair("u", username));
    pairs.add(new BasicNameValuePair("p", passwd));
    post.setEntity(new UrlEncodedFormEntity(pairs));

    HttpResponse rp = hc.execute(post);

    if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
    {
    str = EntityUtils.toString(rp.getEntity());
    }
    }catch(IOException e){
    e.printStackTrace();
    }

    Okay I likely need to add crypting but for testing purposes so far plain text works fine.

    However this takes very long, longer than it should really seeing response is just shorthish line. Browser call gets that return back almost immediately and seeing my phone can stream video without issues I don't think it's network speed issue either.

    Also the button doesn't change colour to show it was clicked consistently. Sometimes it doesn't do that so user ends up pressing again. Not that bad here but on next intent there's another button so if you do that you could end up negating what you did(it's basically on/off switch).

    Is my method of calling web page plain bad? I don't think it's asynchronous so it blocks the program. Is that the issue? If so how to call it so it doesn't block the system? Or is it just something I have to live with?
     

    Advertisement

  2. wubbzy

    wubbzy Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    109
    Joined:
    Dec 19, 2011

    Dec 19, 2011
    109
    32
    63
    *nix kernel's by day
    Silicon Valley
    Your UI thread "maybe" getting blocked i.e. you're calling http in UI thread, sooner or later you'll get into ANR due to low signal etc.

    Use AsyncTask to execute http and in postexecute you can update your button color/text etc. You can disable your button in preexecute so user presses won't have any impact, don't forget to re-enable it in postexexute again. Alternatively you can use Toast or Dialog to display status which should block until when operation is in progress and unblock in postexecute.
     
  3. tneva82

    tneva82 Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    31
    Joined:
    Jun 20, 2011

    Jun 20, 2011
    31
    0
    15
    Hmm. Not sure how the ASyncTask is used. So I put the http request on the doInBackground? How do I notify the intent that now it's done? And how to give the intent that called it the resulting string? onPostExecute has return parameter type void so no luck there.

    Do I need to attach event listener(I presume that's possible in the android?)? On windows phone I got it working by attaching event listener where I did the job with the string when the process was done.

    Is it possible to call functions of the intent that called it somehow?

    Sorry fairly new at the android programming. And very rusty in java as well since before this program there was like 4 year gap where I didn't need java.

    edit got it working by:

    str= myServer.execute(parameters...).get();

    but unless I'm misreading the documents .get function again waits until it's done and then proceeds, blocking system again?

    Umm. how to get the results not-blockingly then...Humhum.

    (but at least this cleaned up my code a bit splitting it to pieces. Next up I think I'll work the php files to split the parameters from one line. I split the AsyncTask into separate class that I'll call from 2 places for different php's but currently the parameters(count and names) are separate. Guess I can have the doInBackground have if's and construct the pairs that way but one parameter which contains multiple in separations would be more reusable code I think. No need to change that file based on which php file I'm calling at.
     
  4. wubbzy

    wubbzy Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    109
    Joined:
    Dec 19, 2011

    Dec 19, 2011
    109
    32
    63
    *nix kernel's by day
    Silicon Valley
    Async task does run stuff in background and hence the name. It is actually a helper class that is laid on top-off Thread framework (broadly speaking). Here is an example
    Painless Threading | Android Developers

    In this case event listener or Handler is not needed. There are event handlers, guess what called Handler :D. Handler | Android Developers

    You can execute intents from onPostExecute.

    No worries, we were all there at one time :D

    You'll need to call HTTP stuff in doInBackground. When doInBackground completes it will call onPostExecute (on main UI thread). This call is asynchronous i.e. it could return immediately or return 60 seconds down the line depending on data/traffic/speed etc. due to nature of processing. doInBackground is actually a separate background thread. In this case you don't need to do get and can continue to do other stuff if there is something for your app code to do i.e. housekeeping etc.

    When onPostExecute is called you can for example re-enable your button so it becomes clickable again or show the results of HTTP request etc.
     
  5. tneva82

    tneva82 Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    31
    Joined:
    Jun 20, 2011

    Jun 20, 2011
    31
    0
    15
    Okay. Got the button to not respond but no help with the speed issue. How on earth such a short line take so slow to load. On browser it's virtually instant and my phone can easily stream video from net so shouldn't be connection speed issue either.
     
  6. wubbzy

    wubbzy Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    109
    Joined:
    Dec 19, 2011

    Dec 19, 2011
    109
    32
    63
    *nix kernel's by day
    Silicon Valley
    It could be anything between your phone running too many apps (HTTP's) to your provider's slow network to your back-end systems upload speed. Hence comparison is incorrect in my opinion.

    When you say "browser", where is that browser running? On your phone or on your computer.

    Maybe you can post your reworked code ?

    Network issues aren't typical, and they can be pain to troubleshoot.
     
  7. tneva82

    tneva82 Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    31
    Joined:
    Jun 20, 2011

    Jun 20, 2011
    31
    0
    15
    I seriously doubt it's slow network because it would be certain to show up when watching HD video via phone's network speed but it doesn't...

    Also as I think I said windows phone version works smooth as ever.

    Very weird.

    ConnectToServer myServer=new ConnectToServer();

    myServer.execute(parameters);

    private class ConnectToServer extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
    super.onPreExecute();


    }

    @Override
    protected String doInBackground(String... aurl) {
    String url=aurl[1];
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    if(aurl[0].compareTo("0")==0) { // okay this is oddity. Couldn't figure better way to handle the fact some pages I call have different name and amount of parameters to give)
    pairs.add(new BasicNameValuePair("u", aurl[2]));
    pairs.add(new BasicNameValuePair("p", aurl[3]));
    } else if(aurl[0].compareTo("1")==0) {
    pairs.add(new BasicNameValuePair("u", aurl[2]));
    pairs.add(new BasicNameValuePair("p", aurl[3]));
    pairs.add(new BasicNameValuePair("imei", aurl[4]));
    } else {
    return null;
    }


    String str="";

    try {
    HttpClient hc = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);



    post.setEntity(new UrlEncodedFormEntity(pairs));

    HttpResponse rp = hc.execute(post);
    if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
    {
    str = EntityUtils.toString(rp.getEntity());
    }
    } catch (Exception e) {

    }

    return str;

    }

    protected void onProgressUpdate(String... progress) {

    }

    @Override
    protected void onPostExecute(String str) {
    TextView myTextView=(TextView)findViewById(R.id.myText);
    if (here be checks on whether server responded one of error messages and show user what happened if it did)
    } else {

    Bundle bundle = new Bundle();
    bundle.putString("param1", str);
    bundle.putString("username", username);
    bundle.putString("passwd", passwd);

    CheckBox checkBox = (CheckBox) findViewById(R.id.storeuserdata);
    if (checkBox.isChecked()) {

    String outputData=username+":"+passwd+":";
    writeFileToInternalStorage(outputData);
    } else {

    String outputData="no_username_storage";
    writeFileToInternalStorage(outputData);

    }
    Button myBtn=(Button)findViewById(R.id.logOnButton);
    myBtn.setClickable(true);
    Intent myIntent = new Intent(myBtn.getContext(), MainScreenActivity.class);
    myIntent.putExtras(bundle);
    startActivityForResult(myIntent, 0);
    }
    }

    }

    Response text(when succesfull) will be akin to:

    name:number/data blocks equal to amount of number. Each contains number(about 10 numbers long), name and 0 or 1 in the end. So maybe something like 10+(30*n) chars long. For connection that can handle real time HD video stream this ought not to be too hard ;)
     
  8. wubbzy

    wubbzy Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    109
    Joined:
    Dec 19, 2011

    Dec 19, 2011
    109
    32
    63
    *nix kernel's by day
    Silicon Valley
    Please do understand this network is one beast that has too many components that can go wrong. Without knowing much it would he hard to say whats wrong and where. But,

    Do you watch HD videos from the same server that is running your PHP? Did you measure latency from your phone to your PHP server? Did you enable compression on PHP web-server? Did you traceroute number of hops? When you say longer long what is the time between request dispatch and response receipt? Does the PHP server running e-accelerator? Did you run the PHP on your phones browser? You earlier said it loads instantaneously on browser, where is that browser running, on the same computer as PHP? Can you connect to WiFi and then measure the speed/time?

    Assuming your signal strength is solid.

    Now coming to HD video...video thread runs at a very high system priority, probably around -12 to -17. The backend servers that serve the video have been highly optimized and to serve millions of requests pretty quick, the video client is assisted by hardware directly to realize seamless playback.

    I understand that there is a problem, however as developers we must look at lot of things that can cause app to behave erratically. Sometimes those problems are obvious other times they're not.
     

Share This Page

Loading...