zoukankan      html  css  js  c++  java
  • 团队开发冲刺第二阶段(二)

      昨日问题解决:要通过tomcat访问文件需要在tomcat下的conf目录配置server.xml:

     改进后代码如下:

      1 package com.example.toas;
      2 
      3 import androidx.appcompat.app.AppCompatActivity;
      4 
      5 import android.graphics.Bitmap;
      6 import android.graphics.BitmapFactory;
      7 import android.os.Build;
      8 import android.os.Bundle;
      9 import android.os.Environment;
     10 import android.os.Handler;
     11 import android.os.Message;
     12 import android.os.StrictMode;
     13 import android.view.View;
     14 import android.widget.Button;
     15 import android.widget.ImageView;
     16 import android.widget.TextView;
     17 import android.widget.Toast;
     18 
     19 import java.io.BufferedReader;
     20 import java.io.File;
     21 import java.io.FileNotFoundException;
     22 import java.io.FileOutputStream;
     23 import java.io.FileReader;
     24 import java.io.IOException;
     25 import java.io.InputStream;
     26 import java.io.InputStreamReader;
     27 import java.io.OutputStream;
     28 import java.net.HttpURLConnection;
     29 import java.net.MalformedURLException;
     30 import java.net.URL;
     31 import java.net.URLConnection;
     32 
     33 public class MainActivity extends AppCompatActivity {
     34     private TextView trytxt;
     35     private Button btnt;
     36     private Button btni;
     37     private ImageView trypng;
     38     private Bitmap bitmap;
     39     private Myhandler handler1;
     40     class Myhandler extends Handler{
     41         public void handleMessage(Message msg){
     42             trypng = (ImageView)findViewById(R.id.trypng);
     43             trypng.setImageBitmap((Bitmap)msg.obj);
     44         }
     45     }
     46     @Override
     47     protected void onCreate(Bundle savedInstanceState) {
     48         super.onCreate(savedInstanceState);
     49         setContentView(R.layout.activity_main);
     50 
     51         btni = (Button)findViewById(R.id.btni);
     52         btnt = (Button)findViewById(R.id.btnt);
     53         trytxt = (TextView)findViewById(R.id.trytxt);
     54         handler1 = new Myhandler();
     55         btni.setOnClickListener(new View.OnClickListener() {
     56             @Override
     57             public void onClick(View v) {
     58                 new Thread(new Runnable() {
     59                     @Override
     60                     public void run() {
     61                         try{
     62                             String path="http://39.101.190.190:8080/filesource/Image/try_image1_20200511114113.png";
     63                             URL url = new URL(path);
     64                             HttpURLConnection conn = (HttpURLConnection)url.openConnection();
     65                             conn.setRequestMethod("GET");
     66                             conn.setConnectTimeout(8000);
     67                             conn.setReadTimeout(8000);
     68                             conn.connect();
     69                             if(conn.getResponseCode()==200){
     70                                 InputStream is = conn.getInputStream();
     71                                 Bitmap bm = BitmapFactory.decodeStream(is);
     72                                 Message msg = new Message();
     73                                 msg.obj = bm;
     74                                 handler1.sendMessage(msg);
     75                             }
     76                         } catch (MalformedURLException e) {
     77                             e.printStackTrace();
     78                         } catch (IOException e) {
     79                             e.printStackTrace();
     80                         }
     81                     }
     82                 }).start();
     83             }
     84         });
     85 
     86         btnt.setOnClickListener(new View.OnClickListener() {
     87             @Override
     88             public void onClick(View v) {
     89                 new Thread(new Runnable() {
     90                     @Override
     91                     public void run() {
     92                         String txtpath = "http://39.101.190.190:8080/filesource/Note/try_note_20200511114113.txt";
     93                         try{
     94                             URL turl = new URL(txtpath);
     95                             HttpURLConnection cont = (HttpURLConnection)turl.openConnection();
     96                             cont.setReadTimeout(8000);
     97                             cont.setConnectTimeout(8000);
     98                             cont.connect();
     99                             InputStream input = cont.getInputStream();
    100                             BufferedReader in = new BufferedReader(new InputStreamReader(input));
    101                             String line = null;
    102                             StringBuffer sb = new StringBuffer();
    103                             while((line = in.readLine())!=null){
    104                                 sb.append(line);
    105                             }
    106                         } catch (MalformedURLException e) {
    107                             e.printStackTrace();
    108                         } catch (IOException e) {
    109                             e.printStackTrace();
    110                         }
    111                     }
    112                 }).start();
    113             }
    114         });
    115     }
    116 }

    效果如下:

     未能解决问题:读取txt文件可以实现,也能拿出其中的内容,但是在改变View控件上出现问题,原因是线程问题,明日任务:编写线程。

  • 相关阅读:
    记某农信社面试
    sublime2 c++的一些使用配置
    switch case
    sizeof与strlen的区别 浅谈
    求最大公约数
    Android基础控件——PopupWindow模仿ios底部弹窗
    Android基础控件——ListView实现时间轴效果
    Android基础控件——CardView的使用、仿支付宝银行卡
    Android基础控件——AlertDialog、ProgressDialog实现单选对话框、多选对话框、进度条对话框、输入框对话框
    Android基础——适配安卓6.0新权限系统
  • 原文地址:https://www.cnblogs.com/20183711PYD/p/12961273.html
Copyright © 2011-2022 走看看