【发布时间】:2020-09-25 17:22:14
【问题描述】:
我正在为我的 Wordpress 网站开发一个应用程序。我正在使用 REST API 和 RETROFIT 库。我遇到了一个错误,显示您必须登录才能发表评论。但在网页版中,我不需要登录才能发表评论。 在下面找到我的代码。
使用 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";
}
【问题讨论】:
标签: android wordpress android-studio android-fragments retrofit