发送的ArrayList<对象>通过插座。 Java的 [英] Sending ArrayList<Object> through socket. Java

查看:158
本文介绍了发送的ArrayList<对象>通过插座。 Java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是通过来自Android客户端套接字到Java服务器发送和ArrayList。下面是客户端的code,它发送的ArrayList

What i'm trying to do is to send and ArrayList through a socket from Android client to Java server. Here is the code of client which sends the ArrayList :

private void sendContacts(){
            AppHelper helperClass = new AppHelper(getApplicationContext());
            final ArrayList<Person> list = helperClass.getContacts();
            System.out.println("Lenght of an contacts array : " +list.size());
//          for (Person person : list) {
//              System.out.println("Name "+person.getName()+"\nNumber "+ person.getNr());
//          } 
            handler.post(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try {
//**151 line**              os.writeObject(list); 
                    os.flush();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    Log.e(TAG, "Sending Contact list has failed");
                    e.printStackTrace();
                }
            }
            });
}

public ArrayList<Person> getContacts() {
        ArrayList<Person> alContacts = null;
        ContentResolver cr = mContext.getContentResolver(); //Activity/Application android.content.Context
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if(cursor.moveToFirst())
        {
             alContacts = new ArrayList<Person>();
            do
            {
                String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

                if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
                {
                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{ id }, null);
                    while (pCur.moveToNext()) 
                    {
                        String contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        String contactName = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        Person temp = new Person(contactName, contactNumber);

                        alContacts.add(temp);
                        break;
                    }
                    pCur.close();
                }

            } while (cursor.moveToNext()) ;
        }
        return alContacts;
    }

下面是服务器code:

Here is the server code:

private void whileChatting() throws IOException {
        ableToType(true);
        String message = "You are now connected ";
        sendMessage(message);
        do {// have conversation
            try {
                message = (String) input.readObject();
                // message = (String) input.readLine();
                showMessage("\n" + message);
            } catch (ClassNotFoundException e) {
                showMessage("It is not a String\n");

                // TODO: handle exception
            }
            try{
                ArrayList<Person> list =(ArrayList<Person>) input.readObject();
                showMessage("GOT A LIST OF PERSON WITH SIZE :" + list.size());
            }catch(ClassNotFoundException e){
                showMessage("It is not a List of Person\n");
            }
        } while (!message.equals("client - end"));
    }

错误code:

Error code :

Caused by: java.io.NotSerializableException: com.lauris.client.Person
    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
    at java.util.ArrayList.writeObject(ArrayList.java:648)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1033)
    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
    **at com.lauris.client.MainActivity$ClientThread$4.run(MainActivity.java:151)**
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我想我的问题是序列化?我真的不明白这是什么意思?有人可以解释我它是什么,我为什么需要它?而且我怎么可能解决我的code?

I guess my problem is serialization ? I don't really understand the meaning of this ? Can somebody explain me what it is, why i need it? And how may i fix my code?

其他的问题。你可以在我的code IM看到想听听不同的InputStreams。我认为,I,M做是错误的。可能有人更高级的解释我该怎么办呢正确吗?

Additional question. You can see in my code im trying to listen for different InputStreams. I think i,m doing it wrong. Could somebody more advanced explain me how to do it correct ?

鸭preciate你们的帮助,我真的坚持这一点。

Appreciate your help, i'm really stuck on this.

推荐答案

类必须实现Serializable

import java.io.Serializable;

public class Person implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

}

这篇关于发送的ArrayList&LT;对象&gt;通过插座。 Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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