標籤:android style blog http java 使用
一、編寫思路
手機端為用戶端,PC為服務端,手機訪問PC通訊,需建立一個虛擬通訊鏈路,用戶端通過socket發送請求到服務端,服務端通過serversocket監聽來自用戶端的socket請求,並產生一個socket。這樣就建立了一條虛擬通訊網路,然後再通過相關方法進行通訊。項目需在服務端建立一個java程式,用戶端建立一個android程式。
二、代碼編寫
(一) PC端的代碼編寫——java程式
(1)相關方法
Socket accept() :如果接收到一個來自用戶端的socket的串連請求,該方法將返回一個與串連用戶端對應的socket,否則該方法一直處於等待狀態,線程也被阻塞。
Serversocket(int port ): 用指定的連接埠port來建立一個serversocket。該連接埠應該是一個有效連接埠整數值:0 --65535 。查詢PC的連接埠:提示命令符下輸入
netstat -na ,選擇上面沒有的連接埠。
(2)java代碼
package com.myServer;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class myServer {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// 建立一個serversocket,用於監聽用戶端socket的串連請求
ServerSocket ss = new ServerSocket(3000);
// 採用迴圈不斷接受來自用戶端的請求
while (true) {
// 每當接受到用戶端socket的請求,服務端也產生一個socket
Socket s = ss.accept();
OutputStream os = s.getOutputStream();
os.write("您好,您收到了伺服器的信念祝福\n".getBytes("utf-8"));
// 關閉輸出資料流,關閉socket
os.close();
s.close();
}
}
}
(二)android程式
(1)MainActivity.java
package com.myServer;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class myServer {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// 建立一個serversocket,用於監聽用戶端socket的串連請求
ServerSocket ss = new ServerSocket(3000);
// 採用迴圈不斷接受來自用戶端的請求
while (true) {
// 每當接受到用戶端socket的請求,服務端也產生一個socket
Socket s = ss.accept();
OutputStream os = s.getOutputStream();
os.write("您好,您收到了伺服器的信念祝福\n".getBytes("utf-8"));
// 關閉輸出資料流,關閉socket
os.close();
s.close();
}
}
}
(2)activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="false"
android:cursorVisible="false"
/>
</LinearLayout>
(3)AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myclient"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.myclient.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>