本周的代码量很多。php大作业舍友完成了大部分模块,我需要把剩下的内容补齐。主要是家电购物界面的php展示。这里仅仅展现一下大项目的结构图:
之后就是Android方面的代码编写,因为大部分时间都去写php了,安卓写的相对少了一些:
首先就是在app中读取服务器端文件,之前一直没解决,后来才发现是tomcat需要设置虚拟路径。。。
改进代码如下:
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 }
基本实现了读取功能,往后就是设计ListView了。