forked from hzuapps/android-labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
509 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903212/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="edu.hzuapps.androidworks.homeworks.net1314080903212"> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
|
||
<activity | ||
android:name=".Net1314080903212MultiChat" | ||
android:label="@string/app_name" | ||
android:theme="@style/AppTheme.NoActionBar"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
|
||
</application> | ||
<uses-permission android:name="android.permission.INTERNET"/> | ||
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> | ||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> | ||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> | ||
</manifest> |
48 changes: 48 additions & 0 deletions
48
...in/java/edu/hzuapps/androidworks/homeworks/net1314080903212/Net1314080903212Activity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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); | ||
} | ||
|
||
} |
107 changes: 107 additions & 0 deletions
107
...ava/edu/hzuapps/androidworks/homeworks/net1314080903212/Net1314080903212ClientThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
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(); | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...va/edu/hzuapps/androidworks/homeworks/net1314080903212/Net1314080903212GetRemoteFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
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(); | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
...n/java/edu/hzuapps/androidworks/homeworks/net1314080903212/Net1314080903212MultiChat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
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!!!!不懂!!! | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...in/java/edu/hzuapps/androidworks/homeworks/net1314080903212/Net1314080903212MultiChat.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
实验7:网络编程 |
Oops, something went wrong.