“已经执行”错误(Retrofit2和Android)

问题描述 投票:0回答:1

首先,我有2个片段,它们显示不同的json值(2个链接)。如果我更改片段(.replace()),该片段将通过改造获取值并显示它。它运行良好,但其他片段将被删除值。更改片段后,再次下载,以便更改结构。我想要获取2个json对象,因此我在mainactivity中获取json对象,并使用方法获取这些对象。它们在第一次打开时效果很好但是如果我第二次打开一个片段,则会出现此错误。我该如何解决?

java.lang.IllegalStateException: Already executed.
at retrofit2.OkHttpCall.enqueue(OkHttpCall.java:84)

代码很长,我将展示主要结构。

main activity.Java

public class MainActivity extends AppCompatActivity
{
    private Call<Restaurant[]> restaurantCall;
    private Call<Dining[]> diningCall;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Restaurant Json value
        RestaurantInterface restaurantInterface = RetroClient.getClient().create(RestaurantInterface.class);
        restaurantCall = restaurantInterface.getJsonValues();

        //Dininghall Json value
        DiningInterface diningInterface = RetroClient.getClient().create(DiningInterface.class);
        diningCall = diningInterface.getJsonValues();
    }
    public Call<Restaurant[]> RestaurantJson()
    {
        return this.restaurantCall;
    }

    public Call<Dining[]> DiningJson()
    {
        return this.diningCall;
    }
}

RestaurantFragment.java(其他片段具有相同的结构)

public class RestFragment extends Fragment
{

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_rest, container, false);

        Call<Restaurant[]> call = ((MainActivity) getActivity()).RestaurantJson();

        call.enqueue(new Callback<Restaurant[]>()
        {
            .
            .
android android-fragments retrofit retrofit2 android-fragmentactivity
1个回答
1
投票

总结一下,为了避免“已经执行”异常,克隆调用:

public Call<Restaurant[]> RestaurantJson()
{
    return this.restaurantCall.clone();
}

public Call<Dining[]> DiningJson()
{
    return this.diningCall.clone();
}

如果您想在活动中而不是在实际执行的片段中执行调用,则需要在活动中调用enqueue(new Callbak(...。所以你需要这样的东西:

public class RestFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_rest, container, false);

        ((MainActivity) getActivity()).RestaurantJson(this);
    }

     public void onResponse(Response<T> response) {
         //your fragment code on response
     } 
    ...
}

public class MainActivity extends AppCompatActivity {

    public void RestaurantJson(final RestFragment fragment)
    {
        RestaurantInterface restaurantInterface = RetroClient.getClient().create(RestaurantInterface.class);
        restaurantCall = restaurantInterface.getJsonValues();
        restaurantCall.enqueue(new Callback<Restaurant[]>() {

        @Override
        public void onResponse(Call<T> call, Response<T> response) {
            ...
            fragment.onResponse(response);
        }
    }
    ...
}

您的DiningFragment和用餐电话也一样......

© www.soinside.com 2019 - 2024. All rights reserved.