第十周总结

  本周是冲刺第二周,基本完成了记录功能的页面,就是后台上传Servlet上没有做好。本周写了相当一部分量的代码,但这里是包括其他学科的代码,比如php。

  印象最深的还是OKHttp的使用,如何导入请参考博客:https://www.cnblogs.com/20183711PYD/p/12763626.html

 1 //OKHttp
 2                 OkHttpClient client = new OkHttpClient();
 3                 MultipartBody.Builder builder = new MultipartBody.Builder();
 4                 builder.setType(MultipartBody.FORM);
 5                 builder.addFormDataPart("title",title_str);
 6                 builder.addFormDataPart("note",note_str);
 7                 builder.addFormDataPart("see",see_str);
 8 
 9                 int pic_i=0;
10                 //本地Tomcat,注意不能写localhost,写本机的ip地址
11                 String URL="http://192.168.101.18:8080/CloudNote/CloudServlet";
12                 for(HashMap<String,Bitmap>pic:imageItem){
13                     Set<String> set=pic.keySet();
14                     for(String key:set){
15                         if(pic_i==0){
16                             //跳过默认的+号图片
17                             pic_i++;
18                             continue;
19                         }
20                         //取出bitmap,转换成file,上传
21                         else if(pic_i==1){
22                             Bitmap fbm1 = pic.get(key);
23                             File dofile = doImage(fbm1);
24                             builder.addFormDataPart("image1",dofile.getName(),RequestBody.create(PNG,dofile));
25                             pic_i++;
26                         }
27                         else if(pic_i==2){
28                             Bitmap fbm2 = pic.get(key);
29                             File dofile = doImage(fbm2);
30                             builder.addFormDataPart("image2",dofile.getName(),RequestBody.create(PNG,dofile));
31                             pic_i++;
32                         }
33                         else if(pic_i==3){
34                             Bitmap fbm3 = pic.get(key);
35                             File dofile = doImage(fbm3);
36                             builder.addFormDataPart("image3",dofile.getName(),RequestBody.create(PNG,dofile));
37                             pic_i++;
38                         }
39                     }
40                 }
41                 RequestBody requestBody = builder.build();
42                 Request.Builder RequestBuilder = new Request.Builder();
43                 RequestBuilder.url(URL);
44                 RequestBuilder.post(requestBody);
45                 final Request request=RequestBuilder.build();
46                 client.newCall(request).enqueue(new Callback() {
47                     @Override
48                     public void onFailure(@NotNull Call call, @NotNull IOException e) {
49                         Log.i("TRYxxx","连接失败");
50                         e.printStackTrace();
51                     }
52 
53                     @Override
54                     public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
55                         Log.i("TRYxxx","连接的消息"+response.message());
56                         if(response.isSuccessful()){
57                             Log.i("TRYxxx","连接成功获取的内容"+response.body().string());
58                             Log.i("TRYyyy",title_str);
59                             Log.i("TRYyyy",note_str);
60                             Log.i("TRYyyy",see_str);
61                         }
62                     }
63                 });

以前曾写过Android连接servlet,但那次用的是HttpURLConnection,文本传输还是可以做得到的。这次因为附上图片传输,我选择使用OKHttp来完成,这一周有一小半时间在学习这东西。。。。

posted @ 2020-04-25 11:28  千幽行  阅读(107)  评论(0编辑  收藏  举报