因為最近實驗室項目要求實現在局域網下將android app數據發送到winsock中進行保存,所以對此進行了簡單學習。pc端因為是另一個同學做的,所以不做說明。
在android端,首先添加權限:
<uses-permission android:name="android.permission.INTERNET"/>
之后對app界面進行簡單設計——main.xml:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 定義一個文本框,它用於接收用戶的輸入 -->
<EditText
android:id="@+id/input"
android:layout_width="280dp"
android:layout_height="wrap_content" android:inputType=""/>
<Button
android:id="@+id/send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="8dp"
android:text="@string/send"/>
</LinearLayout>
<!-- 定義一個文本框,它用於顯示來自服務器的信息 -->
<TextView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:background="#ffff"
android:textSize="14sp"
android:textColor="#f000"/>
界面如下:

創建ClientThread.java用於實現局域網下數據的傳輸:
public class ClientThread implements Runnable
{
private Socket s;
// 定義向UI線程發送消息的Handler對象
private Handler handler;
// 定義接收UI線程的消息的Handler對象
public Handler revHandler;
// 該線程所處理的Socket所對應的輸入流
BufferedReader br = null;
OutputStream os = null;
public ClientThread(Handler handler)
{
this.handler = handler;
}
public void run()
{
try
{
s = new Socket("192.168.1.133", 8040);
br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
os = s.getOutputStream();
// 啟動一條子線程來讀取服務器響應的數據
new Thread()
{
@Override
public void run()
{
String content = null;
// 不斷讀取Socket輸入流中的內容
try
{
while ((content = br.readLine()) != null)
{
// 每當讀到來自服務器的數據之后,發送消息通知程序
// 界面顯示該數據
Message msg = new Message();
msg.what = 0x123;
msg.obj = content.toString();
handler.sendMessage(msg);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}.start();
// 為當前線程初始化Looper
Looper.prepare();
// 創建revHandler對象
revHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
// 接收到UI線程中用戶輸入的數據
if (msg.what == 0x345)
{
// 將用戶在文本框內輸入的內容寫入網絡
try
{
os.write((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();
}
}
}
最后創建MainActivity.java實現app中發送內容的獲取等功能:
public class MainActivity extends Activity
{
// 定義界面上的兩個文本框
EditText input;
TextView show;
// 定義界面上的一個按鈕
Button send;
Handler handler;
// 定義與服務器通信的子線程
ClientThread 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);
handler = new Handler() // ②
{
@Override
public void handleMessage(Message msg)
{
// 如果消息來自於子線程
if (msg.what == 0x123)
{
// 將讀取的內容追加顯示在文本框中
show.append("\n" + msg.obj.toString());
}
}
};
clientThread = new ClientThread(handler);
// 客戶端啟動ClientThread線程創建網絡連接、讀取來自服務器的數據
new Thread(clientThread).start(); // ①
send.setOnClickListener(new 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);
// 清空input文本框
input.setText("");
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
這樣,就實現了我們想要的傳輸數據到pc中的功能了。
