Android ApolloCall.CallBack onResponse не получает данные

Я использую этот запрос для получения данных:

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
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

В методе OnResponse ApolloCall.CallBack, когда я хочу получить название продукта, он генерирует исключение 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();
    }


}

person Sehrish Fiaz    schedule 19.05.2017    source источник


Ответы (1)


Не могли бы вы предоставить трассировку стека. И вы уверены, что получите не пустой ответ?

Вероятно, вы получаете NPE, потому что запрошенный узел по идентификатору не существует.

person Ivan Savytskyi    schedule 20.05.2017