anurag shrivastava anurag shrivastava - 4 months ago 17
JSON Question

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

Answer

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);

    }
}
Comments