1
votes

I'm created android and ios Ecommerce application using Mobile buy SDK for android and Mobile buy SDK for ios.

My question is that how to get a Blog-List from SDK and i need to display Blog-List then on click of blog list display article so is there anyone implemented this type of functionality using query and get blocks from mobile-buy-sdk-android?

This is my SDK for android : SDK

Ref:

Get properties like below attached image but unable to find any code snippet of how to call query and get block list.

enter image description here

1

1 Answers

0
votes

Finally i've got an solution for getting blog-list and article-list and article details by written functions. i've posted here because it'll helpful to others.

Blog-List Query:

String cursor = "";
private void fetchData(String mCursor, String FirstTime) {
    if (FirstTime.equals("0")) {
        Common.ProgressDialogShow(context, getString(R.string.please_wait));
    }
    cursor = Util.reduce(blogsClassArrayList, (acc, val) -> val.cursor, null);
    if (mCursor != null && !mCursor.equals("")) {
        cursor = mCursor;
    }

    Storefront.QueryRootQuery query = Storefront.query(rootQuery ->
            rootQuery.shop(shopQuery -> shopQuery.blogs(args ->
                            args.query("")
                                    .first(Constant.CONSTANT_PAGE_SIZE_250)
                                    .after(cursor),
                    blogConnectionQuery -> blogConnectionQuery.edges(queryEdges ->
                            queryEdges.cursor().node(queryDef ->
                                    queryDef
                                            .title()
                                            .url())))));


    String shopUrl = AppPreference.getStringPref(this, AppPreference.PREF_SHOP,
            AppPreference.PREF_KEY.PREF_KEY_SHOP_DOMAIL);
    String shopifyApiKey = AppPreference.getStringPref(this, AppPreference.PREF_SHOP,
            AppPreference.PREF_KEY.PREF_KEY_API_KEY);

    OkHttpClient httpClient = new OkHttpClient.Builder()
            .addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(BuildConfig.OKHTTP_LOG_LEVEL))
            .build();

    graphClient = GraphClient.builder(this)
            .shopDomain(shopUrl)
            .accessToken(shopifyApiKey)
            .httpClient(httpClient)
            .httpCache(getCacheDir(), 1024 * 1024 * 10)
            .defaultHttpCachePolicy(HttpCachePolicy.CACHE_FIRST.expireAfter(1, TimeUnit.MINUTES))
            .build();

    graphClient.queryGraph(query).enqueue(new GraphCall.Callback<Storefront.QueryRoot>() {

        @Override
        public void onResponse(@NonNull GraphResponse<Storefront.QueryRoot> response) {
            try {
                if (response.data().getShop() != null) {
                    if (response.data().getShop().getBlogs() != null) {
                        if (response.data().getShop().getBlogs().getEdges().size() > 0) {
                            if (response.data().getShop().getBlogs().getEdges().get(0).getNode() != null) {
                                blogsClassArrayList = new ArrayList<>();
                                for (int i = 0; i < response.data().getShop().getBlogs().getEdges().size(); i++) {
                                    ID mId = response.data().getShop().getBlogs().getEdges().get(i).getNode().getId();
                                    String mTitle = response.data().getShop().getBlogs().getEdges().get(i).getNode().getTitle();
                                    String mURL = response.data().getShop().getBlogs().getEdges().get(i).getNode().getUrl();

                                    BlogsClass blogsClass = new BlogsClass();
                                    blogsClass.setmId(mId);
                                    blogsClass.setmTitle(mTitle);
                                    blogsClass.setmURL(mURL);
                                    blogsClassArrayList.add(blogsClass);
                                }

                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (blogsClassArrayList != null && blogsClassArrayList.size() > 0) {
                                            setAdapters();
                                            SetDataEmptyOrNot();
                                            Common.ProgressDialogDismiss();
                                        } else {
                                            Toast.makeText(context, getString(R.string.no_blogs_found), Toast.LENGTH_LONG).show();
                                            SetDataEmptyOrNot();
                                            Common.ProgressDialogDismiss();
                                        }
                                    }

                                });
                            } else {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Common.ProgressDialogDismiss();
                                        Toast.makeText(context, getString(R.string.no_blogs_found), Toast.LENGTH_LONG).show();
                                        SetDataEmptyOrNot();
                                    }
                                });
                            }
                        } else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Common.ProgressDialogDismiss();
                                    Toast.makeText(context, getString(R.string.no_blogs_found), Toast.LENGTH_LONG).show();
                                    SetDataEmptyOrNot();
                                }
                            });
                        }
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Common.ProgressDialogDismiss();
                                Toast.makeText(context, getString(R.string.no_blogs_found), Toast.LENGTH_LONG).show();
                                SetDataEmptyOrNot();
                            }
                        });
                    }
                }
            } catch (Exception e) {
                e.getStackTrace();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Common.ProgressDialogDismiss();
                        Toast.makeText(context, getString(R.string.no_blogs_found), Toast.LENGTH_LONG).show();
                        SetDataEmptyOrNot();
                    }
                });
            }
        }

        //Unable to resolve host "asdfsd": No address associated with hostname ,Failed to execute GraphQL http request
        @Override
        public void onFailure(@NonNull GraphError error) {
            error.getStackTrace();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Common.ProgressDialogDismiss();
                            Toast.makeText(context, getString(R.string.no_blogs_found), Toast.LENGTH_LONG).show();
                            SetDataEmptyOrNot();
                        }
                    }, 5000);
                }
            });
        }
    });
}

