如何使用来自 mobile-buy-sdk-android 的查询获取博客列表
How to get a Blog-List using query from mobile-buy-sdk-android
我使用 移动购买 SDK 为 android 和 移动购买创建了 android 和 ios 电子商务应用程序ios.
的 SDK
我的问题是如何从 SDK 获取 Blog-List 我需要显示 Blog-List 然后点击博客列表显示文章 那么有没有人使用 mobile-buy-sdk-android?
中的查询和获取块实现了这种类型的功能
这是我用于 android 的 SDK:SDK
参考:
- https://help.shopify.com/en/api/reference/online-store/blog#
- https://help.shopify.com/en/api/storefront-api/reference/object/blog
- https://help.shopify.com/en/api/reference/online-store/article
获取如下图所示的属性,但无法找到任何有关如何调用查询和获取阻止列表的代码片段。
我终于找到了一个通过编写函数获取博客列表和文章列表以及文章详细信息的解决方案。我发在这里是因为它对其他人有帮助。
博客列表查询:
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);
}
});
}
});
}
文章列表查询:
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);
}
});
}
});
}
单篇文章详情查询:
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);
}
});
}
});
}
希望这对您有所帮助:)
我使用 移动购买 SDK 为 android 和 移动购买创建了 android 和 ios 电子商务应用程序ios.
的 SDK我的问题是如何从 SDK 获取 Blog-List 我需要显示 Blog-List 然后点击博客列表显示文章 那么有没有人使用 mobile-buy-sdk-android?
中的查询和获取块实现了这种类型的功能这是我用于 android 的 SDK:SDK
参考:
- https://help.shopify.com/en/api/reference/online-store/blog#
- https://help.shopify.com/en/api/storefront-api/reference/object/blog
- https://help.shopify.com/en/api/reference/online-store/article
获取如下图所示的属性,但无法找到任何有关如何调用查询和获取阻止列表的代码片段。
我终于找到了一个通过编写函数获取博客列表和文章列表以及文章详细信息的解决方案。我发在这里是因为它对其他人有帮助。
博客列表查询:
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);
}
});
}
});
}
文章列表查询:
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);
}
});
}
});
}
单篇文章详情查询:
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);
}
});
}
});
}
希望这对您有所帮助:)