From 550555717b790e782a18834e26df174b821513be Mon Sep 17 00:00:00 2001 From: isshe Date: Wed, 11 May 2016 09:26:43 +0800 Subject: [PATCH] wenjianjiadaxiaoxie --- .../Net1314080903212/AndroidManifest.xml | 28 ----- .../Net1314080903212Activity.java | 48 -------- .../Net1314080903212ClientThread.java | 107 ------------------ .../Net1314080903212GetRemoteFile.java | 84 -------------- .../Net1314080903212MultiChat.java | 95 ---------------- .../Net1314080903212MultiChat.xml | 1 - .../Net1314080903212/Net1314080903212RH.java | 59 ---------- .../Net1314080903212/Net1314080903212WH.java | 87 -------------- 8 files changed, 509 deletions(-) delete mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/AndroidManifest.xml delete mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212Activity.java delete mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212ClientThread.java delete mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212GetRemoteFile.java delete mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212MultiChat.java delete mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212MultiChat.xml delete mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212RH.java delete mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212WH.java diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/AndroidManifest.xml b/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/AndroidManifest.xml deleted file mode 100644 index 46e3afe4..00000000 --- a/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/AndroidManifest.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - - - - - - - - diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212Activity.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212Activity.java deleted file mode 100644 index f9a71e25..00000000 --- a/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212Activity.java +++ /dev/null @@ -1,48 +0,0 @@ -package edu.hzuapps.androidworks.homeworks; - -import android.os.Bundle; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.ViewGroup; -import android.widget.LinearLayout; -import android.widget.TextView; - -import edu.hzuapps.androidworks.R; -//import edu.hzuapps.androidworks.homeworks.Net1314080903212; - -/** - * Created by dell on 2016/3/21. - */ -public class Net1314080903212 extends BackActivity { - - private LinearLayout mTextContainer; - - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - -// TextView textView = new TextView(this); -// textView.setText("isshe"); - - createViewInJava(); - setContentView(mTextContainer); - -// 默认使用XML创建的View -// setContentView(R.layout.net1314080903212); - -} - - private void createViewInJava() { - mTextContainer = new LinearLayout(this); - ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams( // - ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); - mTextContainer.setLayoutParams(layoutParams); - mTextContainer.setOrientation(LinearLayout.HORIZONTAL); - - TextView textView = new TextView(this); - textView.setText("isshe: Net13140809032122"); - - mTextContainer.addView(textView); - } - -} diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212ClientThread.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212ClientThread.java deleted file mode 100644 index 5c74ade7..00000000 --- a/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212ClientThread.java +++ /dev/null @@ -1,107 +0,0 @@ -package edu.hzuapps.androidworks.homeworks.net1314080903212; - -import android.os.Looper; -import android.os.Message; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.net.Socket; -import java.net.SocketTimeoutException; -import android.os.Handler; ////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -//import java.util.logging.Handler; - - -/** - * Created by dell on 2016/4/12. - */ -public class Net1314080903212ClientThread implements Runnable { - private Socket s; - private Handler handler; //定义向UI发送消息的Handler对象 - public Handler revHandler; //定义接收UI线程消息的Handler对象 - BufferedReader br =null; //输入流 - OutputStream os = null; //输出流 - - public Net1314080903212ClientThread(Handler handler) - { - this.handler = handler; - } - - - public void run() - { - try { - System.out.println("Socket before!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - s = new Socket("192.168.240.22", 9402); - System.out.println("Socket behind!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - br = new BufferedReader((new InputStreamReader(s.getInputStream()))); - os = s.getOutputStream(); //为什么br不是类似这样? - - //启动一条子线程来读取服务器响应的数据 - new Thread() - { - @Override - public void run() - { - String content = null; - Net1314080903212WH write_history = new Net1314080903212WH(); - //不断读取Socket输入流中的内容 - try - { - while((content = br.readLine()) != null) - { - //读到来自服务器的数据后, 发送消息通知程序 - //界面显示该数据 - System.out.println("content =" + content + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - Message msg = new Message(); - msg.what = 0x123; //????? - msg.obj = content; //s.getLocalAddress() + ":" + content; //???? ////!!! - handler.sendMessage(msg); //??? - //把数据写入文件 - write_history.write_file(content); - - } - } - catch (IOException e) - { - e.printStackTrace(); - } - } - }.start(); //???? - - //当前线程初始化 - Looper.prepare(); //???? - revHandler = new Handler() - { - @Override - public void handleMessage(Message msg) - { - //接收到UI线程中用户输入的数据 - if (msg.what == 0x345) - { - //将用户在文本框输入的内容写入网络 - try - { - os.write((s.getLocalAddress() + ": " + msg.obj.toString() + "\r\n").getBytes("utf-8")); - } - catch (Exception e) - { - e.printStackTrace(); - } - } - } - }; - //启动Looper - Looper.loop(); //???? - } - catch (SocketTimeoutException e1) - { - System.out.println("网络连接超时!!!"); - } - catch (Exception e) - { - e.printStackTrace(); - } - } -} diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212GetRemoteFile.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212GetRemoteFile.java deleted file mode 100644 index 0e2eb82a..00000000 --- a/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212GetRemoteFile.java +++ /dev/null @@ -1,84 +0,0 @@ -package package edu.hzuapps.androidworks.homeworks.net1314080903212; - -import android.os.Environment; -import android.os.Handler; -import android.os.Looper; -import android.os.Message; -import android.renderscript.ScriptGroup; -import android.widget.TextView; - -import java.io.BufferedInputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.RandomAccessFile; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; - -/** - * Created by dell on 2016/4/28. - */ -public class Net1314080903212GetRemoteFile implements Runnable{ - public TextView GRFshow; - public Handler sendHandler; - - public void Net1314080903212GetRemoteFile(Handler handler) - { - sendHandler = handler; - } - - public void run() - { - try { - System.out.println("before new URL!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - URL git_url = new URL("https://raw.githubusercontent.com/OurScream/PerIntBank/master/README.md"); - - System.out.println("before new URLConnection!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - //打开url链接 - URLConnection git_con = git_url.openConnection(); - - //获取数据 - // InputStream is = git_con.getInputStream(); //方法1 - // BufferedInputStream bis = new BufferedInputStream(is); - InputStream bis = git_url.openStream(); //方法2 - - int read_len = 0; - byte[] readbyte = new byte[1024]; - - System.out.println("before new get_path!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - //新建文件 - File get_path = Environment.getExternalStorageDirectory(); // - File file = new File(get_path.getCanonicalPath() , "ChatHistory"); - - //判断历史记录文件是否存在, 不存在则创建 - if (!file.exists()){ - file.createNewFile(); - System.out.println("GetRemoteFile: 创建文件!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - } - - while((read_len = bis.read(readbyte)) != -1) - { - RandomAccessFile raf = new RandomAccessFile(file,"rw"); - raf.seek(file.length()); - raf.write(readbyte); //这里的换行不知道可不可以! - System.out.println(readbyte.toString() + "GetRemoteFile: 看看一条记录大概都长:" + file.length()); - raf.close(); - } - //再读一次文件 - Message msg = new Message(); - msg.what = 0x789; - sendHandler.sendMessage(msg); - -// new Net1314080903212RH().file_read(new Net1314080903212MultiChat().show); - } - catch(MalformedURLException murle) - { - murle.printStackTrace(); - } - catch(IOException ioe) - { - ioe.printStackTrace(); - } - } -} diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212MultiChat.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212MultiChat.java deleted file mode 100644 index ad9a0f9b..00000000 --- a/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212MultiChat.java +++ /dev/null @@ -1,95 +0,0 @@ -package edu.hzuapps.androidworks.homeworks.net1314080903212; - -import android.app.Activity; -import android.os.Bundle; -import android.os.Message; -import android.text.method.ScrollingMovementMethod; -import android.view.View; -import android.widget.Button; -import android.widget.EditText; -import android.widget.TextView; - -//import java.util.logging.Handler; -import android.os.Handler; - -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; - -/** - * Created by dell on 2016/4/12. - */ -public class Net1314080903212MultiChat extends Activity { - //定义界面的两个文本框 - EditText input; - TextView show; - //定义一个界面上的按钮 - Button send; - Handler handler; - //定义与服务器通信的子线程 - Net1314080903212ClientThread clientThread; - - - @Override - public void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - setContentView(R.layout.main); - input = (EditText) findViewById(R.id.input); - send = (Button) findViewById(R.id.send); - show = (TextView) findViewById(R.id.show); - show.setMovementMethod(new ScrollingMovementMethod()); - - //读取历史记录 - Net1314080903212RH read_history = new Net1314080903212RH(); - read_history.file_read(show); - - handler = new Handler() //????不懂!!! - { - @Override - public void handleMessage(Message msg) - { - //如果消息来自于子线程 - if (msg.what == 0x123) //????不懂!!! - { - //将读取的内容追加显示到文本框 - Date date = new Date(); - DateFormat date_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - String time = date_format.format(date); - show.append("\n" + time); - show.append("\n" + msg.obj.toString()); - //并写到文件! //!!!! - } - } - }; - clientThread = new Net1314080903212ClientThread(handler); - //客户端启动ClientThread线程创建网络连接、读取来自服务器的数据 - new Thread(clientThread).start(); - - send.setOnClickListener(new View.OnClickListener() ///!!! - { - @Override - public void onClick(View v) - { - - try { - //当用户按下发送按钮后, 将用户输入的数据封装成Message - //然后发送给子线程的Handler - Message msg = new Message(); - - msg.what = 0x345; ///???????不懂!! - msg.obj = input.getText().toString(); - clientThread.revHandler.sendMessage(msg); - System.out.println("send msg = " + input.getText() + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - //清空input文本框 - input.setText(""); - } - catch (Exception e) - { - e.printStackTrace(); - } - } - }); ///note!!!!不懂!!! - } - -} diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212MultiChat.xml b/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212MultiChat.xml deleted file mode 100644 index b53a0194..00000000 --- a/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212MultiChat.xml +++ /dev/null @@ -1 +0,0 @@ -实验7:网络编程 diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212RH.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212RH.java deleted file mode 100644 index 61052980..00000000 --- a/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212RH.java +++ /dev/null @@ -1,59 +0,0 @@ -package edu.hzuapps.androidworks.homeworks.net1314080903212; - -//import java.io.*; -import android.os.Environment; -import android.widget.TextView; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.FileNameMap; - -/** - * Created by dell on 2016/4/18. - */ - -/*读历史记录,如果没有指定目录或文件,只需直接返回, - 不需要创建, 创建会在写历史记录的时候执行 - */ -public class Net1314080903212RH { - - public void file_read(TextView show) - { - try { - File get_path = Environment.getExternalStorageDirectory(); // - File file = new File(get_path.getCanonicalPath() , "ChatHistory"); - - //如果目录不存在或者目录不存在, 什么也不做 - if (!file.exists()){ - //nothing to do - System.out.println("file_read 文件不存在???????????????????????????????????????????"); - return ; - } - - System.out.println("file_read 文件存在!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - //两个都存在,这读取历史记录,显示到show区域。一行一行存储历史记录。 - FileInputStream fis = new FileInputStream(file); //(get_path.getCanonicalPath() + "ChatHistory"); - BufferedReader read_file = new BufferedReader(new InputStreamReader(fis)); - String line = null; - - while ((line = read_file.readLine()) != null) { - //显示到show区域 - //将读取的内容追加显示到文本框 - show.append("\n" + line); - System.out.println("读历史记录:" + line + "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); - } - read_file.close(); - } - catch (IOException e) { - e.printStackTrace(); - } - - } - - - -} diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212WH.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212WH.java deleted file mode 100644 index 794cbf3c..00000000 --- a/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212WH.java +++ /dev/null @@ -1,87 +0,0 @@ -package edu.hzuapps.androidworks.homeworks.net1314080903212; - -import android.os.Environment; -import android.provider.ContactsContract; -import android.provider.Settings; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.FileWriter; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Random; -import java.util.RandomAccess; - -/** - * Created by dell on 2016/4/18. - */ -public class Net1314080903212WH { - - public void write_file(String msg) - { - File get_path = Environment.getExternalStorageDirectory(); // -/* String path_str = get_path.toString() + "/MultiChat"; - - System.out.println("path_str = " + path_str); - - File path = new File(path_str); - //判断历史记录目录是否存在,不存在则创建 - if (!path.exists()){ - path.mkdirs(); - System.out.println("创建文件夹!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - } -*/ - File file = null; //File(path_str+"/ChatHistory"); - try { - file = new File(get_path.getCanonicalPath() , "ChatHistory"); - } catch (IOException e) { - e.printStackTrace(); - } - //判断历史记录文件是否存在, 不存在则创建 - if (!file.exists()){ - try { - file.createNewFile(); - System.out.println("创建文件!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - } catch (IOException e) { - e.printStackTrace(); - } - } - - System.out.println("新建文件之后!"); - //获取时间+历史记录写入文件 ; 从服务器接收到的信息写入文件 - // (自己发送的信息不用写入,因为服务器还会返回发送出去的信息,如果再写会有重复) - Date date = new Date(); - DateFormat date_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - String time = date_format.format(date); - String content = time + "\n" + msg + "\n"; - try { -// FileWriter fw = new FileWriter(file); -// FileOutputStream fos = new FileOutputStream(file); - - if (file.length() > 300) //日后改!!!! - { - FileWriter fw = new FileWriter(file); - fw.write(content); - System.out.println(content + "看看一条记录大概都长:" + file.length()); - fw.close(); - } - else - { - RandomAccessFile raf = new RandomAccessFile(file,"rw"); - raf.seek(file.length()); - raf.write(content.getBytes()); //这里的换行不知道可不可以! - System.out.println(content + "看看一条记录大概都长:" + file.length()); - raf.close(); - } - - } catch (IOException e) { - e.printStackTrace(); - } - - - } - -}