I have a PHP API which echoes some data depending on the parameters in the URL:
URL = "MyWebsite.com/MyAPI.php/username/password"
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);
String apiURL = "https://MyWebsite.com/MyAPI.php/username/password";
new Handler().postDelayed(() -> {
getUserData.performClick();
apiTEST.setText(getResources().getString(R.string.please_wait));//Show please wait line
}, 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());
requestQueue.add(stringRequest);
});
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
apiTest.setText("");//Remove the "Please Wait" line
}
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.