今天大致完成了社区功能,明天做详细的优化,改改bug,然后写写作业。
1、社区主界面(已完成)
2、帖子详情,包括点赞评论回复的实现(明日待办)
3、发帖界面(已完成)
4、将贴子信息传输至数据库(已完成)
5、从数据库读取帖子信息并展示(已完成)
6、从数据库读取帖子的相关评论及回复(明日待办)
团队博客地址:https://www.cnblogs.com/NSnake/
今日代码
package com.example.xiaoyi_test_2.Activity; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import com.example.xiaoyi_test_2.Bean.SocialBean; import com.example.xiaoyi_test_2.R; import com.example.xiaoyi_test_2.View.GoodView; public class CompDetailActivity extends AppCompatActivity { private TextView tv_name,tv_time,tv_title,tv_desc,tv_num; private ImageView im_1,im_2,im_3,im_dz; private SocialBean socialBean; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_comp_detail); tv_name=findViewById(R.id.cd_name); tv_time=findViewById(R.id.cd_time); tv_title=findViewById(R.id.cd_title); tv_desc=findViewById(R.id.cd_desc); tv_num=findViewById(R.id.cd_im_goodnum); im_1=findViewById(R.id.cd_img_1); im_2=findViewById(R.id.cd_img_2); im_3=findViewById(R.id.cd_img_3); im_dz=findViewById(R.id.cd_im_good); tv_time=findViewById(R.id.cd_time); socialBean= (SocialBean) getIntent().getExtras().getSerializable("soc"); tv_time.setText(socialBean.getTime()); tv_desc.setText(socialBean.getDescrib()); tv_name.setText(socialBean.getName()); tv_title.setText(socialBean.getTitle()); tv_num.setText(socialBean.getGoodnum()); String[] imgs=socialBean.getImgpath().split("|"); if (!socialBean.getImg1().equals("")&&socialBean.getImg1()!=null){ Glide.with(CompDetailActivity.this).load(socialBean.getImg1()).into(im_1); }else { im_1.setVisibility(View.INVISIBLE); } if (!socialBean.getImg2().equals("")&&socialBean.getImg2()!=null){ Glide.with(CompDetailActivity.this).load(socialBean.getImg2()).into(im_2); }else { im_2.setVisibility(View.INVISIBLE); } if (!socialBean.getImg3().equals("")&&socialBean.getImg3()!=null){ Glide.with(CompDetailActivity.this).load(socialBean.getImg3()).into(im_3); }else { im_3.setVisibility(View.INVISIBLE); } im_dz.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { GoodView goodView=new GoodView(CompDetailActivity.this); goodView.setImage(R.drawable.dz_pink); goodView.show(im_dz); } }); } } package com.example.xiaoyi_test_2.Activity; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.Toast; import com.example.xiaoyi_test_2.Bean.SocialBean; import com.example.xiaoyi_test_2.R; import com.example.xiaoyi_test_2.Service.AddComp; import com.example.xiaoyi_test_2.Service.ItemService; import com.example.xiaoyi_test_2.Service.ZipService; import com.example.xiaoyi_test_2.Utils.SPUtils; import java.io.File; public class AddSocialActivity extends AppCompatActivity implements ZipService.SendZipd { private Button btn_retrun,btn_out; private ImageView im_1,im_2,im_3; private EditText edit_title,edit_describe; public ProgressBar progressBar; private int clickone; private ImageView clickimg; private SocialBean socialBean; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_social); initView(); } private void initView() { btn_out=findViewById(R.id.addsoc_btn_out); btn_retrun=findViewById(R.id.addsoc_btn_return); im_1=findViewById(R.id.addsoc_img); im_2=findViewById(R.id.addsoc_img_2); im_3=findViewById(R.id.addsoc_img_3); edit_title=findViewById(R.id.addsoc_et_title); edit_describe=findViewById(R.id.addsoc_et_describe); progressBar=findViewById(R.id.addsoc_progers); progressBar.setVisibility(View.INVISIBLE); btn_out.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { progressBar.setVisibility(View.VISIBLE); socialBean=new SocialBean(); socialBean.setName((String) SPUtils.get(AddSocialActivity.this,"username","11")); socialBean.setHead((String) SPUtils.get(AddSocialActivity.this,"head","11")); socialBean.setDescrib(edit_describe.getText().toString()); socialBean.setTitle(edit_title.getText().toString()); socialBean.setUserid((Integer) SPUtils.get(AddSocialActivity.this,"userid",1)); socialBean.save(AddSocialActivity.this); } }); im_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, 1); clickone = 1; clickimg = im_1; } }); im_2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, 1); clickone = 2; clickimg = im_2; } }); im_3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, 1); clickone = 3; clickimg = im_3; } }); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); //获取图片路径 if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) { Uri selectedImage = data.getData(); String[] filePathColumns = {MediaStore.Images.Media.DATA}; Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null); c.moveToFirst(); int columnIndex = c.getColumnIndex(filePathColumns[0]); String imagePath = c.getString(columnIndex); c.close(); ZipService.zipPhoto(AddSocialActivity.this,imagePath,AddSocialActivity.this); } else if (requestCode == 1 && resultCode == Activity.RESULT_CANCELED) { Toast.makeText(AddSocialActivity.this, "取消选择", Toast.LENGTH_SHORT).show(); } } @Override public void sendZip(File file) { clickimg.setImageURI(Uri.parse(file.getPath())); if (clickone==1){ socialBean.setImg1(file); } if (clickone==2){ socialBean.setImg2(file); } if (clickone==3){ socialBean.setImg3(file); } } } package com.example.xiaoyi_test_2.Service; import android.content.Context; import android.content.Intent; import android.util.Log; import android.view.View; import android.widget.Toast; import com.alibaba.fastjson.JSONObject; import com.example.xiaoyi_test_2.Activity.AddSocialActivity; import com.example.xiaoyi_test_2.Bean.SayToItemBean; import com.example.xiaoyi_test_2.Bean.SocialBean; import com.example.xiaoyi_test_2.Utils.Base36Utils; import com.example.xiaoyi_test_2.Utils.RangeUtil; import com.example.xiaoyi_test_2.Utils.SPUtils; import java.io.IOException; import java.util.ArrayList; import java.util.List; import okhttp3.Call; import okhttp3.Callback; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; public class AddComp { public static void Acomp(SocialBean socialBean, final Context context){ String imgpath = ""; if (socialBean.getImg1() != null) { String s = Base36Utils.imageToBase64(socialBean.getImg1()); MediaType mediaType = MediaType.parse("text/x-markdown; charset=utf-8"); String request_body = s; String name = (String) SPUtils.get(context, "phone", "") + RangeUtil.getRange(); imgpath += ("http://123.56.137.134/photo_XY/" + name + ".jpg|"); Request request = new Request.Builder() .url("http://123.56.137.134/XiaoYiServer/AddPhoto") .addHeader("name", name) .post(RequestBody.create(mediaType, request_body)).build(); OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.d("LogAndRegister-------", "phone查询请求失败"); } @Override public void onResponse(Call call, Response response) throws IOException { //response.body只能调一次 } }); socialBean.setImg1(null); } if (socialBean.getImg2() != null) { String s = Base36Utils.imageToBase64(socialBean.getImg2()); MediaType mediaType = MediaType.parse("text/x-markdown; charset=utf-8"); String request_body = s; String name = (String) SPUtils.get(context, "phone", "") + RangeUtil.getRange(); imgpath += ("http://123.56.137.134/photo_XY/" + name + ".jpg|"); Request request = new Request.Builder(). url("http://123.56.137.134/XiaoYiServer/AddPhoto") .addHeader("name", name) .post(RequestBody.create(mediaType, request_body)).build(); OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.d("LogAndRegister-------", "phone查询请求失败"); } @Override public void onResponse(Call call, Response response) throws IOException { //response.body只能调一次 } }); socialBean.setImg2(null); } if (socialBean.getImg3() != null) { String s = Base36Utils.imageToBase64(socialBean.getImg3()); MediaType mediaType = MediaType.parse("text/x-markdown; charset=utf-8"); String request_body = s; String name = (String) SPUtils.get(context, "phone", "") + RangeUtil.getRange(); imgpath += ("http://123.56.137.134/photo_XY/" + name + ".jpg|"); Request request = new Request.Builder(). url("http://123.56.137.134/XiaoYiServer/AddPhoto") .addHeader("name", name) .post(RequestBody.create(mediaType, request_body)).build(); OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.d("itemService-------", "存储失败!"); } @Override public void onResponse(Call call, Response response) throws IOException { //response.body只能调一次 } }); socialBean.setImg3(null); } socialBean.setImgpath(imgpath); OkHttpClient okHttpClient=new OkHttpClient(); MediaType mediaType=MediaType.parse("application/json; charset=utf-8"); String request_body=JSONObject.toJSONString(socialBean); Request request=new Request.Builder(). url("http://123.56.137.134/XiaoYiServer/AddComp") .post(RequestBody.create(mediaType,request_body)).build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { //response.body只能调一次 String body=response.body().string(); Log.d("IC-------",body); ((AddSocialActivity)context).runOnUiThread(new Runnable() { @Override public void run() { ((AddSocialActivity)context).progressBar.setVisibility(View.INVISIBLE); Toast.makeText(context, "发布成功!", Toast.LENGTH_SHORT).show(); Intent intent=new Intent(); ((AddSocialActivity)context).setResult(0,intent); } }); } }); } public static void getComp(final SendSC sendSC){ OkHttpClient okHttpClient=new OkHttpClient(); MediaType mediaType=MediaType.parse("text/x-markdown; charset=utf-8"); Request request=new Request.Builder(). url("http://123.56.137.134/XiaoYiServer/GetComp") .post(RequestBody.create(mediaType,"1")).build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { //response.body只能调一次 String body=response.body().string(); Log.d("IC-------",body); List<SocialBean> list= com.alibaba.fastjson.JSONObject.parseArray(body,SocialBean.class); sendSC.sendSC(list); } }); } public interface SendSC{ public void sendSC(List<SocialBean> list); } } package com.main.java; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.alibaba.fastjson.JSONArray; import bean.SocialBean; import utils.DBUtil; @WebServlet("/GetComp") public class GetComp extends HttpServlet { private static final long serialVersionUID = 1L; public GetComp() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ArrayList<SocialBean> list=new ArrayList<SocialBean>(); try { Connection con=DBUtil.lin("XiaoYi"); Statement stmt=con.createStatement(); String sql="select * from socialitem inner join user on socialitem.userid=user.id"; ResultSet rsst=stmt.executeQuery(sql); while(rsst.next()) { SocialBean socialBean=new SocialBean(); socialBean.setUserid(rsst.getInt("userid")); socialBean.setCompnum(rsst.getInt("compnum")); socialBean.setDescrib(rsst.getString("describ")); socialBean.setGoodnum(rsst.getInt("goodnum")); socialBean.setImgpath(rsst.getString("imgpath")); socialBean.setTitle(rsst.getString("title")); socialBean.setTime(""+rsst.getDate("date")); socialBean.setName(rsst.getString("name")); socialBean.setHead(rsst.getString("head")); list.add(socialBean); } rsst.close(); stmt.close(); con.close(); response.setCharacterEncoding("utf-8"); response.getWriter().print(JSONArray.toJSONString(list)); response.getWriter().close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } package com.main.java; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import utils.DBUtil; @WebServlet("/AddComp") public class AddComp extends HttpServlet { private static final long serialVersionUID = 1L; public AddComp() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); InputStream is=request.getInputStream(); InputStreamReader ir=new InputStreamReader(is); BufferedReader br=new BufferedReader(ir); StringBuffer sb=new StringBuffer(); String s=""; while((s=br.readLine())!=null) { sb.append(s); } br.close(); ir.close(); is.close(); JSONObject json=JSONObject.parseObject(sb.toString()); try { Connection con=DBUtil.lin("XiaoYi"); Statement stmt=con.createStatement(); String sql="insert into socialitem(title,describ,userid,imgpath) value('"+json.getString("title")+"','"+json.getString("describ")+"'," +json.getString("userid")+",'"+json.getString("imgpath")+"')"; System.out.println(sql); stmt.executeUpdate(sql); stmt.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } response.setCharacterEncoding("utf-8"); response.getWriter().print(true); response.getWriter().close(); } }