团队开发冲刺第八天(实现评论功能)
昨天在设计评论的UI,以及查阅网址看看如何添加评论
今天完成了评论的功能
遇到的问题:多线程并发的问题,导致第一次输出评论没有及时的显示在屏幕上,之后在进入该页面时,之前的数据会少一条
明天解决这个bug
明天要完成,点赞,收藏,关注等功能。
shownewsActivity.java
package com.example.newbsh.UI.home.hometype.news.shownews; import androidx.annotation.NonNull; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.content.Context; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.method.ScrollingMovementMethod; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; import com.example.newbsh.HttpUtil.HttpUtil; import com.example.newbsh.MainActivity; import com.example.newbsh.R; import com.example.newbsh.UI.home.hometype.news.shownews.comment.BlogComment; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.lang.ref.WeakReference; import java.sql.Driver; public class ShowNewsActivity extends AppCompatActivity { TextView textViewtitle,textViewdoc,textViewdate; LinearLayout piclinearLayout,commentLayout; TextView msg; Button send; private String title; private String username= MainActivity.getUserName(); private ShowBlogHandler blogHandler; String url; @RequiresApi(api = Build.VERSION_CODES.M) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show_news); blogHandler = new ShowBlogHandler(this); init(); showComment(); } private void init() { textViewtitle = findViewById(R.id.NewsTitle); textViewdoc = findViewById(R.id.Newsdoc); textViewdate = findViewById(R.id.Newsdate); piclinearLayout=findViewById(R.id.picturelinearlayout); commentLayout=findViewById(R.id.comment_layout); textViewdoc.setMovementMethod(new ScrollingMovementMethod()); msg=findViewById(R.id.msg); send=findViewById(R.id.send); initdoc(); send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String comment=msg.getText().toString(); if(comment.isEmpty()){ Toast.makeText(ShowNewsActivity.this, "发送内容不能为空", Toast.LENGTH_SHORT).show(); return; } writeComment(comment); //开启线程 } }); } private void writeComment(String comment) { new Thread(new Runnable() { @Override public void run() { String reqdata="http://39.97.181.86/BaiXiaoSheng/sendnewscomment?username="+username+"&comment="+comment+"&title="+title; String json=String.valueOf(HttpUtil.sendPost(reqdata,"")); Message msg=new Message(); msg.what=1000; msg.obj=json; blogHandler.sendMessage(msg); Log.d("writecomment",json); } }).start(); } private void showComment() { new Thread(new Runnable() { @Override public void run() { Log.d("data", "评论"); String requrl="http://39.97.181.86/BaiXiaoSheng/findallcomments?title="+title; String json = String.valueOf(HttpUtil.sendPost(requrl, "")); Message msg = new Message(); msg.what = 600; msg.obj = json; blogHandler.sendMessage(msg); Log.d("评论", json); } }).start(); } /* 加载新闻文章 */ private void initdoc() { Bundle bundle = getIntent().getExtras(); /* 获取图片的链接 */ url=bundle.getString("url"); String []urls=url.split(" "); /* 设置文章的内容 */ if (bundle != null) { title=bundle.getString("title"); textViewtitle.setText(bundle.getString("title")); textViewdoc.setText(bundle.getString("doc")); textViewdate.setText("文章发布时间为:"+bundle.getString("date")); } else { Log.i("bundle", "为空。。。。。。"); } /* 动态加载图片 */ RequestOptions options = new RequestOptions() .error(R.drawable.error) .placeholder(R.drawable.loading); for(int i=0;i<urls.length;i++) { ImageView imageView=new ImageView(this); Glide.with(this) .load(urls[i]) .apply(options) .into(imageView); LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); layout.setMargins(0, 10, 0, 0); imageView.setLayoutParams(layout); piclinearLayout.addView(imageView); } } static class ShowBlogHandler extends Handler { //防止内存泄漏 private final ShowNewsActivity mcontext; ShowBlogHandler(ShowNewsActivity activity) { WeakReference<ShowNewsActivity> weakReference = new WeakReference<>(activity); mcontext = weakReference.get(); } @SuppressLint("SetTextI18n") @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); switch (msg.what) { case 600: try { JSONArray comments = new JSONArray(msg.obj.toString()); Log.i("length:", ""+comments.length()); for (int i = 0; i < comments.length(); i++) { JSONObject object = comments.getJSONObject(i); BlogComment blogComment = new BlogComment(mcontext); if (!object.isNull("imgurl")) { Glide.with(mcontext) .load(object.getString("imgurl")) .circleCrop() .into(blogComment.getImageView()); } blogComment.getName().setText(object.getString("username")); blogComment.getComment().setText(object.getString("comment")); blogComment.getDate().setText(object.getString("date")); Log.i("次数:", ""+object.getString("comment")); mcontext.commentLayout.addView(blogComment,0); } } catch (JSONException e) { e.printStackTrace(); } break; case 1000: try { JSONObject object=new JSONObject(msg.obj.toString()); BlogComment blogComment=new BlogComment(mcontext); blogComment.getDate().setText(object.getString("date")); blogComment.getComment().setText(object.getString("comment")); blogComment.getName().setText(object.getString("username")); if(!object.isNull("imgurl")){ if(object.getString("imgurl").length()>0) Glide.with(mcontext) .load(object.getString("imgurl")) .circleCrop() .into(blogComment.getImageView()); } mcontext.commentLayout.addView(blogComment,0); mcontext.msg.setText(""); mcontext.msg.clearFocus(); InputMethodManager imm= (InputMethodManager) mcontext.getSystemService(Context.INPUT_METHOD_SERVICE); //键盘收缩 if (imm != null) { imm.hideSoftInputFromWindow(mcontext.msg.getWindowToken(),0); } Toast toast = Toast.makeText(mcontext, "评论成功", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } catch (JSONException e) { e.printStackTrace(); } break; default: break; } } } }
Blogcomment.java
package com.example.newbsh.UI.home.hometype.news.shownews.comment; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import androidx.constraintlayout.widget.ConstraintLayout; import com.example.newbsh.R; public class BlogComment extends ConstraintLayout { private ImageView imageView; private TextView name; private TextView date; private TextView comment; public BlogComment(Context context) { super(context); LayoutInflater inflater= LayoutInflater.from(context); View view=inflater.inflate(R.layout.shownews_comment_layout,this); imageView=view.findViewById(R.id.comment_userimg); name=view.findViewById(R.id.comment_name); date=view.findViewById(R.id.comment_date); comment=view.findViewById(R.id.comment_comment); } public ImageView getImageView() { return imageView; } public TextView getName() { return name; } public TextView getDate() { return date; } public TextView getComment() { return comment; } }