【问题标题】:Not able to get value of an integer after callback method response回调方法响应后无法获取整数值
【发布时间】:2016-05-28 04:00:55
【问题描述】:

我有一个方法可以进行 api 调用并解析响应。我在 onResponse 方法中获取当前天气的整数值。我想将此值设置为 textview 但在 onResponse 方法值不保留之后。我也将该变量设为全局变量。这个类扩展了片段,所以我在 onViewCreated 方法中设置值。

这里是带有日志的方法的代码...

public class HomeScreen extends Fragment{
int currentWeather = 0;

onViewCreated(){
 curWeather = ... initialisation.
 curWeather.setText(""+getCurrentWeather);
}

public int getCurrentWeather(){

    OkHttpClient okHttpClient = new OkHttpClient();
    Request re = new Request.Builder().url(forecastURL).build();
    Call call = okHttpClient.newCall(re);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String jsonResponse = response.body().string();
            JSONObject forecast;
            JSONObject currently;
            try {
                forecast = new JSONObject(jsonResponse);
                currently = forecast.getJSONObject("currently");
                currentTempFaron = (int) Math.round(currently.getDouble("temperature"));
                currentTempCelc = (currentTempFaron - 32) * 5 / 9;

                Log.d("weather in try", "" + currentTempCelc); // has weather value
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Log.d("weather in response", "" + currentTempCelc); // again has weather value.
        }
    });
return currentTempCelc; //nothing it has at this point after coming out of response.
}

}

【问题讨论】:

  • 您看到错误还是您的视图没有更新?
  • 不明白先生...
  • 我的视图没有更新,因为我的方法返回默认值 0
  • 好的,你知道你收到了一个有效的 JSON 响应吗?
  • 是的,这是有效的,我在日志语句中得到的值是完美的

标签: java android android-fragments


【解决方案1】:

Log.d("weather in response", "" + currentTempCelc); 之后在onResponse() 内执行此操作:

getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
        curWeather.setText(Integer.toString(currentTempCelc));
    }
});

这里的问题是您试图从不允许的后台线程更新 UI 线程。不过,您可以使用 runOnUiThread 来实现此目的。

【讨论】:

  • 由于这是一个片段,我已经获取了一个活动对象并将代码与 activity.runOnUi.... 方法一起使用,但它显示“无法在未调用 Looper.prepare 的线程内创建处理程序()".. 上线Activity activity = new Activity();
  • @AalapPatel,等等,你可以使用getActivity().runOnUiThread(etc) 来处理片段,而你正在使用片段。你先尝试过吗?事实上,我特意把它放在我的回答中,因为我看到你有一个片段。
  • 没问题,很高兴能帮上忙。
【解决方案2】:

使用Handler更新onResponse中的UI组件

new Handler(Looper.getMainLooper()).post(new Runnable() {
    @Override
    public void run() {
        curWeather.setText(Integer.toString(currentTempCelc));
        }
    }
});

【讨论】:

    【解决方案3】:

    使curWeather 成为Fragment 中的成员变量。然后在 onResponse 方法中调用 setText 上的 curWeather

    public class HomeScreen extends Fragment{
    int currentWeather = 0;
    final TextView curWeather
    
    onViewCreated(){
     curWeather = ... initialisation.
     // curWeather.setText(""+getCurrentWeather);
    }
    
    public int getCurrentWeather(){
    
        OkHttpClient okHttpClient = new OkHttpClient();
        Request re = new Request.Builder().url(forecastURL).build();
        Call call = okHttpClient.newCall(re);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
    
            }
    
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String jsonResponse = response.body().string();
                JSONObject forecast;
                JSONObject currently;
                try {
                    forecast = new JSONObject(jsonResponse);
                    currently = forecast.getJSONObject("currently");
                    currentTempFaron = (int) Math.round(currently.getDouble("temperature"));
                    currentTempCelc = (currentTempFaron - 32) * 5 / 9;
    
                    Log.d("weather in try", "" + currentTempCelc); // has weather value
                    curWeather.setText(""+currentTempCelc);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Log.d("weather in response", "" + currentTempCelc); // again has weather value.
            }
        });
    return currentTempCelc; //nothing it has at this point after coming out of response.
    }
    

    【讨论】:

      【解决方案4】:

      您可以使用方法 getCurrentWeather() 传递您的活动上下文。然后您可以访问 TextView 并设置 TextView。我尝试了这个解决方案,它对我有用。

      【讨论】:

        猜你喜欢
        • 2018-09-07
        • 1970-01-01
        • 2020-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-10
        • 2021-01-31
        • 1970-01-01
        相关资源
        最近更新 更多