团队第二阶段冲刺05
一、前言
1、昨天完成了:
增加用户反馈系统:我们将为用户提供反馈系统,方便用户提交建议和问题,以便及时改进和优化系统。
2、今天完成了:
文献推荐系统:基于用户的历史搜索记录和行为数据,建立推荐系统,为用户提供个性化的文献推荐服务。
3、遇到的困难:
文献推荐系统的功能还不够完善,还需要继续改进
二、冲刺进度
-
1.任务量:10天
目前已经花费的时间:5天
还剩余的时间:5天
三、逻辑代码
package com.example.medicalretrieval; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.pdf.PdfDocument; import android.net.Uri; import android.net.wifi.hotspot2.pps.Credential; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.provider.Settings; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.alibaba.sdk.android.oss.ClientConfiguration; import com.alibaba.sdk.android.oss.ClientException; import com.alibaba.sdk.android.oss.OSS; import com.alibaba.sdk.android.oss.OSSClient; import com.alibaba.sdk.android.oss.ServiceException; import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback; import com.alibaba.sdk.android.oss.common.OSSConstants; import com.alibaba.sdk.android.oss.common.OSSLog; import com.alibaba.sdk.android.oss.common.auth.OSSAuthCredentialsProvider; import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider; import com.alibaba.sdk.android.oss.common.auth.OSSFederationCredentialProvider; import com.alibaba.sdk.android.oss.common.auth.OSSFederationToken; import com.alibaba.sdk.android.oss.common.auth.OSSStsTokenCredentialProvider; import com.alibaba.sdk.android.oss.common.utils.IOUtils; import com.alibaba.sdk.android.oss.model.GetObjectRequest; import com.alibaba.sdk.android.oss.model.GetObjectResult; //import com.aliyun.auth.credentials.Credential; //import com.aliyun.auth.credentials.provider.StaticCredentialProvider; //import com.aliyun.sdk.service.sts20150401.AsyncClient; //import com.aliyun.sdk.service.sts20150401.models.AssumeRoleRequest; //import com.aliyun.sdk.service.sts20150401.models.AssumeRoleResponse; import com.example.medicalretrieval.dao.BrowsingHistoryDao; import com.example.medicalretrieval.dao.DownloadHistoryDao; import com.example.medicalretrieval.pojo.BrowsingHistory; import com.example.medicalretrieval.pojo.DownloadHistory; import com.github.barteksc.pdfviewer.PDFView; import com.google.gson.Gson; import org.json.JSONObject; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.URL; import java.nio.channels.FileChannel; import java.text.SimpleDateFormat; import java.time.Duration; import java.util.Calendar; import java.util.Date; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import javax.net.ssl.KeyManager; import javax.net.ssl.X509TrustManager; //import darabonba.core.client.ClientOverrideConfiguration; public class ShowPdfActivity extends AppCompatActivity implements View.OnClickListener { private TextView pdfDownload; private String uri; private final int MY_PERMISSIONS_REQUEST_READ_STORAGE = 1; private final int MY_PERMISSIONS_REQUEST_WRITE_STORAGE = 2; private PDFView pdfView; private BrowsingHistoryDao browsingHistoryDao; private DownloadHistoryDao downloadHistoryDao; private String authors; private String title; private OSSClient oss; private GetObjectRequest get; private String name; private String filePath; private OSS oss1; private byte[] downloadFile; private boolean IsDownloadFile = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show_pdf); init(); Intent intent = getIntent(); Bundle extras = intent.getExtras(); uri = extras.getString("PdfUri"); title = extras.getString("title"); name = extras.getString("name"); name = title+".pdf"; filePath = extras.getString("filePath"); authors = extras.getString("authors"); System.out.println("uri="+uri+";title="+title+";name="+name+";filePath="+filePath+";authors="+authors); TextView pdf_title = findViewById(R.id.pdf_title); pdf_title.setText(title); ImageView back = findViewById(R.id.pdf_back); back.setOnClickListener(this); pdfDownload = findViewById(R.id.pdf_download); pdfDownload.setOnClickListener(this); pdfView = findViewById(R.id.pdfView); downloadFileFromOSS(); browsing(); } /** * 从oss里面下载PDF文件,预览用 */ private void downloadFileFromOSS() { // 构造下载文件请求。 // 依次填写Bucket名称(例如examplebucket)和Object完整路径(例如exampledir/exampleobject.txt)。Object完整路径中不能包含Bucket名称。 System.out.println("从oss里面下载文件"); GetObjectRequest get = new GetObjectRequest("medical-retrieval", filePath); oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>() { private byte[] buffer; @Override public void onSuccess(GetObjectRequest request, GetObjectResult result) { long length = result.getContentLength(); if (length > 0) { buffer = new byte[(int) length]; int readCount = 0; while (readCount < length) { try{ readCount += result.getObjectContent().read(buffer, readCount, (int) length - readCount); pdfView.fromBytes(buffer).load(); // pdfView.fromAsset("文件名").load(); downloadFile = buffer; }catch (Exception e){ OSSLog.logInfo(e.toString()); } } } } @Override public void onFailure(GetObjectRequest request, ClientException clientException, ServiceException serviceException) { } }); } /** * 点进来了,也就浏览了,记录一下 */ private void browsing(){ browsingHistoryDao = MyApplication.getInstance().getBrowsingHistoryDataBase().browsingHistoryDao(); browsingHistoryDao.deleteByTitle(title); BrowsingHistory browsingHistory = new BrowsingHistory(); browsingHistory.setAuthors(authors); long time=System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis(); // SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date d1=new Date(time); browsingHistory.setDate(d1); browsingHistory.setTitle(title); System.out.println("browsingHistory"); System.out.println(browsingHistory); browsingHistoryDao.insert(browsingHistory); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.pdf_back: finish(); break; case R.id.pdf_download: if(IsDownloadFile){ Toast.makeText(this, "文件已经下载,请勿重复下载", Toast.LENGTH_SHORT).show(); break; } try { initPermission(); } catch (IOException e) { e.printStackTrace(); } break; } } private void initPermission() throws IOException { if (Build.VERSION.SDK_INT >= 30) { if (!Environment.isExternalStorageManager()) { Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION); startActivity(intent); return; } downFile(); }else{ if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_STORAGE); } if (ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},MY_PERMISSIONS_REQUEST_WRITE_STORAGE); } downFile(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == MY_PERMISSIONS_REQUEST_READ_STORAGE) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { //权限被授予 try { downFile(); } catch (IOException e) { e.printStackTrace(); } } else { //权限被拒绝 Toast.makeText(this, "未获取权限,无法下载文档", Toast.LENGTH_SHORT).show(); } } } /** * 下载文件,下载到特定的地点 */ private void downFile() throws IOException { //记录下载情况。 downloadHistoryDao = MyApplication.getInstance().getDownloadHistoryDataBase().downloadHistoryDao(); DownloadHistory downloadHistory = new DownloadHistory(); downloadHistoryDao.deleteByTitle(title); downloadHistory.setDownloadUri(".\\app\\src\\main\\assets\\"+name); downloadHistory.setAuthors(authors); downloadHistory.setDate(new Date(System.currentTimeMillis())); downloadHistory.setTitle(title); downloadHistoryDao.insert(downloadHistory); copyFile(name); } private static final String AUDIO_FILE_PATH = Environment.getExternalStorageDirectory().getPath()+"/download/"; public void copyFile( String name) { new Thread(()->{ File file = new File(Environment.getExternalStorageDirectory(), name); if(file.exists()){ file.delete(); } try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } FileOutputStream fos = null; try { fos = new FileOutputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } try { assert fos != null; fos.write(downloadFile); } catch (IOException e) { e.printStackTrace(); } try { fos.close(); Toast.makeText(this, "文件下载成功", Toast.LENGTH_SHORT).show(); IsDownloadFile = true; } catch (IOException e) { e.printStackTrace(); } }).start(); } public void init(){ String endpoint = "https://oss-cn-beijing.aliyuncs.com"; // 推荐使用OSSAuthCredentialsProvider,Token过期后会自动刷新。 String stsServer = "http://192.168.43.144:7080"; OSSCredentialProvider credentialProvider = new OSSAuthCredentialsProvider(stsServer); // 完成以下配置项。 ClientConfiguration conf = new ClientConfiguration(); conf.setConnectionTimeout(15 * 1000); // 连接超时时间,默认15秒。 conf.setSocketTimeout(15 * 1000); // Socket超时时间,默认15秒。 conf.setMaxConcurrentRequest(5); // 最大并发请求数,默认5个。 conf.setMaxErrorRetry(2); // 失败后最大重试次数,默认2次。 oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider, conf); } }