无法 post 在 android 应用程序中对我的 wordpress 网站发表评论?
Not able to post comment in android app to my wordpress site?
我正在为我的 Wordpress 网站开发一个应用程序。我正在使用 REST API & RETROFIT 库。我遇到了一个错误,显示您必须登录 post 才能发表评论。但是在网络版中,我不需要登录post评论。
在下面找到我的代码。
Activity 使用 Dialog Fragment 写评论
WriteACommentFragment.java
public class WriteACommentFragment extends DialogFragment {
private int clickedPostId;
private int commentId;
private EditText edtAuthorName, edtAuthorEmail, edtAuthorComment;
private Activity mActivity;
private boolean commentSuccessful = false;
private String authorName;
private String authorEmail;
private String authorComment;
public static interface OnCompleteListener {
public abstract void onComplete(Boolean isCommentSuccessful, CommentsAndReplies commentsAndReplies);
}
private OnCompleteListener mListener;
public static WriteACommentFragment newInstance(int clickedPostId, int commentId) {
Bundle args = new Bundle();
args.putInt(AppConstant.ARG_CLICKED_POST_ID, clickedPostId);
args.putInt(AppConstant.ARG_COMMENT_ID, commentId);
WriteACommentFragment fragment = new WriteACommentFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.mActivity = activity;
try {
this.mListener = (OnCompleteListener) activity;
} catch (final ClassCastException e) {
throw new ClassCastException(activity.toString());
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View rootView = LayoutInflater.from(mActivity).inflate(R.layout.fragment_dialog_comment, null);
initVar();
initView(rootView);
initFunctionality();
return new AlertDialog.Builder(mActivity)
.setView(rootView)
.setTitle(R.string.write_a_comment)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
authorName = edtAuthorName.getText().toString().trim();
authorEmail = edtAuthorEmail.getText().toString().trim();
authorComment = edtAuthorComment.getText().toString().trim();
AppPreference.getInstance(mActivity).setString(PrefKey.KEY_EMAIL, authorEmail);
AppPreference.getInstance(mActivity).setString(PrefKey.KEY_NAME, authorName);
if (commentId == AppConstant.THIS_IS_COMMENT) {
sendComment(authorName, authorEmail, authorComment);
} else {
sendReply(authorName, authorEmail, authorComment);
}
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (dialogInterface != null) {
dialogInterface.dismiss();
}
}
})
.create();
}
public void initVar() {
Bundle bundle = getArguments();
if (bundle != null) {
clickedPostId = getArguments().getInt(AppConstant.ARG_CLICKED_POST_ID);
commentId = getArguments().getInt(AppConstant.ARG_COMMENT_ID);
}
}
public void initView(View rootView) {
edtAuthorName = (EditText) rootView.findViewById(R.id.edt_author_name);
edtAuthorEmail = (EditText) rootView.findViewById(R.id.edt_author_email);
edtAuthorComment = (EditText) rootView.findViewById(R.id.edt_author_comment);
}
public void initFunctionality() {
edtAuthorName.setText(AppPreference.getInstance(mActivity).getString(PrefKey.KEY_NAME));
edtAuthorEmail.setText(AppPreference.getInstance(mActivity).getString(PrefKey.KEY_EMAIL));
}
private void sendReply(String authName, String authEmail, String authComment) {
ApiUtils.getApiInterface().postAReply(authName, authEmail, authComment, clickedPostId, commentId).enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
commentSuccessful = true;
showReturnMessage(mActivity.getString(R.string.successful_reply));
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
t.printStackTrace();
showReturnMessage(mActivity.getString(R.string.error_message));
}
});
}
private void sendComment(String authName, String authEmail, String authComment) {
ApiUtils.getApiInterface().postAComment(authName, authEmail, authComment, clickedPostId).enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
commentSuccessful = true;
showReturnMessage(mActivity.getString(R.string.successful_comment));
} else {
try {
JSONObject jObjError = new JSONObject(response.errorBody().string());
showReturnMessage(jObjError.getString(AppConstant.COMMENT_MESSAGE));
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
t.printStackTrace();
showReturnMessage(mActivity.getString(R.string.error_message));
}
});
}
public void showReturnMessage(String messageText) {
Toast.makeText(mActivity, messageText, Toast.LENGTH_SHORT).show();
if (authorName.isEmpty()) {
authorName = mActivity.getString(R.string.author_name);
}
CommentsAndReplies commentsAndReplies = new CommentsAndReplies(
AppConstant.COMMENT_ID,
AppConstant.COMMENT_PARENT_ID,
authorName,
new SimpleDateFormat(AppConstant.COMMENT_DATE_FORMAT).format(Calendar.getInstance().getTime()),
new Content(authorComment),
new AuthorAvaterUrl(null));
mListener.onComplete(commentSuccessful, commentsAndReplies);
}
}
所有应用程序常量都在此文件中声明
AppConstant.java
public class AppConstant {
public static final String EMPTY = " ";
public static final String BUNDLE_KEY_TITLE = "title";
public static final String BUNDLE_KEY_MESSAGE = "message";
public static final String BUNDLE_KEY_URL = "url";
public static final String BUNDLE_KEY_POST_ID = "post_id";
public static final String BUNDLE_KEY_POST = "post";
public static final String BUNDLE_KEY_ALL_COMMENT = "all_comment";
public static final String BUNDLE_KEY_ALL_ZERO_PARENT_COMMENT = "zero_parent_comments";
public static final String BUNDLE_KEY_CLICKED_COMMENT = "clicked_comment";
public static final String BUNDLE_KEY_DIALOG_FRAGMENT = "dialog_fragment";
public static final String BUNDLE_KEY_SHOULD_DIALOG_OPEN = "should_dialog_open";
public static final int THIS_IS_COMMENT = -1;
public static final int REQUEST_CODE_COMMENT = 0;
public static final String BUNDLE_KEY_COMMENT_STATUS = "comment_success_status";
public static final int DEFAULT_CATEGORY_ID = -1;
public static final int MAX_VALUE = 999;
public static final String DOT = ".";
public static final String ARG_CLICKED_POST_ID = "clicked_post_id";
public static final String ARG_COMMENT_ID = "comment_id";
public static final String COMMENT_MESSAGE = "message";
public static final String COMMENT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
public static final Double COMMENT_ID = 1000.0;
public static final Double COMMENT_PARENT_ID = 0.0;
public static final int DEFAULT_PAGE = 1;
}
我的应用偏好键
PrefKey.java
public class PrefKey {
public static final String APP_PREF_NAME = "METO";
public static final String KEY_EMAIL = "email";
public static final String KEY_NAME = "name";
}
宾果!!!
找到了解决我的问题的方法。
我试图使用 WP REST API 匿名发表评论,但受到限制。
我的 android 代码没有错误。
我在 WordPress 当前主题的 function.php 文件
中做了修改
function filter_rest_allow_anonymous_comments() {
return true;
}
add_filter('rest_allow_anonymous_comments','filter_rest_allow_anonymous_comments');
在 function.php 中添加了上面的 PHP 代码,用于过滤匿名评论的 WP REST 请求。
一切正常。
我正在为我的 Wordpress 网站开发一个应用程序。我正在使用 REST API & RETROFIT 库。我遇到了一个错误,显示您必须登录 post 才能发表评论。但是在网络版中,我不需要登录post评论。 在下面找到我的代码。
Activity 使用 Dialog Fragment 写评论
WriteACommentFragment.java
public class WriteACommentFragment extends DialogFragment {
private int clickedPostId;
private int commentId;
private EditText edtAuthorName, edtAuthorEmail, edtAuthorComment;
private Activity mActivity;
private boolean commentSuccessful = false;
private String authorName;
private String authorEmail;
private String authorComment;
public static interface OnCompleteListener {
public abstract void onComplete(Boolean isCommentSuccessful, CommentsAndReplies commentsAndReplies);
}
private OnCompleteListener mListener;
public static WriteACommentFragment newInstance(int clickedPostId, int commentId) {
Bundle args = new Bundle();
args.putInt(AppConstant.ARG_CLICKED_POST_ID, clickedPostId);
args.putInt(AppConstant.ARG_COMMENT_ID, commentId);
WriteACommentFragment fragment = new WriteACommentFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.mActivity = activity;
try {
this.mListener = (OnCompleteListener) activity;
} catch (final ClassCastException e) {
throw new ClassCastException(activity.toString());
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View rootView = LayoutInflater.from(mActivity).inflate(R.layout.fragment_dialog_comment, null);
initVar();
initView(rootView);
initFunctionality();
return new AlertDialog.Builder(mActivity)
.setView(rootView)
.setTitle(R.string.write_a_comment)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
authorName = edtAuthorName.getText().toString().trim();
authorEmail = edtAuthorEmail.getText().toString().trim();
authorComment = edtAuthorComment.getText().toString().trim();
AppPreference.getInstance(mActivity).setString(PrefKey.KEY_EMAIL, authorEmail);
AppPreference.getInstance(mActivity).setString(PrefKey.KEY_NAME, authorName);
if (commentId == AppConstant.THIS_IS_COMMENT) {
sendComment(authorName, authorEmail, authorComment);
} else {
sendReply(authorName, authorEmail, authorComment);
}
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (dialogInterface != null) {
dialogInterface.dismiss();
}
}
})
.create();
}
public void initVar() {
Bundle bundle = getArguments();
if (bundle != null) {
clickedPostId = getArguments().getInt(AppConstant.ARG_CLICKED_POST_ID);
commentId = getArguments().getInt(AppConstant.ARG_COMMENT_ID);
}
}
public void initView(View rootView) {
edtAuthorName = (EditText) rootView.findViewById(R.id.edt_author_name);
edtAuthorEmail = (EditText) rootView.findViewById(R.id.edt_author_email);
edtAuthorComment = (EditText) rootView.findViewById(R.id.edt_author_comment);
}
public void initFunctionality() {
edtAuthorName.setText(AppPreference.getInstance(mActivity).getString(PrefKey.KEY_NAME));
edtAuthorEmail.setText(AppPreference.getInstance(mActivity).getString(PrefKey.KEY_EMAIL));
}
private void sendReply(String authName, String authEmail, String authComment) {
ApiUtils.getApiInterface().postAReply(authName, authEmail, authComment, clickedPostId, commentId).enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
commentSuccessful = true;
showReturnMessage(mActivity.getString(R.string.successful_reply));
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
t.printStackTrace();
showReturnMessage(mActivity.getString(R.string.error_message));
}
});
}
private void sendComment(String authName, String authEmail, String authComment) {
ApiUtils.getApiInterface().postAComment(authName, authEmail, authComment, clickedPostId).enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
commentSuccessful = true;
showReturnMessage(mActivity.getString(R.string.successful_comment));
} else {
try {
JSONObject jObjError = new JSONObject(response.errorBody().string());
showReturnMessage(jObjError.getString(AppConstant.COMMENT_MESSAGE));
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
t.printStackTrace();
showReturnMessage(mActivity.getString(R.string.error_message));
}
});
}
public void showReturnMessage(String messageText) {
Toast.makeText(mActivity, messageText, Toast.LENGTH_SHORT).show();
if (authorName.isEmpty()) {
authorName = mActivity.getString(R.string.author_name);
}
CommentsAndReplies commentsAndReplies = new CommentsAndReplies(
AppConstant.COMMENT_ID,
AppConstant.COMMENT_PARENT_ID,
authorName,
new SimpleDateFormat(AppConstant.COMMENT_DATE_FORMAT).format(Calendar.getInstance().getTime()),
new Content(authorComment),
new AuthorAvaterUrl(null));
mListener.onComplete(commentSuccessful, commentsAndReplies);
}
}
所有应用程序常量都在此文件中声明
AppConstant.java
public class AppConstant {
public static final String EMPTY = " ";
public static final String BUNDLE_KEY_TITLE = "title";
public static final String BUNDLE_KEY_MESSAGE = "message";
public static final String BUNDLE_KEY_URL = "url";
public static final String BUNDLE_KEY_POST_ID = "post_id";
public static final String BUNDLE_KEY_POST = "post";
public static final String BUNDLE_KEY_ALL_COMMENT = "all_comment";
public static final String BUNDLE_KEY_ALL_ZERO_PARENT_COMMENT = "zero_parent_comments";
public static final String BUNDLE_KEY_CLICKED_COMMENT = "clicked_comment";
public static final String BUNDLE_KEY_DIALOG_FRAGMENT = "dialog_fragment";
public static final String BUNDLE_KEY_SHOULD_DIALOG_OPEN = "should_dialog_open";
public static final int THIS_IS_COMMENT = -1;
public static final int REQUEST_CODE_COMMENT = 0;
public static final String BUNDLE_KEY_COMMENT_STATUS = "comment_success_status";
public static final int DEFAULT_CATEGORY_ID = -1;
public static final int MAX_VALUE = 999;
public static final String DOT = ".";
public static final String ARG_CLICKED_POST_ID = "clicked_post_id";
public static final String ARG_COMMENT_ID = "comment_id";
public static final String COMMENT_MESSAGE = "message";
public static final String COMMENT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
public static final Double COMMENT_ID = 1000.0;
public static final Double COMMENT_PARENT_ID = 0.0;
public static final int DEFAULT_PAGE = 1;
}
我的应用偏好键 PrefKey.java
public class PrefKey {
public static final String APP_PREF_NAME = "METO";
public static final String KEY_EMAIL = "email";
public static final String KEY_NAME = "name";
}
宾果!!! 找到了解决我的问题的方法。 我试图使用 WP REST API 匿名发表评论,但受到限制。 我的 android 代码没有错误。 我在 WordPress 当前主题的 function.php 文件
中做了修改function filter_rest_allow_anonymous_comments() {
return true;
}
add_filter('rest_allow_anonymous_comments','filter_rest_allow_anonymous_comments');
在 function.php 中添加了上面的 PHP 代码,用于过滤匿名评论的 WP REST 请求。
一切正常。