Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make fast my android app

In my android app I am using 5 API's in a single class, call them one by one in onCreate, but It makes my App slower, how to optimize my code to make it fast.

My code is like this.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

      API1();
      API2();
      API3();
      API4();
      API5();
   }

    // all the Apis are call in onCreate

  public void API1() {
            FetchData fetch = new FetchData(this);
            fetch.response = new FetchListener() {
        @Override
        public void fetchFinish(String output) {
            try {
                JSONObject jobjOUT = new JSONObject(output);

                JSONArray jsarray = jobjOUT.getJSONArray("details");

                for (int i = 0; i < jsarray.length(); i++) {
                    JSONObject obj = jsarray.getJSONObject(i);
                    String a = obj.getString("abc");
                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    try {
        fetch.execute("http://example.com/api/something.php?param=xyz");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

  }
  public void API2() {
     // same code here
  }
  public void API3() {
     // same code here
  }
  public void API4() {
     // same code here
  }
  public void API5() {
     // same code here
  }

 }

My question is can I call all the API at same time instead of calling them one by one. Please suggest me some tips to optimize my code. Thank You

like image 404
Anurag Shrivastava Avatar asked Sep 03 '25 06:09

Anurag Shrivastava


2 Answers

try to use thread pool executor refer this and this for learn and understand. by using thread pool executor you can run multiple tasks at the same time or we can say parallel.

private void startAsyncTaskInParallel(MyAsyncTask task) {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    else
        task.execute();
}
like image 161
Cruiser Raga S Avatar answered Sep 04 '25 20:09

Cruiser Raga S


Can you get around this by putting the APIx() calls into a runnable task?

class MyClass {

    public boolean ready = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        handler = new Handler();

        final Runnable r = new Runnable() {
            public void run() {
                API1();
                API2();
                API3();
                API4();
                ready = true;
            }
        };

        handler.postDelayed(r, 100);

    }
}
like image 37
Kenneth Hippolite Avatar answered Sep 04 '25 20:09

Kenneth Hippolite