1

My android application is calling authenticated webservice API to download and sync records from server based on type of data.

For example:

Application calls the API in loop for different content types(Commerce, Science, Arts).

Now for each content type, application maintains last sync date so that it could sync data after that date only, for last one month.

The API call looks like:

private void loadData(){
    String apiUrl = "";
    String[] classArray = { "Commerce", "Science", "Arts" };
    try{
        for(int classIndex = 0; classIndex < classArray.length; classIndex++){
            apiUrl = "http://www.myserver.com/datatype?class="+classArray[classIndex]+"&syncDate="+lastSyncDate;

            String responseStr = getSyncData(apiUrl);

            // Code to parse the JSON data and store it in SqliteDB.
        }

    }catch (Exception e) {
        e.printStackTrace();
    }

}

private String getSyncData(String webservice){
    String line = "", jsonString = "";

    try{
        URL url = new URL(webservice);  

        HttpURLConnection conn = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY); //using proxy may increase latency
        conn.setInstanceFollowRedirects(false);

        String userName = "abc@myserver.com", password = "abc123";
        String base64EncodedCredentials = Base64.encodeToString((userName
                + ":" + password).getBytes(), Base64.URL_SAFE
                | Base64.NO_WRAP);
        conn.setRequestProperty("Authorization", "basic "+ base64EncodedCredentials);

        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        while ((line = rd.readLine()) != null) {
            // Process line...

            return line;

        }

            rd.close();

        }

    }catch (Exception e) {
        e.printStackTrace();
    }
    return jsonString;

}

This method getSyncData() returns JSON response, which is parsed and stored in SqliteDb.

This code is working fine. But there is a slight performance issue when there are more content types in the classArray and each class have large data-set.

My question is :

To improve the overall performance of this process, can I open the connection to www.myserver.com and pass the parameters with API call in loop to stop creating connection again and again for each content type.

Here I am using HttpURLConnection for API calls but can use any other technique in java.

Main purpose is to make the connection persistent so that application should not create it again and again for each call because for every call application creates a separate connection which is consuming more time.

Ravi Sharma
  • 873
  • 7
  • 24

1 Answers1

0

I've done a similar processing before with webcall -> parse JSON -> store DB -> show/update views

and with a lot of testing and debugging I found out that what actually was slowing down the process was the store DB part, nothing to do with the webcall or JSON parsing.

I solved the situation by changing it to:

webcall -> parse JSON -> fire new thread to store DB -> show/update views

and with that simple change my results started appearing in a matter of 1sec (instead of the previous 5 to 6 seconds).

Hope it helps.

edit:

regarding the connection itself, you could use web-sockets (which are persistent, but not very well implemented in Android, you'll have to do quite an amount of manual parsing), so I suggest testing the DB thing first.

Budius
  • 39,391
  • 16
  • 102
  • 144
  • Thanks for the quick reply. Yes, of course DB is also a concern. Optimizing DB operations too but still I need a persistent connection to get highest performance. – Ravi Sharma Apr 11 '13 at 10:37
  • I understand your concern, but from my experience try simply firing the DB operation in a new thread and check the difference in performance, it's pretty big! – Budius Apr 11 '13 at 10:42
  • I can't fire a separate DB thread because after every DB insert, application is maintaining a total row count for that class so that in next API call it could skip downloaded data and to show the number of rows inserted in db after API call. – Ravi Sharma Apr 12 '13 at 10:17