Article-List Query :

String cursor = "";
private void fetchData(String mCursor, String FirstTime) {
    if (FirstTime.equals("0")) {
        Common.ProgressDialogShow(context, getString(R.string.please_wait));
    }
    cursor = Util.reduce(blogArticleClassArrayList, (acc, val) -> val.cursor, null);
    if (mCursor != null && !mCursor.equals("")) {
        cursor = mCursor;
    }

    Storefront.QueryRootQuery query = Storefront.query(rootQuery ->
            rootQuery.shop(shopQuery -> shopQuery.articles(args ->
                            args.query(mBlogTitle)
                                    .first(Constant.CONSTANT_PAGE_SIZE_250)
                                    .after(cursor).reverse(true),
                    articleConQuery -> articleConQuery.edges(queryEdges ->
                            queryEdges.cursor().node(queryDef ->
                                    queryDef
                                            .tags()
                                            .title()
                                            .content()
                                            .contentHtml()
                                            .excerpt()
                                            .excerptHtml()
                                            .image(imgQueryDef ->
                                                    imgQueryDef
                                                            .src()
                                                            .id()
                                                            .altText())
                                            .blog(blogQueryDef -> blogQueryDef.title().url()))))));



    String shopUrl = AppPreference.getStringPref(this, AppPreference.PREF_SHOP,
            AppPreference.PREF_KEY.PREF_KEY_SHOP_DOMAIL);
    String shopifyApiKey = AppPreference.getStringPref(this, AppPreference.PREF_SHOP,
            AppPreference.PREF_KEY.PREF_KEY_API_KEY);

    OkHttpClient httpClient = new OkHttpClient.Builder()
            .addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(BuildConfig.OKHTTP_LOG_LEVEL))
            .build();

    graphClient = GraphClient.builder(this)
            .shopDomain(shopUrl)
            .accessToken(shopifyApiKey)
            .httpClient(httpClient)
            .httpCache(getCacheDir(), 1024 * 1024 * 10)
            .defaultHttpCachePolicy(HttpCachePolicy.CACHE_FIRST.expireAfter(1, TimeUnit.MINUTES))
            .build();

    graphClient.queryGraph(query).enqueue(new GraphCall.Callback<Storefront.QueryRoot>() {

        @Override
        public void onResponse(@NonNull GraphResponse<Storefront.QueryRoot> response) {
            try {
                if (response.data().getShop() != null) {
                    if (response.data().getShop().getArticles() != null) {
                        if (response.data().getShop().getArticles().getEdges().size() > 0) {
                            if (response.data().getShop().getArticles().getEdges().get(0).getNode() != null) {
                                blogArticleClassArrayList = new ArrayList<>();
                                for (int i = 0; i < response.data().getShop().getArticles().getEdges().size(); i++) {
                                    ID mId = response.data().getShop().getArticles().getEdges().get(i).getNode().getId();
                                    String mTitle = response.data().getShop().getArticles().getEdges().get(i).getNode().getTitle();
                                    String mContent = response.data().getShop().getArticles().getEdges().get(i).getNode().getContent();
                                    String mContentHtml = response.data().getShop().getArticles().getEdges().get(i).getNode().getContentHtml();
                                    String mImage = response.data().getShop().getArticles().getEdges().get(i).getNode().getImage().getSrc();

                                    BlogArticleClass blogArticleClass = new BlogArticleClass();
                                    blogArticleClass.setmId(mId);
                                    blogArticleClass.setmTitle(mTitle);
                                    blogArticleClass.setmContent(mContent);
                                    blogArticleClass.setmContentHtml(mContentHtml);
                                    blogArticleClass.setmImage(mImage);
                                    blogArticleClassArrayList.add(blogArticleClass);
                                }

                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (blogArticleClassArrayList != null && blogArticleClassArrayList.size() > 0) {
                                            setAdapters();
                                            SetDataEmptyOrNot();
                                            Common.ProgressDialogDismiss();
                                        } else {
                                            Toast.makeText(context, getString(R.string.no_article_found), Toast.LENGTH_LONG).show();
                                            SetDataEmptyOrNot();
                                            Common.ProgressDialogDismiss();
                                        }
                                    }

                                });
                            } else {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Common.ProgressDialogDismiss();
                                        Toast.makeText(context, getString(R.string.no_article_found), Toast.LENGTH_LONG).show();
                                        SetDataEmptyOrNot();
                                    }
                                });
                            }
                        } else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Common.ProgressDialogDismiss();
                                    Toast.makeText(context, getString(R.string.no_article_found), Toast.LENGTH_LONG).show();
                                    SetDataEmptyOrNot();
                                }
                            });
                        }
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Common.ProgressDialogDismiss();
                                Toast.makeText(context, getString(R.string.no_article_found), Toast.LENGTH_LONG).show();
                                SetDataEmptyOrNot();
                            }
                        });
                    }
                }
            } catch (Exception e) {
                e.getStackTrace();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Common.ProgressDialogDismiss();
                        Toast.makeText(context, getString(R.string.no_article_found), Toast.LENGTH_LONG).show();
                        SetDataEmptyOrNot();
                    }
                });
            }
        }

        //Unable to resolve host "asdfsd": No address associated with hostname ,Failed to execute GraphQL http request
        @Override
        public void onFailure(@NonNull GraphError error) {
            error.getStackTrace();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Common.ProgressDialogDismiss();
                            Toast.makeText(context, getString(R.string.no_article_found), Toast.LENGTH_LONG).show();
                            SetDataEmptyOrNot();
                        }
                    }, 5000);
                }
            });
        }
    });
}

