连接方式大体跟Socket连接没有差别,只是各种输入输出流的使用变化比较多
客户端Activity组件准备:
1 public class MainActivity extends Activity { 2 3 private TextView infoText = null; 4 private Button connectBtn = null; 5 6 private boolean sdcardExists = false; 8 9 @Override 10 public void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.activity_main); 13 14 /*下面两段代码使Android3.0以上系统可以让 http代码使用主UI线程,因为3.0以上系统对UI资源的使用更严格*/ 15 StrictMode. setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 16 .detectDiskReads().detectDiskWrites().detectNetwork() 17 .penaltyLog().build()); 18 19 StrictMode. setVmPolicy(new StrictMode.VmPolicy.Builder() 20 .detectLeakedSqlLiteObjects().penaltyLog() 21 .penaltyDeath().build()); 22 23 24 infoText = (TextView) findViewById(R.id.infoText); 25 connectBtn = (Button) findViewById(R.id.connectBtn); 26 connectBtn.setOnClickListener(new ConnListener()); 27 }
连接按钮监听事件:
处理流程:生成客户端Socket ---> 调用序列化文件的方法取得序列化文件对象 ---> 取得对象输出流(输出到客户端),客户端输入流(用来接收服务器反馈)---> 向服务器端传送序列化对象
因为网络操作比较耗时,所以此处将其放入线程处理。
1 private class ConnListener implements OnClickListener { 2 3 @Override 4 public void onClick(View v) { 5 try { 6 // 先生成客户端socket 7 final Socket client = new Socket("172.16.1.210", 9999); 8 9 // 上传过程耗时,所以扔到进程中进行,使用匿名内部类 10 new Thread(new Runnable() { 11 @Override 12 public void run() { 13 // 取得序列化对象 14 UploadFile uploadFile = getUploadFile(); 15 //取得client输入流 16 BufferedReader br = null; 17 // 取得对象输出流 18 ObjectOutputStream oos = null; 19 20 try { 21 br = new BufferedReader(new InputStreamReader(client.getInputStream())); 22 oos = new ObjectOutputStream(client.getOutputStream()); 23 oos.writeObject(uploadFile); // 向服务器端写数据 30 oos.close(); 31 } catch (IOException e1) { 32 e1.printStackTrace(); 33 } 34 } 35 }).start(); 36 37 38 39 } catch (UnknownHostException e) { 40 e.printStackTrace(); 41 } catch (IOException e) { 42 e.printStackTrace(); 43 } 44 } 45 46 }
获得序列化对象的方法:
流程:实例化一个对象序列类,填入如文件名,类型等基本信息 ---> 检测sd卡状态并实例化File对象,指向待传文件路径 ---> 取得文件输入流(读取待传文件) ---> 取得字节数组输出流,填充序列化对象用作数据存放的字节数组 ---> 返回序列化对象
1 public UploadFile getUploadFile() { 2 // 准备序列化对象并设置标题等属性 3 UploadFile uploadFile = new UploadFile(); 4 uploadFile.setTitle("a image from XiaoMi"); 5 uploadFile.setMimeType("image/jpeg"); 6 7 // 准备好File路径 8 File file = null; 9 if (this.sdcardExists = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 10 file = new File(Environment.getExternalStorageDirectory().toString() 11 + File.separator + "image" + File.separator 12 + "SmartisanOS" + File.separator + "1Day.jpg"); 13 if (!file.getParentFile().exists()) { 14 file。getParentFile().mkdirs(); 15 } 16 } else { 17 MainActivity.this.infoText.setText("请插入sd卡"); 18 return null; 19 } 20 21 // 将该文件序列化并返回序列 22 FileInputStream fis = null; 23 try { 24 fis = new FileInputStream(file); 25 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 26 byte[] buffer = new byte[1024]; 27 int length = -1; 28 while ((length = fis.read(buffer)) != -1) { 29 baos.write(buffer, 0, buffer.length); 30 } 31 uploadFile.setData(baos.toByteArray()); 32 uploadFile.setContentLength(file.length()); 33 uploadFile.setExt("jpg"); 34 } catch (FileNotFoundException e) { 35 e.printStackTrace(); 36 } catch (IOException e) { 37 e.printStackTrace(); 38 } finally { 39 if (fis != null) { 40 try { 41 fis.close(); 42 fis = null; 43 } catch (IOException e) { 44 e.printStackTrace(); 45 } 46 } 47 } 48 return uploadFile; 49 } 50 51 }