【问题标题】:Shopify Integration in android安卓中的 Shopify 集成
【发布时间】:2017-07-12 07:21:23
【问题描述】:

您好,我已将 shopify sdk 集成到我的应用程序中,现在版本已更改,因此我必须将我的 api v2 迁移到我的 android 应用程序的 v3 中。在这里,他们一直在使用 GraphQL 概念而不是 REST API,用于 Web 服务调用来获取产品和集合详细信息。

这是我的代码。

graphClient = GraphClient.builder(getActivity())
                    .shopDomain(shopUrl)
                    .accessToken(shopifyAPIKey)
                    //   .httpClient(httpClient) // optional
                    .httpCache(new File(getActivity().getCacheDir(), "/http"), 10 * 1024 * 1024) // 10mb for http cache
                    .defaultHttpCachePolicy(HttpCachePolicy.CACHE_FIRST.expireAfter(5, TimeUnit.MINUTES)) // cached response valid by default for 5 minutes
                    .build();


            query = Storefront.query(rootQuery -> rootQuery
                    .shop(shopQuery -> shopQuery
                            .name()
                            .currencyCode()
                            .refundPolicy(refundPolicyQuery -> refundPolicyQuery
                                    .title()
                                    .url()
                            )
                    )
            );


            graphClient.queryGraph(query).enqueue(new GraphCall.Callback<Storefront.QueryRoot>() {
                @Override
                public void onResponse(@NonNull GraphResponse<Storefront.QueryRoot> response) {

                    String name = response.data().getShop().getName();

                    System.out.println("Response of Shopify : " + response.data().toString());

                    System.out.println("Shop Name : " + name);


                }

                @Override
                public void onFailure(@NonNull GraphError error) {

                    error.printStackTrace();

                    System.out.println("Shopify Error : " + error.getMessage());

                    if (progressDialog != null && progressDialog.isShowing()) {
                        progressDialog.dismiss();
                    }

                }
            });

此代码用于查询以获取集合和产品

query = Storefront.query(rootQuery -> rootQuery
            .shop(shopQuery -> shopQuery
                    .collections(collectionCount, collectionConnectionQuery -> collectionConnectionQuery
                            .edges(collectionEdgeQuery -> collectionEdgeQuery
                                    .node(collectionQuery -> collectionQuery
                                            .title()
                                            .products(productCount, productConnectionQuery -> productConnectionQuery
                                                    .edges(productEdgeQuery -> productEdgeQuery
                                                            .node(productQuery -> productQuery
                                                                    .title()
                                                                    .productType()
                                                                    .description()
                                                                    .images(2, imageConnectionQuery -> imageConnectionQuery
                                                                            .edges(imageEdgeQuery -> imageEdgeQuery
                                                                                    .node(imageQuery -> imageQuery
                                                                                            .src()
                                                                                    )
                                                                            )
                                                                    )
                                                                    .variants(2, variantConnectionQuery -> variantConnectionQuery
                                                                            .edges(variantEdgeQuery -> variantEdgeQuery
                                                                                    .node(productVariantQuery -> productVariantQuery
                                                                                            .price()
                                                                                            .title()
                                                                                            .available()


                                                                                    )


                                                                            )
                                                                    )
                                                            )
                                                    )
                                            )
                                    )
                            )
                    )));

这里我是 GraphQL 概念的新手,那么我如何获得产品图片 url、价格和其他详细信息?请指导我使用此 GraphQL 获取有关产品和其他详细信息的任何数据

【问题讨论】:

  • 你做这个项目了吗,我已经开始了一个新的android studio项目但是无法调用单个API。你能帮我吗?

标签: android shopify graphql


【解决方案1】:

虽然迟到是解决办法。

在给定的代码中,您已经创建了一个图形客户端,并提供了您的域和 API 密钥。接下来,您刚刚通过查询提到了您需要的所有数据。

现在需要调用 Shopify 端点,以便您可以获取所需的数据。

QueryGraphCall call_products = graphClient.queryGraph(query);

        call_products.enqueue(new GraphCall.Callback<Storefront.QueryRoot>() {
            @Override
            public void onResponse(@NonNull GraphResponse<Storefront.QueryRoot> response) {

                List<Storefront.ProductEdge> list_edge = response.data().getShop().getProducts().getEdges(); //List of products that you have in your store.

                for(int i=0;i<list_edge.size();i++){
                    Log.d(TAG,"product title: "+response.data().getShop().getProducts().getEdges().get(i).getNode().getTitle());
                    Log.d(TAG,"product id: "+response.data().getShop().getProducts().getEdges().get(i).getNode().getId());
                    Log.d(TAG,"product description: "+response.data().getShop().getProducts().getEdges().get(i).getNode().getDescription());
                }
            }

            @Override
            public void onFailure(@NonNull GraphError error) {
                Log.e(TAG, "onFailure: " + error.toString());
            }
        });

希望它有效:)

【讨论】:

  • 我已经启动了一个新的 android studio 项目但无法调用单个 API。你能帮帮我吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-09
  • 2015-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多