Java 如何确保函数在调用它的函数完成后运行?

Java 如何确保函数在调用它的函数完成后运行?,java,android,function,Java,Android,Function,我有一个函数,可以获取一些天气数据并将其输出到应用程序中的文本视图。发生这种情况后,我需要更新另一个文本视图(基于天气数据和一些其他变量…),我有一个执行此任务的函数,但它似乎在前一个函数完成之前运行,就像我现在正在做的那样。处理天气数据的函数称为weatherUpdate,处理第二次文本更新的函数称为textUpdate。我正在调用weatherUpdate函数末尾的textUpdate函数 如何确保textUpdate在weatherUpdate完成后运行 void weatherUpdat

我有一个函数,可以获取一些天气数据并将其输出到应用程序中的文本视图。发生这种情况后,我需要更新另一个文本视图(基于天气数据和一些其他变量…),我有一个执行此任务的函数,但它似乎在前一个函数完成之前运行,就像我现在正在做的那样。处理天气数据的函数称为weatherUpdate,处理第二次文本更新的函数称为textUpdate。我正在调用weatherUpdate函数末尾的textUpdate函数

如何确保textUpdate在weatherUpdate完成后运行

void weatherUpdate() {
    //Weather API url and key
    String apiURL = "https://api.openweathermap.org/data/2.5/weather?lat=00.0000&lon=00.0000&units=metric&APPID=00000000000000000000000000000000";
    //Request JSON data from weather API
    RequestQueue queue = Volley.newRequestQueue(this);
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, apiURL, null, new Response.Listener<JSONObject>() {
        //Parse data to get temperature
        @Override
        public void onResponse(JSONObject response) {
            try {
                //Get temperature data, format it to 1 decimal place, and output in the text box.
                temp1 = (response.getJSONObject("main").getDouble("temp"));
                String tempFormatted = (getString(R.string.temp_format, temp1));
                tempBox.setText(tempFormatted);
                //get the icon for weather conditions.
                String iconName = response.getJSONArray("weather").getJSONObject(0).getString("icon");
                String imageURL = String.format("http://openweathermap.org/img/w/%1s.png", iconName);
                Glide.with(MainActivity.this).load(imageURL).into(weatherImage);
            } catch (JSONException e) {
                //catch errors and toast error message.
                e.printStackTrace();
                Toast errorToast = Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG);
                errorToast.show();
            }
        }
        //Request error handler
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast errorToast = Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG);
            errorToast.show();
        }
    });
    queue.add(request);
    //Toast notification that update has run.
    Toast.makeText(MainActivity.this, "Weather Updated!", Toast.LENGTH_SHORT).show();
    textUpdate(); //<-this is where my problem is. It seems to run before the above is finished.
}
void weatherUpdate(){
//天气API url和密钥
字符串apiURL=”https://api.openweathermap.org/data/2.5/weather?lat=00.0000&lon=00.0000&units=metric&APPID=00000000000000000000000000000000";
//从天气API请求JSON数据
RequestQueue=Volley.newRequestQueue(this);
JsonObjectRequest=newJSONObjectRequest(request.Method.GET,apiURL,null,new Response.Listener()){
//解析数据以获取温度
@凌驾
公共void onResponse(JSONObject响应){
试一试{
//获取温度数据,将其格式设置为小数点后1位,并在文本框中输出。
temp1=(response.getJSONObject(“main”).getDouble(“temp”);
字符串tempFormatted=(getString(R.String.temp_格式,temp1));
tempBox.setText(tempFormatted);
//获取天气状况图标。
String iconName=response.getJSONArray(“天气”).getJSONObject(0.getString(“图标”);
String imageURL=String.format(“http://openweathermap.org/img/w/%1s.png“,我的名字);
Glide.with(MainActivity.this).load(imageURL).into(weatherImage);
}捕获(JSONException e){
//捕获错误并发送错误消息。
e、 printStackTrace();
Toast errorToast=Toast.makeText(MainActivity.this,e.getMessage(),Toast.LENGTH\u LONG);
errorToast.show();
}
}
//请求错误处理程序
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast errorToast=Toast.makeText(MainActivity.this,error.getMessage(),Toast.LENGTH\u LONG);
errorToast.show();
}
});
添加(请求);
//更新已运行的Toast通知。
Toast.makeText(MainActivity.this,“天气更新!”,Toast.LENGTH_SHORT.show();

textUpdate();//我认为您在错误的位置调用textUpdate。您正在执行异步网络调用,应该只在回调函数中调用textUpdate。 请参见下文-在onResponse中调用函数

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, apiURL, null, new Response.Listener<JSONObject>() {
        //Parse data to get temperature
        @Override
        public void onResponse(JSONObject response) {
            try {
                //Get temperature data, format it to 1 decimal place, and output in the text box.
                temp1 = (response.getJSONObject("main").getDouble("temp"));
                String tempFormatted = (getString(R.string.temp_format, temp1));
                tempBox.setText(tempFormatted);
                //get the icon for weather conditions.
                String iconName = response.getJSONArray("weather").getJSONObject(0).getString("icon");
                String imageURL = String.format("http://openweathermap.org/img/w/%1s.png", iconName);
                Glide.with(MainActivity.this).load(imageURL).into(weatherImage);
                textUpdate();
            } catch (JSONException e) {
                //catch errors and toast error message.
                e.printStackTrace();
                Toast errorToast = Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG);
                errorToast.show();
            }
        }
        //Request error handler
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast errorToast = Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG);
            errorToast.show();
        }
    });
