问题描述
我想使用socket从Android客户端向Java服务器发送2个对象(因为我正在开发一个远程PC)。
AndroidClient.java
public class MainActivity extends Activity {
Socket client;
ObjectOutputStream oos;
OutputStream os;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SendObj so = new SendObj();
so.execute();
}
$ b $ class SendObj extends AsyncTask< Void,Void,Void> {
@Override
protected void doInBackground(Void ... arg0){
尝试{
client = new Socket(192.168.237.1,6566);
os = client.getOutputStream();
oos = new ObjectOutputStream(os);
Serializer ma = new Serializer(2,Helllo);
Log.i(Serial,+ ma.name);
oos.writeObject(ma);
oos.writeObject(new String(来自客户端的另一个对象));
oos.close();
os.close();
client.close();
} catch(UnknownHostException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
返回null;
@Override
public boolean onCreateOptionsMenu(菜单菜单){
//使菜单膨胀;这会将项目添加到操作栏(如果存在)。
getMenuInflater()。inflate(R.menu.main,menu);
返回true;
$ b @SuppressWarnings(serial)
class Serializer实现Serializable {
int num;
字符串名称;
public Serializer(int num,String name){
this.name = name;
this.num = num;
JavaServer。 java
public class ObjectReceiver {
static ServerSocket server;
静态套接字客户端;
static ObjectInputStream ois;
static InputStream是;
public static void main(String [] arg)throws IOException,ClassNotFoundException {
server = new ServerSocket(6566);
System.out.println(等待);
while(true){
client = server.accept();
System.out.println(Connected);
is = client.getInputStream();
ois = new ObjectInputStream(is);
尝试{
ObjectSerail或=(ObjectSerail)ois.readObject();
if(or!= null){
System.out.println(Done);
}
} catch(ClassNotFoundException e){
e.printStackTrace();
$ @SuppressWarnings(serial)
class ObjectSerail实现了Serializable {
int num;
字符串名称;
public ObjectSerail(int num,String name){
this.name = name;
this.num = num;
}
}
}
当然我知道上述方法不起作用,因为它会给出 ClassNotFoundException()
。所以现在我想知道如何使用Gson库来序列化和反序列化对象。提前致谢。
可以在任何平台上与Java一起使用 - 不仅仅是Android。
使用gson序列化一个对象:
//序列化一个对象。
public String serializeToJson(MyClass myClass){
Gson gson = new Gson();
String j = gson.toJson(myClass);
return j;
}
使用gson反序列化为单个对象。 b
//反序列化为单个对象。
public MyClass deserializeFromJson(String jsonString){
Gson gson = new Gson();
MyClass myClass = gson.fromJson(jsonString,MyClass.class);
返回myClass;
$ b从示例中可以看出,gson非常神奇:)它是实际上不是神奇的 - 你需要确保至少有几件事情:确保你的类有一个没有参数的构造函数,这样gson库可以很容易地得到一个实例。
确保属性名称与json中的属性名称匹配,以便gson库可以将字段从json映射到类中的字段。
另请参阅
I want to send 2 objects to Java server from Android client using socket (as I am developing a Remote PC).
AndroidClient.java
public class MainActivity extends Activity{
Socket client;
ObjectOutputStream oos;
OutputStream os;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SendObj so=new SendObj();
so.execute();
}
class SendObj extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... arg0) {
try {
client=new Socket("192.168.237.1",6566);
os=client.getOutputStream();
oos=new ObjectOutputStream(os);
Serializer ma=new Serializer(2, "Helllo");
Log.i("Serial",""+ma.name);
oos.writeObject(ma);
oos.writeObject(new String("Another Object from Client"));
oos.close();
os.close();
client.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
@SuppressWarnings("serial")
class Serializer implements Serializable {
int num;
String name;
public Serializer(int num,String name){
this.name=name;
this.num=num;
}
}
JavaServer.java
public class ObjectReceiver {
static ServerSocket server;
static Socket client;
static ObjectInputStream ois;
static InputStream is;
public static void main(String[] arg) throws IOException, ClassNotFoundException{
server=new ServerSocket(6566);
System.out.println("Wait");
while(true){
client=server.accept();
System.out.println("Connected");
is=client.getInputStream();
ois=new ObjectInputStream(is);
try {
ObjectSerail or = (ObjectSerail) ois.readObject();
if(or!=null){
System.out.println("Done");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
@SuppressWarnings("serial")
class ObjectSerail implements Serializable{
int num;
String name;
public ObjectSerail(int num,String name){
this.name=name;
this.num=num;
}
}
}
Of course I know the above method won't work because it will gives ClassNotFoundException()
. So now I want to know how can I use Gson library to serialize and deserialize the objects. Thanks in advance.
解决方案 gson can be used with Java on any platform – not only Android.
Using gson to serialize a single object:
// Serialize a single object.
public String serializeToJson(MyClass myClass) {
Gson gson = new Gson();
String j = gson.toJson(myClass);
return j;
}
Using gson to deserialize to a single object.
// Deserialize to single object.
public MyClass deserializeFromJson(String jsonString) {
Gson gson = new Gson();
MyClass myClass = gson.fromJson(jsonString, MyClass.class);
return myClass;
}
As you can see from the examples, gson is quite magical :) It is not actually magical - you need to ensure at least a couple of things:
Ensure that your class has a no args constructor so that the gson library can easily get an instance.
Ensure that the attribute names match those in the json so that the gson library can map fields from the json to the fields in your class.
Also see https://sites.google.com/site/gson/gson-user-guide#TOC-Object-Examples
这篇关于如何使用Gson序列化android中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!