Reputation: 692
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
Upvotes: 4
Views: 1161
Reputation: 100
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();
}
Upvotes: 3
Reputation: 2701
Is your FetchData class a runnable of some kind? If it is an asynctask (looks like), the problem may lie in the listener's placement. If you call it in any other callback than doInBackground, the listener is going to be executed into the UI thread. Even assuming all your api calls are asynchronous, a bottleneck will be created. Another cause of performance loss is having too many tasks waiting to execute. A good way to avoid such a problem is by using a dedicated ThreadPoolExecutor when dealing with runnables, and using the executeOnExecutor(AsyncTask.ThreadPoolExecutor) method in the asynctasks.
If your calls are not asynchronous, then you must make them asynchronous. This means, executing all the non-ui related code in a separated, parallel thread, so the UI does not get blocked while you make all the calls. take a look at this
(I'm the dev), specifically, at the doInBackground method (line 994). As you can see, all the Rest call code, serialization code, and async callbacks are executed in this phase. The onPostExecute phase only executes the synchronous listeners (UI - related, basically) in order to reduce the workload of the UI thread. You can do exactly the same in your FetchData class.
Upvotes: 0
Reputation: 96
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);
}
}
Upvotes: 2