android网络通信,与socket交换数据

来源:互联网 发布:js中遍历数组对象 编辑:程序博客网 时间:2024/06/05 08:29

     1.Echo程序:

       对于网络开发而言,最常用的交互模式:Web Service , Web Server , Socket程序,一些Socket程序的使用绝对要比 JSP/Servlet等程序更加安全,所以在许多的手机端都会用Socket进行数据的交互。

      (1)首先完成服务器端的开发:启动一个Eclipse,创建一个名为ServerProject的JAVA项目

  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() ;
}


}

       当服务器端程序编写完成之后,就可以开发客户端程序了,客户端现在使用Android完成的。

布局文件:

<?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">
<Button
android:id="@+id/send" 
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:text="连接SocketServer" />
<TextView 
android:id="@+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:text="等待服务器端发送回的显示信息...." />
</LinearLayout>

java代码:

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 {
@Override
public void onClick(View v) {
try {
Socket client = new Socket("192.168.1.114", 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() ;
}
}


}
}

别忘了加上internet权限:

<uses-permission android:name="android.permission.INTERNET" />

其实在Android端编写的客户端代码,与在JAVA中所编写的Socket程序的客户端代码的功能是完全一样的,每没有任何区别。

2.使用Socket上传文件:


如果现在采用第二种方式,那么就要一次性传上去:图片信息,或者一些信息吃标题等等。

public class UploadFile implements Serializable {
private String title ;
private byte [] contentData ;
private String mimeType ;
private long contentLength ;
private String ext ;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public byte[] getContentData() {
return contentData;
}
public void setContentData(byte[] contentData) {
this.contentData = contentData;
}
public String getMimeType() {
return mimeType;
}
public void setMimeType(String mimeType) {
this.mimeType = mimeType;
}
public long getContentLength() {
return contentLength;
}
public void setContentLength(long contentLength) {
this.contentLength = contentLength;
}
public String getExt() {
return ext;
}
public void setExt(String ext) {
this.ext = ext;
}
}


以后所有通过android手机上传的数据,都使用此种方法,进行数据的封装。

因为要进行操做,所以在服务器端也需要定义这么一个类。

下面主要在Activity程序当中,对图片数据进行封装。

上传数据肯定是一个耗费时间的操作,所以为了不影响主线程的操作,应该单独建立一个新的线程对象负责数据的上传。


上传文件客户端代码:

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 {
@Override
public void onClick(View v) {
try {
final Socket client = new Socket("192.168.1.114", 8888);
BufferedReader buf = new BufferedReader(new InputStreamReader(
client.getInputStream())); // 读取返回的数据
new Thread(new Runnable() {


@Override
public void run() {
try {
ObjectOutputStream oos = new ObjectOutputStream(
client.getOutputStream());
UploadFile myFile = SendOnClickListener.this
.getUploadFile();
oos.writeObject(myFile);
oos.close();
} catch (Exception e) {
}
}
}).start();
String result = buf.readLine(); // 接收返回信息
System.out.println("**************** " + result);
if ("true".equals(result)) {
MyClientDemo.this.info.setText("操作成功!");
} else {
MyClientDemo.this.info.setText("操作失败!");
}
buf.close();
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}


private UploadFile getUploadFile() throws Exception { // 包装了传送数据
UploadFile myFile = new UploadFile();
myFile.setTitle("DISNEY公园"); // 设置标题
myFile.setMimeType("image/jpeg"); // 图片的类型
File file = new File(Environment.getExternalStorageDirectory()
.toString() + File.separator + "disney.jpg");
InputStream input = null;
try {
input = new FileInputStream(file); // 从文件中读取
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte data[] = new byte[1024];
int len = 0;
while ((len = input.read(data)) != -1) {
bos.write(data, 0, len);
}
myFile.setContentData(bos.toByteArray());
myFile.setContentLength(file.length());
myFile.setExt("jpg");
} catch (Exception e) {
throw e;
} finally {
input.close();
}
return myFile;
}


}


}

客户端代码编写完成之后,下面可以继续编写服务器端的操作代码,下面为了操作方便,单独定义一个线程类来完成,万一以后传送的客户端比较多呢。

public class ServerThreadUtil implements Runnable {
private static final String DIRPATH = "D:" + File.separator + "mldnfile"
+ File.separator; // 目录路径
private Socket client = null;
private UploadFile upload = null;


public ServerThreadUtil(Socket client) {
this.client = client;
System.out.println("新的客户端连接...");
}


@Override
public void run() {
try {
PrintStream out = new PrintStream(this.client.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(
client.getInputStream()); // 反序列化
this.upload = (UploadFile) ois.readObject(); // 读取对象
System.out.println("文件标题:" + this.upload.getTitle());
System.out.println("文件类型:" + this.upload.getMimeType());
System.out.println("文件大小:" + this.upload.getContentLength());
out.print(this.saveFile()) ;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
this.client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


private boolean saveFile() throws Exception { // 负责文件内容的保存
File file = new File(DIRPATH + UUID.randomUUID() + "."
+ this.upload.getExt());
if (!file.getParentFile().exists()) {
file.getParentFile().mkdir();
}
OutputStream output = null;
try {
output = new FileOutputStream(file) ;
output.write(this.upload.getContentData()) ;
return true ;
} catch (Exception e) {
throw e;
} finally {
output.close();
}
}
}

此时只是完成了一个线程类的编写,但是线程需要被启动。

public class MyServer {


public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(8888); // 服务器端端口
boolean flag = true; // 定义标记,可以一直死循环
while (flag) { // 通过标记判断循环
new Thread(new ServerThreadUtil(server.accept())).start(); // 启动线程
}
server.close(); // 关闭服务器
}


}

每一个链接都启动我们的线程。

小结:使用Android可以与Sockrt程序进行通信;

            上传文件可以采用序列化的方式简单完成,但是程序不具备通用性。

            (使用协议就比较复杂了)


0 0
原创粉丝点击