李浩:完成用户主页面的布局以及交互事件。
效果图:
第二阶段任务基本已经完成,我会继续测试程序,完善UI以及其他功能。
戴伟伟:
今日完成夜间模式的开发
代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_marginRight="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="@string/day_night_label" android:textSize="20sp" android:textColor="@color/colorDayNightChange" /> <Button android:id="@+id/day_night_change" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="5dp" android:text="日夜间模式切换" android:textSize="20sp" android:textColor="@color/colorDayNightChange"/> </LinearLayout>
package com.example.nighttheme; import android.content.res.Configuration; import android.os.Bundle; import android.view.View; import android.widget.Button; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatDelegate; public class MainActivity extends AppCompatActivity { private Button mDayNightChange; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDayNightChange = (Button) findViewById(R.id.day_night_change); mDayNightChange.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int mode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; if (mode == Configuration.UI_MODE_NIGHT_YES) { getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO); } else if (mode == Configuration.UI_MODE_NIGHT_NO) { getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES); } recreate(); } }); } }
package com.example.nighttheme; import android.app.Application; import androidx.appcompat.app.AppCompatDelegate; public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); /** * 默认设置一直使用夜间模式 * * 这里AppCompatDelegate.setDefaultNightMode()方法可以接受的参数值有4个: * MODE_NIGHT_NO. Always use the day (light) theme(一直应用日间(light)主题). * MODE_NIGHT_YES. Always use the night (dark) theme(一直使用夜间(dark)主题). * MODE_NIGHT_AUTO. Changes between day/night based on the time of day(根据当前时间在day/night主题间切换). * MODE_NIGHT_FOLLOW_SYSTEM(默认选项). This setting follows the system's setting, which is essentially MODE_NIGHT_NO(跟随系统,通常为MODE_NIGHT_NO). */ AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); } }
代码需整合到app中,暂未整合
明日工作:尝试爬取大牛们文章
徐利峰:
今日完成将动态的本地图片上传至服务器端的文件夹下。
明天将个人动态的数据完整存储到服务器端,并在相应的界面进行展示。
服务端代码:
package Servlet; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.List; import java.util.Random; 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 org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; /** * Servlet implementation class downloadimg */ @WebServlet("/downloadimg") public class downloadimg extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public downloadimg() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String temppath="C:/Program Files/apache-tomcat-9.0.31-windows-x64/apache-tomcat-9.0.31/tempfile"; //String path="http://39.97.181.86/BaiXiaoSheng/userimg"; String path ="C:/Program Files/apache-tomcat-9.0.31-windows-x64/apache-tomcat-9.0.31/webapps/STDEverything/images/dongtai"; String username=request.getParameter("username"); DiskFileItemFactory disk = new DiskFileItemFactory(1024*10,new File(temppath)); ServletFileUpload up = new ServletFileUpload(disk); List<FileItem> list; try { list=up.parseRequest(request); for(FileItem item:list) { if(!item.isFormField()) { InputStream inputStream=item.getInputStream(); String filename=item.getName(); String imgname=String.valueOf(new Random().nextInt(1000))+"_"+username+"_"+String.valueOf(new Random().nextInt(1000))+filename; OutputStream outputStream=new FileOutputStream(path+"/"+imgname); System.out.println(imgname); int len=0; byte buff[]=new byte[1024]; while((len=inputStream.read(buff))!=-1) { outputStream.write(buff,0,len); } outputStream.flush(); outputStream.close(); inputStream.close(); //update用户的imgurl /*DBUtil.writeBlogImg(request.getRequestURL().substring(0,request.getRequestURL().lastIndexOf("/")) +"/imagesog/"+imgname, blogid);*/ item.delete(); }else { System.out.println(item.getFieldName()); item.delete(); } } } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); response.getWriter().write("no"); } response.getWriter().write("yes"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
Android端代码:
package com.lh.std_everything.ui.home.hometype.dongtai; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.navigation.Navigation; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.app.Activity; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.Toast; import com.giftedcat.easylib.selector.MultiImageSelector; import com.lh.std_everything.HttpUtil.HttpUtil; import com.lh.std_everything.MainActivity; import com.lh.std_everything.R; import com.lh.std_everything.ui.home.hometype.blog.WriteViewModel; import com.lh.std_everything.ui.home.hometype.news.shownews.ShowNewsActivity; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.Unbinder; public class DongTaiActivity extends AppCompatActivity implements View.OnClickListener{ private static final int REQUEST_IMAGE = 2; private int maxNum = 9; private Button submit; private EditText text; Unbinder unbinder; ProgressBar progressBar; @BindView(R.id.rv_images) RecyclerView rvImages; private Handler handle; NineGridAdapter adapter; private int uploadnum=0; private int size; private String urlimg; private List<String> select; List<String> mSelect; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dong_tai); unbinder = ButterKnife.bind(this); submit=findViewById(R.id.submit); text=findViewById(R.id.text); progressBar=findViewById(R.id.progressBar); mSelect = new ArrayList<>(); submit.setOnClickListener(this); initView(); } private void initView() { rvImages.setLayoutManager(new GridLayoutManager(this, 3)); adapter = new NineGridAdapter(DongTaiActivity.this, mSelect, rvImages); adapter.setMaxSize(maxNum); rvImages.setAdapter(adapter); adapter.setOnAddPicturesListener(new OnAddPicturesListener() { @Override public void onAdd() { pickImage(); } }); } /*上传发动态的信息和照片到服务器 */ @Override public void onClick(View v) { switch (v.getId()){ case R.id.submit: String daongtaitext=text.getText().toString(); if(daongtaitext.isEmpty()){ Toast.makeText(DongTaiActivity.this, "动态内容不能为空", Toast.LENGTH_SHORT).show(); return; } String username= MainActivity.getUsername(); handle = new Handler(new Handler.Callback() { @Override public boolean handleMessage(@NonNull Message msg) { switch (msg.what) { case 100: if (uploadnum < size) { writeDongTaiImg(select.get(uploadnum)); } else { Toast.makeText(DongTaiActivity.this, "上传成功", Toast.LENGTH_SHORT).show(); progressBar.setVisibility(View.GONE); } break; case 404: Toast.makeText(DongTaiActivity.this, "上传失败,您可以点击上传按钮重新上传", Toast.LENGTH_SHORT).show(); progressBar.setVisibility(View.GONE); break; default: break; } return true; } }); new Thread(new Runnable() { @Override public void run() { DongTaiActivity.this.runOnUiThread(new Runnable() { @Override public void run() { progressBar.setVisibility(View.VISIBLE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { progressBar.setTooltipText("正在上传..."); } } }); /*将动态写到服务器*/ // final String url =""; // HttpUtil.sendPost(url,""); /*将图片一张一张上传到服务器*/ urlimg = "http://116.62.178.231:8080/BaiXiaoSheng/downloadimg?username="+MainActivity.getUsername(); handle.sendEmptyMessage(100); } }).start(); } } /*上传图片*/ private void writeDongTaiImg(final String img) { new Thread(new Runnable() { @Override public void run() { Log.i("图片地址:", img); Map<String, File> fileMap = new HashMap<String, File>(); fileMap.put("file", new File(img)); String msg = null; try { msg = HttpUtil.sendPostImg(urlimg, fileMap); } catch (IOException e) { e.printStackTrace(); } uploadnum++; final String finalMsg = msg; DongTaiActivity.this.runOnUiThread(new Runnable() { @Override public void run() { if (finalMsg.equals("yes")) { Log.d("tag", finalMsg); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { progressBar.setTooltipText("图片进度:" + uploadnum + "/" + size); } handle.sendEmptyMessage(100); } else { uploadnum = 0; handle.sendEmptyMessage(404); } } }); } }).start(); } /** * 选择添加图片 */ private void pickImage() { MultiImageSelector selector = MultiImageSelector.create(this); selector.showCamera(true); selector.count(maxNum); selector.multi(); selector.origin(mSelect); selector.start(this, REQUEST_IMAGE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_IMAGE) { if (resultCode == RESULT_OK) { select = data.getStringArrayListExtra(MultiImageSelector.EXTRA_RESULT); size=select.size(); mSelect.clear(); mSelect.addAll(select); adapter.notifyDataSetChanged(); } } } @Override protected void onDestroy() { super.onDestroy(); unbinder.unbind(); } }