如何通过json POST方法在android应用程序中完美实现注释选项
我有一个与新闻相关的json api url,我已经实现了对url的评论发布,但没有在textview中的同一个新闻id中显示响应中发布的参数评论。我也使用了共享pref,但效果不好。请任何人帮助我。谢谢。这是我的代码:如何通过json POST方法在android应用程序中完美实现注释选项,android,comments,Android,Comments,我有一个与新闻相关的json api url,我已经实现了对url的评论发布,但没有在textview中的同一个新闻id中显示响应中发布的参数评论。我也使用了共享pref,但效果不好。请任何人帮助我。谢谢。这是我的代码: holder.btnpost.setOnClickListener( view -> { String comment = holder.inputcomment.getText().toString();
holder.btnpost.setOnClickListener( view -> {
String comment = holder.inputcomment.getText().toString();
sp = new SharedPreferencesHandler( context );
session = new SessionManager( this.context );
if (!comment.isEmpty() && session.isLoggedIn()) {
commentPost( comment );
} else if (!session.isLoggedIn()) {
Toast.makeText( context, "Please Sign in first!", Toast.LENGTH_LONG ).show();
} else {
Toast.makeText( context, "Please write something!", Toast.LENGTH_LONG ).show();
}
} );
//post method
private void commentPost(final String comment) {
sp = new SharedPreferencesHandler( context );
commentDataList = new ArrayList<>();
String tag_comments = "comment";
StringRequest stringRequest = new StringRequest( Request.Method.POST, commentURL + currentId, response -> {
Log.d( "TAG", "response = " + response );
if (response != null) {`enter code here`
try {
JSONObject jsonResponse = new JSONObject( response );
JSONArray jsonArray = jsonResponse.getJSONArray( "success" );
if (jsonArray != null && jsonArray.length() > 0) {
for (int i = 0; i < jsonArray.length(); i++) {`enter code here`
JSONObject dataOjbect = jsonArray.getJSONObject( i );
String mComment = dataOjbect.getString( "comment" );
Comment mCommentData = new Comment( mComment );
commentDataList.add( mCommentData );
displayComment.setText( mCommentData.getComment() );
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Toast.makeText( context, "Commented", Toast.LENGTH_LONG ).show();
}
},
error -> {
Log.d( "TAG", "Error = " + error );
Toast.makeText( context, "No comment posted", Toast.LENGTH_LONG ).show();
} )
{
@Override
public Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put( "comment", comment );
return params;
}
@Override
public Map<String, String> getHeaders() {
SharedPreferences mPrefs = context.getSharedPreferences( "radiobanglany", Context.MODE_PRIVATE );
Map<String, String> headers = new HashMap<>();
headers.put( "Accept", "application/json" );
headers.put( "Authorization",
"Bearer"+mPrefs.getString("token", "" ) );
return headers;
}
};
发布您尝试过的代码?以及输出中实际需要的内容?@userI ok我编辑了我的问题。
}