如何在请求未成功完成时在Retrofit中调试Enqueue Call功能

时间:2016-12-07 09:04:06

标签: java android-studio android-fragments retrofit2 okhttp

在我的代码中,我正在使用改造来调用API。我已经把断点放在了线上 如果(response.isSuccessful())但该断点从未被击中。我想知道为什么会这样 因为当我将这个完全相同的代码放在另一个片段中并且它工作正常时我能够检索到 来自API的结果,但在此片段中,调用不起作用。我还在失败public void onFailure上设置了一个断点来检查是否 也许呼叫失败了,但是这个断点也没有被击中,这意味着对服务器的调用没有失败,但出于某种原因 正在跳过public void OnResponse中的整个代码块。 我现在怀疑我的片段中有代码与改装调用有关。  我已将我的片段的代码放在

下面
package com.xera.deviceinsight.home;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
//import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import com.androidquery.AQuery;
import com.xera.deviceinsight.Errors;
import com.xera.deviceinsight.Globals;
import com.xera.deviceinsight.R;
import com.xera.deviceinsight.api.OrganisationDeviceSensorsResult;
import com.xera.deviceinsight.api.Results;
import com.xera.deviceinsight.net.Claritech;
import com.xera.deviceinsight.net.ClaritechClient;
import com.xera.deviceinsight.receivers.IEvent;
import com.xera.deviceinsight.sensors.IotTabFragment;
import com.xera.deviceinsight.sensors.ItemClickedEvent;
import com.xera.deviceinsight.structs.DistanceUpdatedEvent;
import com.xera.deviceinsight.structs.HelloWorldEvent;
import com.xera.deviceinsight.structs.OrganisationDeviceSensorsEvent;

import de.greenrobot.event.EventBus;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CostCentreListFragment  extends Fragment {

   public static final String TAG = Globals.TAG + ".ALF";
   ViewPager mViewPager;
   ExpandableListAdapter listAdapter;
   ExpandableListView expListView;
   List<String> listDataHeader;
   HashMap<String, List<String>> listDataChild;
   public List<OrganisationDeviceSensorsResult> Items;
   public String currentReportingGroup;
   private ViewPager viewPager;
   private IEvent onDeviceSensorsObtained;
   private IEvent event;
   public List sensorList;


   @Nullable
   @Override
   public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      //EventBus eventBus = EventBus.getDefault();
     // if (!eventBus.isRegistered(this)) eventBus.register(this);
      View view = inflater.inflate(R.layout.layout_expandable, container, false);
      AQuery aq = new AQuery(view);
      getDeviceSensorCostCentres();
      load(view);
      return view;
   }

   private void load(View view)
   {
      expListView = (ExpandableListView) view.findViewById(R.id.lvExp);
      //EventBus.getDefault().post(new ItemClickedEvent(IotTabFragment.TAB_SENSOR));
      getDeviceSensorCostCentres();
      prepareListData();
      listAdapter = new ExpandableListAdapter(this.getActivity(), listDataHeader, listDataChild);
      expListView.setAdapter(listAdapter);
      expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
         @Override
         public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            //Nothing here ever fires
            System.err.println("child clicked");
            Toast.makeText(getActivity(), "child clicked", Toast.LENGTH_SHORT).show();

            // Navigate to second tab
           // EventBus.getDefault().post(new ItemClickedEvent(IotTabFragment.TAB_SENSOR));
            EventBus.getDefault().post(new ItemClickedEvent(IotTabFragment.TAB_USAGE));
            return true;
         }
      });
   }
   @Override
   public void onStart() {
      super.onStart();
      EventBus.getDefault().register(this);
   }
   @Override
   public void onPause() {
      EventBus.getDefault().unregister(this);
      super.onPause();
   }
   private void getDeviceSensorCostCentres() {
      //int costCentreID =0
      final Context context = this.getActivity();
      ClaritechClient client = new ClaritechClient(context);

      Claritech.api(context).getDeviceSensorCostCentres(0).enqueue(new Callback<Results<OrganisationDeviceSensorsResult>>() {
         @Override
         public void onResponse(Call<Results<OrganisationDeviceSensorsResult>> call, Response<Results<OrganisationDeviceSensorsResult>> response) {

            if (response.isSuccessful()) {
               // Reload data source
               Items.clear();
               Items.addAll(response.body());
               //ItemsAdapter.notifyDataSetChanged();
               Log.i(TAG, "onResponse: ");
            }
         }
         @Override
         public void onFailure(Call<Results<OrganisationDeviceSensorsResult>> call, Throwable t) {
            Errors.handleException(t);


         }
     });
    }


  public void onEvent(OrganisationDeviceSensorsEvent event){
       String Tag  ="";
       sensorList = event.deviceSensors;
       Log.i("EventBus",Tag);
       //prepareSensorListData();
        //Toast.makeText(getActivity(), event.deviceSensors, Toast.LENGTH_SHORT).show();
     };

   private void prepareListData() {
      listDataHeader = new ArrayList<String>();
      listDataChild = new HashMap<String, List<String>>();
      // Adding child data
      listDataHeader.add("Top 250");
      listDataHeader.add("Now Showing");
      listDataHeader.add("Coming Soon..");
      List<String> top250 = new ArrayList<String>();
      top250.add("The Shawshank Redemption");
      top250.add("The Godfather");
      top250.add("The Godfather: Part II");
      top250.add("Pulp Fiction");
      top250.add("The Good, the Bad and the Ugly");
      top250.add("The Dark Knight");
      top250.add("12 Angry Men");

      List<String> nowShowing = new ArrayList<String>();
      nowShowing.add("The Conjuring");
      nowShowing.add("Despicable Me 2");
      nowShowing.add("Turbo");
      nowShowing.add("Grown Ups 2");
      nowShowing.add("Red 2");
      nowShowing.add("The Wolverine");

      List<String> comingSoon = new ArrayList<String>();
      comingSoon.add("2 Guns");
      comingSoon.add("The Smurfs 2");
      comingSoon.add("The Spectacular Now");
      comingSoon.add("The Canyons");
      comingSoon.add("Europa Report");

      listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
      listDataChild.put(listDataHeader.get(1), nowShowing);
      listDataChild.put(listDataHeader.get(2), comingSoon);
   }

      };

}

1 个答案:

答案 0 :(得分:0)

我试图调用一个api调用的方法,然后我想调用另一个与改进冲突的方法,因为它在异步中进行异步调用,因此在完成请求之前它会跳转到下一个方法。我删除了第二个方法load()并将其放在onresponse