Android ApolloCall.CallBack onResponse无法获取数据

时间:2017-05-19 12:20:31

标签: android interceptor graphql apollo apollo-android

我正在使用此查询来获取数据:

query Products($id: ID!){
            node(id: $id) {
                    id
                    ... on Collection {
                                title
                                products(first: 250){
                                                edges{
                                                    node{
                                                    title
                                                    id
                                            variants(first: 1){
                                                edges{
                                                    node{
                                                        price
                                                        }
                                                    }
                                                }
                                            images(first: 1,maxWidth:400,maxHeight:740,scale:3){
                                                edges{
                                                    node{
                                                        src
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

在ApolloCall.CallBack的OnResponse方法中,当我想要获取产品标题时,它会在此行生成nullPointerException" Log.i(" CallBack products",String.valueOf(response.data()。node()。asCollection()。products()。edges())); " 我需要在recyclerview中显示产品的标题,价格和图像。获取它们的最佳方法是什么? 提前谢谢!

下面是我的代码:

      @Override                                                                                                                 
 protected void onCreate(@Nullable Bundle savedInstanceState) {                                                            
     super.onCreate(savedInstanceState);                                                                                   
     setContentView(R.layout.activity_product);                                                                            
     String s = getIntent().getStringExtra("id");                                                                          
     Bundle extras = getIntent().getExtras();                                                                              
     if (extras != null) {                                                                                                 
         s = extras.getString("id");                                                                                       
     }                                                                                                                     
     application = (SampleApplication) getApplication();                                                                 


     content = (ViewGroup) findViewById(R.id.rl_content_holder);                                                           
     progressBar = (ProgressBar) findViewById(R.id.loading_bar);                                                           
     productRecyclerView = (RecyclerView) findViewById(R.id.rvProductList);                                                
     mProductAdapter = new ProductAdapter(this);                                                                           
     mGridLayoutManager = new GridLayoutManager(this,2);                                                                   

     fetchProducts();                                                                                                      
 }                                                                                                                         

 ApolloCall.Callback<Products.Data> mDataCallback =                                                                        
         new ApolloCall.Callback<Products.Data>() {                                                                        

             @Override                                                                                                     
             public void onResponse(@Nonnull final Response<Products.Data> response) {                                     

                     Log.i("CallBack products", String.valueOf(response.data().node().asCollection().products().edges())); 

                     runOnUiThread(new Runnable() {                                                                        
                         @Override                                                                                         
                         public void run() {                                                                               
                             Toast.makeText(ProductActivity.this, "getting products", Toast.LENGTH_SHORT).show();          
                             mProductAdapter.setProductData(response.data().node().asCollection().products().edges());     
                             productRecyclerView.setAdapter(mProductAdapter);                                              
                             productRecyclerView.setLayoutManager(mGridLayoutManager);                                     
                             progressBar.setVisibility(View.GONE);                                                         
                             content.setVisibility(View.VISIBLE);                                                          
                         }                                                                                                 
                     });                                                                                                   

             }                                                                                                             

             @Override                                                                                                     
             public void onFailure(@Nonnull ApolloException e) {                                                           
                 Log.e("Product Activity", e.getMessage(), e);                                                             
                 runOnUiThread(new Runnable() {                                                                            
                     @Override                                                                                             
                     public void run() {                                                                                   
                         progressBar.setVisibility(View.GONE);                                                             
                         Toast.makeText(ProductActivity.this, "getting products failed", Toast.LENGTH_SHORT).show();       
                     }                                                                                                     
                 });                                                                                                       
             }                                                                                                             
         };                                                                                                                
 public void fetchProducts() {                                                                                             

     mHttpClient = new OkHttpClient.Builder()                                                                              
             .addInterceptor(new HttpInterceptor())                                                                        
             .build();                                                                                                     
     Products productQuery = Products                                                                                      
             .builder().id("id")                                                                                           
             .build();                                                                                                     
     productCall = application.apolloClient().newCall(productQuery);                                                       
     productCall.enqueue(mDataCallback);                                                                                   

 }  

这里是RecyclerView适配器:

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {

    // Store the context for easy access
    private Context mContext;

    private List<Products.Data.Edge> productData = Collections.emptyList();

    public void setProductData(List<Products.Data.Edge>productData) {
        this.productData = productData;
        this.notifyDataSetChanged();
    }

    public ProductAdapter(Context context ) {

        mContext = context;
    }

    private Context getContext() {
        return mContext;
    }



    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView productListItem;
        public ImageView productListImage;
        private View rlContainer;
        Context context;

        public ViewHolder(Context context,View itemView) {
            super(itemView);
            productListItem = (TextView) itemView.findViewById(R.id.itemProductTxt);
            productListImage = (ImageView)itemView.findViewById(R.id.itemProductImg);
            rlContainer =  itemView.findViewById(R.id.linear_layout);
            this.context = context;
        }

        public void setProductItem(final Products.Data.Edge productItem) {

            productListItem.setText(productItem.node().title());


            rlContainer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Toast.makeText(context, "list item pressed", Toast.LENGTH_SHORT).show();

                }
            });
        }
    }

    @Override
    public ProductAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        // Inflate the custom layout
        View productView = inflater.inflate(R.layout.listitem_product, parent, false);

        // Return a new holder instance
        ProductAdapter.ViewHolder viewHolder = new ProductAdapter.ViewHolder(context,productView);
        return viewHolder;
    }


    @Override
    public void onBindViewHolder(ProductAdapter.ViewHolder holder, int position) {

        final Products.Data.Edge productsEntry = this.productData.get(position);
        holder.setProductItem(productsEntry);



    @Override
    public int getItemCount() {
        return productData.size();
    }


}

1 个答案:

答案 0 :(得分:0)

请你提供堆栈跟踪。 你确定你没有回复空洞吗?

你得到NPE可能是因为id所请求的节点不存在。