From f59d813432d276e8f8d118ebb11afe27f7f34665 Mon Sep 17 00:00:00 2001 From: isshe Date: Wed, 11 May 2016 08:44:20 +0800 Subject: [PATCH] =?UTF-8?q?#93=20#12=20=E5=AE=9E=E9=AA=8C7:=E7=BD=91?= =?UTF-8?q?=E7=BB=9C=E7=BC=96=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Net1314080903212GetRemoteFile.java | 84 +++++++++++++++++++ .../Net1314080903212MultiChat.xml | 1 + 2 files changed, 85 insertions(+) create mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212GetRemoteFile.java create mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212MultiChat.xml 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 new file mode 100644 index 00000000..c71ea557 --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212GetRemoteFile.java @@ -0,0 +1,84 @@ +package com.example.dell.multichat; + +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.xml b/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212MultiChat.xml new file mode 100644 index 00000000..b53a0194 --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/Net1314080903212/Net1314080903212MultiChat.xml @@ -0,0 +1 @@ +实验7:网络编程