JsonObjectRequest request=newjsonobjectrequest(request.Method.GET,apirl,null,new Response.Listener()){
//解析数据以获取温度
@凌驾
公共void onResponse(JSONObject响应){
试一试{
//获取温度数据,将其格式设置为小数点后1位,并在文本框中输出。
temp1=(response.getJSONObject(“main”).getDouble(“temp”);
字符串tempFormatted=(getString(R.String.temp_格式,temp1));
tempBox.setText(tempFormatted);
//获取天气状况图标。
String iconName=response.getJSONArray(“天气”).getJSONObject(0.getString(“图标”);
String imageURL=String.format(“http://openweathermap.org/img/w/%1s.png“,我的名字);
Glide.with(MainActivity.this).load(imageURL).into(weatherImage);
textUpdate();
}捕获(JSONException e){
//捕获错误并发送错误消息。
e、 printStackTrace();
Toast errorToast=Toast.makeText(MainActivity.this,e.getMessage(),Toast.LENGTH\u LONG);
errorToast.show();
}
}
//请求错误处理程序
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast errorToast=Toast.makeText(MainActivity.this,error.getMessage(),Toast.LENGTH\u LONG);
errorToast.show();
}
});

我认为您在错误的位置调用textUpdate。您正在执行异步网络调用,您应该只在回调函数中调用textUpdate。 请参见下文-在onResponse中调用函数

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, apiURL, null, new Response.Listener<JSONObject>() {
        //Parse data to get temperature
        @Override
        public void onResponse(JSONObject response) {
            try {
                //Get temperature data, format it to 1 decimal place, and output in the text box.
                temp1 = (response.getJSONObject("main").getDouble("temp"));
                String tempFormatted = (getString(R.string.temp_format, temp1));
                tempBox.setText(tempFormatted);
                //get the icon for weather conditions.
                String iconName = response.getJSONArray("weather").getJSONObject(0).getString("icon");
                String imageURL = String.format("http://openweathermap.org/img/w/%1s.png", iconName);
                Glide.with(MainActivity.this).load(imageURL).into(weatherImage);
                textUpdate();
            } catch (JSONException e) {
                //catch errors and toast error message.
                e.printStackTrace();
                Toast errorToast = Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG);
                errorToast.show();
            }
        }
        //Request error handler
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast errorToast = Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG);
            errorToast.show();
        }
    });
JsonObjectRequest request=newjsonobjectrequest(request.Method.GET,apirl,null,new Response.Listener()){
//解析数据以获取温度
@凌驾
公共void onResponse(JSONObject响应){
试一试{
//获取温度数据,将其格式设置为小数点后1位,并在文本框中输出。
temp1=(response.getJSONObject(“main”).getDouble(“temp”);
字符串tempFormatted=(getString(R.String.temp_格式,temp1));
tempBox.setText(tempFormatted);
//获取天气状况图标。
String iconName=response.getJSONArray(“天气”).getJSONObject(0.getString(“图标”);
String imageURL=String.format(“http://openweathermap.org/img/w/%1s.png“,我的名字);
Glide.with(MainActivity.this).load(imageURL).into(weatherImage);
textUpdate();
}捕获(JSONException e){
//捕获错误并发送错误消息。
e、 printStackTrace();
Toast errorToast=Toast.makeText(MainActivity.this,e.getMessage(),Toast.LENGTH\u LONG);
errorToast.show();
}
}
//请求错误处理程序
},new Response.ErrorListener(){
@凌驾
公众反应(截击)