功能没有被称为正确

时间:2016-09-02 19:37:37

标签: java android okhttp

我正在尝试通过for循环调用updateDisplay方法来设置相应索引的文本,但在输出中只有第5个索引代码正在运行。

这是我在片段的onCreateView()中调用的for循环;

private int mIndexofDays;

for(int i =1; i < 6; i++) {
        DateTime nextday = mDateTime.plusDays(i);
        long time = nextday.getMillis() / 1000;
        getForecast(mLattitude, mLongitude, time);
        mIndexofDays = i;
   }

这是getForecast()方法:

 private void getForecast(double latitude, double longitude, long time)
{
    String apiKey =  getResources().getString(R.string.api_key);


    String forecastUrl = "https://api.forecast.io/forecast/" + apiKey +
            "/" + latitude + "," + longitude + "," + time;





            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(forecastUrl)
                    .build();
            Call call = client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {



                        }
                    });


                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                        }
                    });

                    try {
                        String jsonData = response.body().string();
                        Log.v(TAG, jsonData);
                        if (response.isSuccessful()) {

                            mWeather = getCurrentDetails(jsonData);
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Log.d(TAG, "Running....");
                                    updateDisplay();


                                }
                            });

                        } else {
                            Log.d(TAG, "Response not successful");
                        }
                    } catch (IOException e) {
                        Log.e(TAG, " IOException caught: ", e);
                    } catch (JSONException e) {
                        Log.e(TAG, "JSON exception caught: ", e);

                    }
                }
            });
        }

这是updateDisplay()方法:

private void updateDisplay(){

    if(mIndexofDays == 1) {
        mDayOfWeek1.setText(mDateTime.plusDays(1).dayOfWeek().getAsShortText());
        Drawable drawable = getResources().getDrawable(mWeather.getIconId());
        mDayOfWeekImage1.setImageDrawable(drawable);
        mHighTemp1.setText(mWeather.getTemperatureMax() + "");
        mLowTemp1.setText(mWeather.getTemperatureMin() + "");
    }
    if(mIndexofDays == 2) {
        mDayOfWeek2.setText(mDateTime.plusDays(2).dayOfWeek().getAsShortText());
        Drawable drawable = getResources().getDrawable(mWeather.getIconId());
        mDayOfWeekImage2.setImageDrawable(drawable);
        mHighTemp2.setText(mWeather.getTemperatureMax() + "");
    }

    if(mIndexofDays == 3) {
        mDayOfWeek3.setText(mDateTime.plusDays(3).dayOfWeek().getAsShortText());
        Drawable drawable = getResources().getDrawable(mWeather.getIconId());
        mDayOfWeekImage3.setImageDrawable(drawable);
        mHighTemp3.setText(mWeather.getTemperatureMax() + "");
    }
    if(mIndexofDays == 4) {
        mDayOfWeek4.setText(mDateTime.plusDays(4).dayOfWeek().getAsShortText());
        Drawable drawable = getResources().getDrawable(mWeather.getIconId());
        mDayOfWeekImage4.setImageDrawable(drawable);
        mHighTemp4.setText(mWeather.getTemperatureMax() + "");
    }
   if(mIndexofDays == 5) {
       mDayOfWeek5.setText(mDateTime.plusDays(5).dayOfWeek().getAsShortText());
        Drawable drawable = getResources().getDrawable(mWeather.getIconId());
        mDayOfWeekImage5.setImageDrawable(drawable);
        mHighTemp5.setText(mWeather.getTemperatureMax() + "");

    }
    else
    {
        Log.d(TAG, "Index to high!!!");
    }
}

从日志中我可以看到“正在运行”被调用,但updateDisplay从不更新仅针对第5个索引的1-4个索引。

我是一名非常新手的程序员,所以请告诉我我的风格有什么问题,以及做我想做的事情的更好的方法。

3 个答案:

答案 0 :(得分:0)

修改updateDisplay并将mIndexofDays的副本作为参数传递,这应该有效。我可以提供实际的实现代码,但我鼓励您先自己尝试。

希望这会有所帮助:)

答案 1 :(得分:0)

像这样改变你的for循环

for(int i =1; i < 6; i++) {
        mIndexofDays = i;
        DateTime nextday = mDateTime.plusDays(i);
        long time = nextday.getMillis() / 1000;
        getForecast(mLattitude, mLongitude, time, mIndexofDays); // new parameter: mIndexofDays
}

在getForecast方法中捕获参数mIndexofDays并通过updateDisplay方法传递它。接下来,使用mIndexofDays的值在if ... else语句中进行比较。您可以使用Log方法或时间延迟方法来检查它是否实际正常工作。

答案 2 :(得分:0)

好吧,问题是只有在updateDisplay()收到回复时才会调用onResponse()。现在,到发生这种情况时,您的循环已经结束,mIndexofDays的值为5。要解决此问题,您可以执行的一项操作是将mIndexofDays的值传递给getForecast()方法:

private void getForecast(double latitude, double longitude, long time, int indexOfDays) {
    ...
    updateDisplay(numberOfDays); 
    ...
}

您还需要更改updateDisplay()方法:

private void updateDisplay(int indexOfDays) {
    ...
}

另外,摆脱mIndexOfDays实例变量,因为你[可能]在任何地方都不需要它。