Single Article Details Query:

int PreviousHeight = 0, NewHeight = 0;
private void fetchData() {
    Common.ProgressDialogShow(context, getString(R.string.please_wait));
    Storefront.QueryRootQuery query = Storefront.query(rootQuery ->
            rootQuery.shop(shopQuery -> shopQuery.articles(args ->
                            args.query(mBlogArticleTitle)
                                    .first(Constant.CONSTANT_PAGE_SIZE_250),
                    articleConQuery -> articleConQuery.edges(queryEdges ->
                            queryEdges.cursor().node(queryDef ->
                                    queryDef
                                            .tags()
                                            .title()
                                            .publishedAt()
                                            .content()
                                            .contentHtml()
                                            .excerpt()
                                            .excerptHtml()
                                            .image(imgQueryDef ->
                                                    imgQueryDef
                                                            .src()
                                                            .id()
                                                            .altText())
                                            .blog(blogQueryDef -> blogQueryDef.title().url()))))));


    String shopUrl = AppPreference.getStringPref(this, AppPreference.PREF_SHOP,
            AppPreference.PREF_KEY.PREF_KEY_SHOP_DOMAIL);
    String shopifyApiKey = AppPreference.getStringPref(this, AppPreference.PREF_SHOP,
            AppPreference.PREF_KEY.PREF_KEY_API_KEY);

    OkHttpClient httpClient = new OkHttpClient.Builder()
            .addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(BuildConfig.OKHTTP_LOG_LEVEL))
            .build();

    graphClient = GraphClient.builder(this)
            .shopDomain(shopUrl)
            .accessToken(shopifyApiKey)
            .httpClient(httpClient)
            .httpCache(getCacheDir(), 1024 * 1024 * 10)
            .defaultHttpCachePolicy(HttpCachePolicy.CACHE_FIRST.expireAfter(1, TimeUnit.MINUTES))
            .build();

    graphClient.queryGraph(query).enqueue(new GraphCall.Callback<Storefront.QueryRoot>() {

        @Override
        public void onResponse(@NonNull GraphResponse<Storefront.QueryRoot> response) {
            try {
                if (response.data().getShop() != null) {
                    if (response.data().getShop().getArticles() != null) {
                        if (response.data().getShop().getArticles().getEdges().size() > 0) {
                            if (response.data().getShop().getArticles().getEdges().get(0).getNode() != null) {
                                for (int i = 0; i < response.data().getShop().getArticles().getEdges().size(); i++) {
                                    ID mId = response.data().getShop().getArticles().getEdges().get(i).getNode().getId();
                                    String mTitle = response.data().getShop().getArticles().getEdges().get(i).getNode().getTitle();
                                    String mContent = response.data().getShop().getArticles().getEdges().get(i).getNode().getContent();
                                    String mContentHtml = response.data().getShop().getArticles().getEdges().get(i).getNode().getContentHtml();
                                    String mImage = response.data().getShop().getArticles().getEdges().get(i).getNode().getImage().getSrc();
                                    DateTime mPublishAtDate = response.data().getShop().getArticles().getEdges().get(i).getNode().getPublishedAt();

                                    DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
                                    DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("MMMM dd, yyyy", Locale.ENGLISH);
                                    LocalDate date = LocalDate.parse(mPublishAtDate.toString(), inputFormatter);
                                    String formattedDate = outputFormatter.format(date);

                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            tv_blog_article_published_date.setText(formattedDate);

                                            tv_blog_article_title.setText(mTitle);

                                            DisplayMetrics displayMetrics = new DisplayMetrics();
                                            getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
                                            int DeviceWidth = displayMetrics.widthPixels;
                                            int DeviceHeight = displayMetrics.heightPixels;

                                            try {
                                                URL url = new URL(mImage);
                                                Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                                                int ImageWidth = bmp.getWidth();
                                                int ImageHeight = bmp.getHeight();

                                                NewHeight = (DeviceWidth * ImageHeight) / ImageWidth;

                                                Glide.with(context)
                                                        .load(mImage)
                                                        .asBitmap()
                                                        .placeholder(R.mipmap.ic_launcher_default)
                                                        .error(R.mipmap.ic_launcher_default)
                                                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                                                        .dontTransform()
                                                        .dontAnimate()
                                                        .into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
                                                            @Override
                                                            public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {

                                                                DisplayMetrics displayMetrics = new DisplayMetrics();
                                                                getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
                                                                int DeviceWidth = displayMetrics.widthPixels;
                                                                int DeviceHeight = displayMetrics.heightPixels;

                                                                int ImageWidth = bitmap.getWidth();
                                                                int ImageHeight = bitmap.getHeight();

                                                                int NewHeight = (DeviceWidth * ImageHeight) / ImageWidth;

                                                                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, NewHeight);
                                                                params.setMargins(0, 0, 0, 10);
                                                                params.addRule(RelativeLayout.BELOW, R.id.header);
                                                                rr_blog_article_image.setLayoutParams(params);
                                                                iv_blog_article_image.setImageBitmap(bitmap);
                                                            }
                                                        });


                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }
                                            updateDescription(mContentHtml);
                                        }
                                    });
                                }

                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (!mBlogArticleTitle.equals("")) {
                                            SetDataEmptyOrNot();
                                            Common.ProgressDialogDismiss();
                                        } else {
                                            Toast.makeText(context, getString(R.string.no_article_found), Toast.LENGTH_LONG).show();
                                            SetDataEmptyOrNot();
                                            Common.ProgressDialogDismiss();
                                        }
                                    }
                                });
                            } else {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Common.ProgressDialogDismiss();
                                        Toast.makeText(context, getString(R.string.no_article_found), Toast.LENGTH_LONG).show();
                                        SetDataEmptyOrNot();
                                    }
                                });
                            }
                        } else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Common.ProgressDialogDismiss();
                                    Toast.makeText(context, getString(R.string.no_article_found), Toast.LENGTH_LONG).show();
                                    SetDataEmptyOrNot();
                                }
                            });
                        }
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Common.ProgressDialogDismiss();
                                Toast.makeText(context, getString(R.string.no_article_found), Toast.LENGTH_LONG).show();
                                SetDataEmptyOrNot();
                            }
                        });
                    }
                }
            } catch (Exception e) {
                e.getStackTrace();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Common.ProgressDialogDismiss();
                        Toast.makeText(context, getString(R.string.no_article_found), Toast.LENGTH_LONG).show();
                        SetDataEmptyOrNot();
                    }
                });
            }
        }

        //Unable to resolve host "asdfsd": No address associated with hostname ,Failed to execute GraphQL http request
        @Override
        public void onFailure(@NonNull GraphError error) {
            error.getStackTrace();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Common.ProgressDialogDismiss();
                            Toast.makeText(context, getString(R.string.no_article_found), Toast.LENGTH_LONG).show();
                            SetDataEmptyOrNot();
                        }
                    }, 5000);
                }
            });
        }
    });
}

Hope this may helps you :)