# My Android app is really slow when getting simple data from PHP

I have a PHP API which echoes some data depending on the parameters in the URL:

When I try to access this API with Postman, I get the result within 5 seconds, however, using any Android device, I get the result at least 15 seconds later, under same network.

Now I suspect there might be a delay in the way I'm processing the data, but I'm also doubting that it's taking +10 seconds.

Here's the result from the API call:

{'Data1': Value1, 'Data2': 'Value2', 'Data3': 'Value3', 'Data4': 'Value4', 'Data5': 'Value5', 'Data6': 'Value6', 'Data7': 'Value7'}


Only the first value is integer, the rest are all string.

API Call code:

    apiTEST = view.findViewById(R.id.apiTEST);//Text View
Button getUserData = view.findViewById(R.id.getUserData);
new Handler().postDelayed(() -> {
getUserData.performClick();
}, 1000);//Automatically press the invisible button a second after loading the activity
getUserData.setOnClickListener(v -> {
StringRequest stringRequest = new StringRequest(apiURL,
response -> showJSONS(view, response),
error -> {
apiTEST.setText(error.getMessage());
getUserData.performClick(); //Sometimes an error occurs (null), so this line will just click the button automatically to make the request again
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
});


Data processing/showing:

   private void showJSONS(View view, String response) {
response = response.replace("'", "").replace("\"", "").replace("{", "").replace("}","");
String[] theValues = response.split(",");
apiTEST = view.findViewById(R.id.apiTEST);
for (String theValue : theValues) {
if (theValue.contains("Data1")){
String[] data1Line = theValue.split(":");
String Data1 = data1Line[1];
Data1 = Data1.replace("'", "");
data1_field.setText(Data1);
}
else if (theValue.contains("Data2")){
String[] data2Line = theValue.split(":");
String data2 = data2Line[1];
data2 = data2.replace("'", "");
data2_field.setText(data2);
data2ProgressBar[0] = data2;
}
else if (theValue.contains("Data3")){
String[] data3Line = theValue.split(":");
String data3 = data3Line[1];
data3 = data3.replace("\"", "");
data3_field.setText(data3);
data3ProgressBar[0] = data3;
}
else if (theValue.contains("Data4")){
String[] data4Line = theValue.split(":");
String data4Value = data4Line[1];
data4Value = data4Value.replace("\"", "");
data4Value = data4Value.replace("}]}", "");
data4_field.setText(data4Value);
data4ProgressBar[0] = data4Value;
}
... same for data5, data6 & data7
}
setButtonsVisibility(view, true);//Show buttons to interact with the values
}


Although I have set the buttons visibility before removing the "Please wait" line, what actually happens in the app is that the "Please wait" line gets removed, then about 7 seconds later the buttons appear!

Is there a better approach to retrieving the data from the API? Or to process the data more effectively?

About the auto clicking the button automatically when an error occurs: If I try to use Postman, I get the result 10/10, never failed or showed any error, but through the app, sometimes I just get "null" so I can only get the result after retrying few times. Thanks.

• Welcome to Stack Review, from your code you know you are receiving a json object , there is a specific reason why you chose to see it as a string and not a json object ? Nov 8 '21 at 9:30
• Thank you, the only reason is that this will be the first version for the app, and I found it a little bit difficult for me to deal with JSON in Java, so I decided to deal with it as String, then on the next version I'll have more time to learn how to use it as JSON.
– KiDo
Nov 8 '21 at 9:45
• You are welcome, but with string you are inspecting the message several times while with json the inspection process (deserialization) is done just one time , so it is more convenient and almost surely explains the slowness. Not related to the question,if you want to be sure users receive your reply you have to write @ before the username, there is also the username autocomplete option. Nov 8 '21 at 11:04
• Also retreiving 6 values of data should never take more then 5 seconds... I would expect something like maximum of 100ms Nov 8 '21 at 16:22

When approaching optimization problems (and it kinda is one as you want to make a call faster) its generally best to first make sure that you understand exactly what is happening so when in doubt - measure and debug.

Before doing any changes in the code I'd first set up debugger and probably some logs with timestamps (assuming you are testing on separate physical device - maybe emulator would be more useful?).

After you have tools that allow you to measure the code runtime - don't guess, locate the hotspot. When hotspot is known usually the solution becomes obvious (if its not then its a good moment to ask further questions).

Sorry if this is not the answer you were counting for but I believe that giving advice on code performance without specific measurement is exactly wrong - and amount of data you have provided is insufficient (mainly because there is too much difference between using postman from your development machine and app working on a android device - does it have enough ram? what exactly spans those 15 seconds - changes in the UI or it is time between http request and response? what does it mean they are on the same network? etc...).