Android 通过截击设置的访问变量';回应

Android 通过截击设置的访问变量';回应,android,request,android-volley,Android,Request,Android Volley,我有两个全局变量currentTemp和currentHum,它们是在调用Volley的onResponse方法时设置的。我的代码如下所示: // Request a string response from the provided URL. private JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, WEATHER_URL, null, new Response.Lis

我有两个全局变量
currentTemp
currentHum
,它们是在调用Volley的
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 " ";
        }
    }
//从提供的URL请求字符串响应。
私有JsonObjectRequest JsonObjectRequest=新JsonObjectRequest(Request.Method.GET,WEATHER_URL,null,new Response.Listener()){
公共void onResponse(JSONObject响应){
试一试{
JSONObject main=新的JSONObject(response.getString(“main”);
currentTemp=main.getString(“temp”);
currentHum=main.getString(“湿度”);
Log.i(“RES”,“Temp:”+main.getString(“Temp”)+“Hum:”+main.getString(“湿度”));
}捕获(JSONException e){
e、 printStackTrace();
}
}},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(appContext,“检索天气信息时出错”,Toast.LENGTH_LONG.show();
Log.e(“ERR”、“ERR RET气象数据”);
}
});
//调用OpenWeatherMapAPI并获取温度和湿度等数据
私有字符串getWeatherInfo(字符串键){
//将请求添加到RequestQueue以调用API
add(jsonObjectRequest);
//在这里访问由Volley的onResponse设置的变量。
开关(钥匙){
案例“临时”:
返回字符串.valueOf(Float.parseFloat(currentTemp)-273.15);
案例“哼”:
回流电流;
违约:
返回“”;
}
}

我希望能够访问调用它的
getWeatherInfo
方法中的
onResponse
方法设置的全局变量值。然后将值传递给switch语句进行处理。如何在不获取
currentTemp
currentHum
的空值的情况下执行此操作

这项工作是通过使用接口来完成的。如果您不知道接口回调,请查看。这将有助于你们理解界面,另外一点是,这将指导你们处理截击反应


另外,不建议使用全局变量来设置任何响应,相反,您可以通过volley类传递整个
JsonObject
。然后在你打电话的地方解析它。您可以使用解析对
模型的响应
数组列表

添加一个代码,说明如何执行
jsonObjectRequest
并调用
getWeatherInfo
@MJM我调用getWeatherInfo时使用类似于:
“Its”+getWeatherInfo(“temp”)+“摄氏度”