使用Socket进行数据的交互将更安全。
启动tomcat
服务器端:
另起一个eclipse,换工作空间,新建一个java项目,并新建MyServer.java:
package com.li.server;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(8888);
Socket client = server.accept(); // 接收客户端请求
PrintStream out = new PrintStream(client.getOutputStream());
BufferedReader buf = new BufferedReader(new InputStreamReader(
client.getInputStream()));
StringBuffer info = new StringBuffer() ;
info.append("Android : ") ;
info.append(buf.readLine()) ;
out.print(info) ;
out.close() ;
buf.close() ;
client.close() ;
server.close() ;
}
}
客户端:
在main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="#000000">
<Button
android:id="@+id/send"
android:layout_marginTop="8dp"
android:layout_width="160dp"
android:layout_height="40dp"
android:background="#3399ff"
android:textColor="#ffffff"
android:text="连接SocketServer" />
<TextView
android:id="@+id/info"
android:gravity="center_horizontal"
android:layout_marginTop="8dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="等待服务器端发送回的显示信息...." />
</LinearLayout>
在MyClientDemo.java中:
package com.li.clientproject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MyClientDemo extends Activity {
private Button send = null;
private TextView info = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.send = (Button) super.findViewById(R.id.send);
this.info = (TextView) super.findViewById(R.id.info);
this.send.setOnClickListener(new SendOnClickListener());
}
private class SendOnClickListener implements OnClickListener {
public void onClick(View v) {
try {
Socket client = new Socket("192.168.1.13", 8888);
PrintStream out = new PrintStream(client.getOutputStream());
BufferedReader buf = new BufferedReader(new InputStreamReader(
client.getInputStream()));
out.println("这是服务器端返回的数据!") ; // 向服务器端发送数据
MyClientDemo.this.info.setText(buf.readLine()) ;
out.close() ;
buf.close() ;
client.close() ;
} catch (Exception e) {
e.printStackTrace() ;
}
}
}
}
在AndroidManifest.xml中修改权限:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.li.clientproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyClientDemo"
android:label="@string/title_activity_my_client_demo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>