I have a StringRequest reading from an API, but it only sometimes works:
System.out.println("Getting data");
String myUrl = "https://api.quotable.io/random";
StringRequest myRequest = new StringRequest(Request.Method.GET, myUrl,
response -> {
try{
// Create a JSON object containing information from the API.
JSONObject object = new JSONObject(response);
currentQuote = object.getString("content");
currentQuoteAuthor = object.getString("author");
System.out.println("Set");
} catch (JSONException e) {
e.printStackTrace();
}
},
volleyError -> Toast.makeText(MainActivity.this, volleyError.getMessage(), Toast.LENGTH_SHORT).show()
);
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(myRequest);
System.out.println(currentQuote);
System.out.println(currentQuoteAuthor);
output
2022-05-25 13:50:34.758 10165-10165/com.example.lab I/System.out: Getting data
2022-05-25 13:50:34.768 10165-10165/com.example.lab I/System.out: When the world is so complicated, the simple gift of friendship is within all of our hands.
2022-05-25 13:50:34.768 10165-10165/com.example.lab I/System.out: Maria Shriver
2022-05-25 13:50:34.822 10165-10165/com.example.lab I/System.out: Set
2022-05-25 13:50:37.038 10165-10165/com.example.lab I/System.out: Getting data
2022-05-25 13:50:37.050 10165-10165/com.example.lab I/System.out: Character develops itself in the stream of life.
2022-05-25 13:50:37.050 10165-10165/com.example.lab I/System.out: Johann Wolfgang von Goethe
2022-05-25 13:50:37.121 10165-10165/com.example.lab I/System.out: Set
2022-05-25 13:50:54.003 10165-10225/com.example.lab D/EGL_emulation: eglMakeCurrent: 0xddc05ba0: ver 3 0 (tinfo 0xddc03420)
2022-05-25 13:50:54.009 10165-10225/com.example.lab D/EGL_emulation: eglMakeCurrent: 0xddc05ba0: ver 3 0 (tinfo 0xddc03420)
2022-05-25 13:50:55.652 10165-10165/com.example.lab I/System.out: Getting data
2022-05-25 13:50:55.656 10165-10165/com.example.lab I/System.out: Technological progress has merely provided us with more efficient means for going backwards.
2022-05-25 13:50:55.656 10165-10165/com.example.lab I/System.out: Aldous Huxley
2022-05-25 13:50:55.751 10165-10225/com.example.lab D/EGL_emulation: eglMakeCurrent: 0xddc05ba0: ver 3 0 (tinfo 0xddc03420)
2022-05-25 13:50:55.757 10165-10225/com.example.lab D/EGL_emulation: eglMakeCurrent: 0xddc05ba0: ver 3 0 (tinfo 0xddc03420)
2022-05-25 13:50:55.759 10165-10165/com.example.lab I/System.out: Set
As you can see, in the final line of output, it is able to print "set", but doesn't output either the quote or the author. Any ideas?