从Java服务器发送序列化对象到Android客户端 [英] Sending Serialized Object from java server to android client

查看:123
本文介绍了从Java服务器发送序列化对象到Android客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请,我有一个Java服务器和Android手机客户端,服务器会从数据库(revokedUser)的对象,并将其发送到Android客户端。

但是客户端通过对象输入流,但块接收在的readObject阶段的目标,我不知道为什么。

在其他的意思,在客户端code中的第一UIhelper正在打印的引用,而第二UIhelper不是

请需要您的帮助。

服务器code

 公共无效revokeUser(字符串userid){            尝试{
                连接康恩= Config.getConnection();
                。preparedStatement STAT =康恩prepareStatement(SQL);                stat.setString(1,用户id);
                stat.setInt(2,listno);
                stat.setInt(3,计数器);
                stat.execute();
                revokeUserzz(用户ID,listno,计数器);
            }赶上(的SQLException E){
                e.printStackTrace();
            }}
私人无效revokeUserzz(字符串userid,诠释listno,诠释计数){    RevokedUser吊销=新RevokedUser();revoke.setuserName(用户ID);
revoke.setlistNumber(listno);
revoke.setcounter(柜);尝试{
        ServerSocket的ServerSocket的=新的ServerSocket(4400);
        的System.out.println(监听:8888);
        Socket套接字的ServerSocket.accept =();
        ObjectOutputStream的OOS =新的ObjectOutputStream(socket.getOutputStream());
        oos.writeObject(撤销);
        的System.out.println(用户名:+ revoke.getuserName());
        的System.out.println(listnumber的:+ revoke.getlistNumber());
        的System.out.println(计数器:+ revoke.getcounter());    serversocket.close();
    socket.close();
    }
    赶上(IOException异常五){
        e.printStackTrace();
    }
}

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

客户端code

 保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    如果(android.os.Build.VERSION.SDK_INT> 9){
        。StrictMode.ThreadPolicy政策=新StrictMode.ThreadPolicy.Builder()permitAll()建();
        StrictMode.setThreadPolicy(政策);
    }
    Socket套接字;
    ObjectInputStream的OIS = NULL;
    尝试{
        插座=新的Socket(192.168.1.115,4400);
        OIS =新的ObjectInputStream(socket.getInputStream());
        UIHelper.displayText(此,R.id.textView1,对象= \\ N+ ois.toString());        RevokedUser撤消=(RevokedUser)ois.readObject();
        UIHelper.displayText(这一点,R.id.textView3,对象= \\ n+ revoke.getuserName()的toString());
    }
}

/////////////////////////////////////////////// //////////////////

序列化code

 公共类RevokedUser实现Serializable {

私有静态最后的serialVersionUID长= -86055680657258429L;

 私人诠释listnumber的;
私人诠释柜台;
私人字符串用户名;公共RevokedUser(){
}公众诠释getlistNumber(){
    返回listnumber的;
}公共无效setlistNumber(INT listnumber的){
    this.listNumber = listnumber的;
}公众诠释getcounter(){
    返回柜台;
}在此输入code公共无效setcounter(INT计数器){
    this.counter =计数器;
}
在此输入code
公共字符串getuserName(){
    返回用户名;
}公共无效setuserName(用户名字符串){
    this.userName =用户名;
}@覆盖
公共字符串的toString(){    回归RevockedUser {+
            用户id ='+使用者名称+'\\''+
            listnumber的=+ listnumber的+
            柜台=+专柜+
            };
}


解决方案

一个潜在的问题是,你是在服务器关闭的东西的方式。

如果您关闭套接字不首先关闭输出流,它可能是你previously写信给流的东西不会得到刷新...和客户端将无法看到它。

(我不相信这是实际的问题...但我认为你无论如何都应该修复它。)


第二个问题是,你的服务器端和客户端是容易泄露文件描述符。的问题是,如果同时有一个插座的异常被抛出,异常可能不会得到闭合。这样做过于频繁,并且你将耗尽应用程序将耗尽或者文件描述符或IP端口号。

要解决这个正确的方法是关闭套接字(和ServerSocket)在最后条款...所以它总是被关闭。

Kindly, I am having a java server and an android mobile client, the server gets an object from database (revokedUser) and sends it to the android client.

However the client receives the object by Object input stream but blocks at the readobject phase and I don't know why

In other mean, the first UIhelper in the client code is printing a reference while the second UIhelper is not

Please need your help

Server Code

    public void revokeUser(String userid){

            try {
                Connection conn = Config.getConnection();       
                PreparedStatement stat = conn.prepareStatement(sql);

                stat.setString(1,userid);
                stat.setInt(2, listno);
                stat.setInt(3, counter);
                stat.execute();
                revokeUserzz(userid,listno, counter);
            } catch (SQLException e) {
                e.printStackTrace();
            }}


private void revokeUserzz(String userid, int listno, int counter) {

    RevokedUser revoke = new RevokedUser();

revoke.setuserName(userid);
revoke.setlistNumber(listno);
revoke.setcounter(counter);

try { 
        ServerSocket serversocket = new ServerSocket(4400);
        System.out.println("listening: 8888");
        Socket socket = serversocket.accept();
        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        oos.writeObject(revoke);
        System.out.println("UserName: " +revoke.getuserName());
        System.out.println("ListNumber: " +revoke.getlistNumber());
        System.out.println("Counter: " +revoke.getcounter());

    serversocket.close();
    socket.close();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Client Code

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    Socket socket;
    ObjectInputStream ois = null;
    try {
        socket = new Socket("192.168.1.115", 4400);
        ois = new ObjectInputStream(socket.getInputStream());
        UIHelper.displayText(this, R.id.textView1, "Object =\n" +ois.toString());

        RevokedUser revoke = (RevokedUser) ois.readObject();
        UIHelper.displayText(this, R.id.textView3, "Object =\n" +revoke.getuserName().toString());
    }
}

/////////////////////////////////////////////////////////////////

Serialized Code

public class RevokedUser implements Serializable { 

private static final long serialVersionUID = -86055680657258429L;

private int listNumber;
private int counter;
private String userName;

public RevokedUser() {
}

public int getlistNumber() {
    return listNumber;
}

public void setlistNumber(int listNumber) {
    this.listNumber = listNumber;
}

public int getcounter() {
    return counter;
}

enter code here

public void setcounter(int counter) {
    this.counter = counter;
}
enter code here
public String getuserName() {
    return userName;
}

public void setuserName(String userName) {
    this.userName = userName;
}

@Override
public String toString() {

    return "RevockedUser{" +
            "userIds='" + userName + '\'' +
            ", listNumber=" + listNumber +
            ", counter=" + counter +
            '}';
}

解决方案

One potential problem is the way that you are closing stuff in the server.

If you close the Socket without closing the output stream first, it is possible that the stuff you previously wrote to the stream won't get flushed ... and the client won't see it.

(I'm not convinced this is the actual problem ... but I think you should fix it anyway.)


A second problem is that your server-side and client-side is liable to leak file descriptors. The problem is that if an exception gets thrown while you have a Socket, the exception may not get closed. Do this too often, and your will run out application will run out of either file descriptors or IP port numbers.

The correct way to deal with this is to close the Socket (and ServerSocket) in a finally clause ... so that it always gets closed.

这篇关于从Java服务器发送序列化对象到Android客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