Access variable set by Volley's onResponse

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Access variable set by Volley's onResponse



I have two global variables currentTemp and currentHum that are set when Volley's onResponse method is called. My code looks like this:


currentTemp


currentHum


onResponse


// Request a string response from the provided URL.
private JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, WEATHER_URL, null, new Response.Listener<JSONObject>()
public void onResponse(JSONObject response)
try
JSONObject main = new JSONObject(response.getString("main"));
currentTemp = main.getString("temp");
currentHum = main.getString("humidity");
Log.i("RES", "Temp: " + main.getString("temp") + " Hum: " + main.getString("humidity"));
catch (JSONException e)
e.printStackTrace();

, new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
Toast.makeText(appContext, "An error occurred while retrieving weather info", Toast.LENGTH_LONG).show();
Log.e("ERR", "ERROR RET WEATHER DATA");

);


// Call the OpenWeatherMap API and get data such as temperature and humidity
private String getWeatherInfo(String key)
// Add the request to the RequestQueue to invoke the API
queue.add(jsonObjectRequest);
// Access variables set by Volley's onResponse here.
switch (key)
case "temp":
return String.valueOf(Float.parseFloat(currentTemp) - 273.15);
case "hum":
return currentHum;
default:
return " ";




I want to be able to access the values of the global variables set by the onResponse method in the getWeatherInfo method that invoked it. Then pass the values to a switch statement for processing. How do I do it without getting empty values for currentTemp and currentHum?


onResponse


getWeatherInfo


currentTemp


currentHum





Add a code for how you are executing jsonObjectRequest and calling getWeatherInfo
– MJM
Aug 8 at 4:26


jsonObjectRequest


getWeatherInfo





@MJM I call getWeatherInfo using something like: "Its" + getWeatherInfo("temp") + "degrees celsius"
– Nikhil Raghavendra
Aug 8 at 4:35


"Its" + getWeatherInfo("temp") + "degrees celsius"




1 Answer
1



This work is meant to be done with use of an interface. If you don't know about an interface callbacks then please go through this answer. Which will help you understand interface, and plus point that this will guide you for handling volley response.



Also this is not recommended to take global variables to set any response, rather you can pass your whole JsonObject from volley class. and parse it where you make a call. You can use Gson for parsing the response to your Model or ArrayList.


JsonObject


Model


ArrayList






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered