1 year ago
#73319
Ehsan Pagheh
how to stay connected to the server
I'm trying to stay connected with a url but I don't know how. I'm working on a quiz app so I need to refresh the question every time that user lose or win.
but it takes long time to connect to the url every time. so I just want to connected once and then just get new questions from that url. I'm using volley library to connect to a url.
And This is my code.
public static void getQuestionsData(Activity activity,
OnDoneLintenter onDoneListener) {
String url = "https://opentdb.com/api.php?amount=15&type=multiple";
RequestQueue queue = VolleySingleton.getInstance().getRequestQueue();
JsonObjectRequest jsonObjectRequest =
new JsonObjectRequest(ColumnStructure.GET, url, null,
jsonObject -> {
try {
JSONArray array
= jsonObject.getJSONArray(ColumnStructure.COL_RESULTS);
for (int i = 0; i < array.length(); i++) {
setColumnStructureFields(array, i);
}
onDoneListener.onDone();
} catch (JSONException e) {
Log.e("Log", "there is not result : " + e.getMessage());
}
},
volleyError -> {
Log.e("Log", "volley error : " + volleyError.getMessage());
});
queue.add(jsonObjectRequest);
}
I use this code at the beginning and every time user lose or win.So is there any way to stay connected with the url or at least get the json faster.
android
json
url
connection
android-volley
0 Answers
Your Answer