Java 服务器不通过ObjectOutputStream接收序列化对象
我正在编写一个Android客户端,它将序列化的“BuyListing”对象发送到我的http servlet。我使用ObjectOutputStream从客户端发送,并使用ObjectInputStream在服务器上接收。当服务器接收到序列化对象时,它会尝试使用instanceof将其识别为BuyListing,然后从中对其进行排序 当我通过objectstream传递字符串时,我当前的实现工作正常,但是当我传递BuyListing对象时,我在服务器端得到的对象只是“null”。我不明白为什么会发生这种情况,我怀疑这可能与我不理解的序列化有关 这是我发送BuyListing对象的客户端代码Java 服务器不通过ObjectOutputStream接收序列化对象,java,android,servlets,serialization,objectoutputstream,Java,Android,Servlets,Serialization,Objectoutputstream,我正在编写一个Android客户端,它将序列化的“BuyListing”对象发送到我的http servlet。我使用ObjectOutputStream从客户端发送,并使用ObjectInputStream在服务器上接收。当服务器接收到序列化对象时,它会尝试使用instanceof将其识别为BuyListing,然后从中对其进行排序 当我通过objectstream传递字符串时,我当前的实现工作正常,但是当我传递BuyListing对象时,我在服务器端得到的对象只是“null”。我不明白为什么
public static void sendListing(final Object inputListing)
{
new Thread(new Runnable() {
public void run() {
Log.d("LOUD AND CLEAR", "Starting new thread for client/server connect with Listing support");
try{
URL url = new URL("http://myserverURL");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
//Begin to open a new OutputObjectStream
ObjectOutputStream objectOut = new ObjectOutputStream(connection.getOutputStream());
objectOut.writeObject(inputListing);
objectOut.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String returnString="";
doubledValue = "";
while ((returnString = in.readLine()) != null)
{
doubledValue= returnString;
Log.d("LOUD AND CLEAR", doubledValue);
}
in.close();
}catch(Exception e)
{
Log.d("Exception",e.toString());
}
}
}).start();
}
public abstract class Listing implements Serializable{
//the following variables need to be set when we create a new listing from the backend
private int swipeCount;
private User user;
private Venue venue; //might need to change this to just a string
private String time;
private String startTime;
private String endTime;
public String section;
public Boolean isSection;
//Getters and setters...
这是我的服务器端接收代码
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
int length = request.getContentLength();
ServletInputStream sin = request.getInputStream();
ObjectInputStream osin = new ObjectInputStream(sin); //ObjectInputStream is created from our inputstream, and allows us to pass serialized objects
// The following segment of code is used to transmit listings through the Object Streams
// Read an object
Object inputObject = null;
String receivedString = " Server received a Listing";
try {
inputObject = osin.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (inputObject instanceof BuyListing)
{
// Cast object to a sellListing
BuyListing inputBuyListing = (BuyListing) inputObject;
SDB.submitBuyListing(inputBuyListing);
receivedString = "Server received a BuyListing";
}
else if (inputObject instanceof String)
{
String out = (String) inputObject;
receivedString = "Server found a string:" + out;
}
else
{
String temp = (String) inputObject;
receivedString = "Server did not recognize object as a listing:" + temp;
}
response.setStatus(HttpServletResponse.SC_OK);
OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream());
String outputValue = receivedString + " was received by Server";
writer.write(outputValue.toString());
writer.flush();
writer.close();
} catch (IOException e) {
try{
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.getWriter().print(e.getMessage());
response.getWriter().close();
} catch (IOException ioe) {
}
}
}
我的BuyListing对象扩展了这个列表对象
public static void sendListing(final Object inputListing)
{
new Thread(new Runnable() {
public void run() {
Log.d("LOUD AND CLEAR", "Starting new thread for client/server connect with Listing support");
try{
URL url = new URL("http://myserverURL");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
//Begin to open a new OutputObjectStream
ObjectOutputStream objectOut = new ObjectOutputStream(connection.getOutputStream());
objectOut.writeObject(inputListing);
objectOut.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String returnString="";
doubledValue = "";
while ((returnString = in.readLine()) != null)
{
doubledValue= returnString;
Log.d("LOUD AND CLEAR", doubledValue);
}
in.close();
}catch(Exception e)
{
Log.d("Exception",e.toString());
}
}
}).start();
}
public abstract class Listing implements Serializable{
//the following variables need to be set when we create a new listing from the backend
private int swipeCount;
private User user;
private Venue venue; //might need to change this to just a string
private String time;
private String startTime;
private String endTime;
public String section;
public Boolean isSection;
//Getters and setters...
其中地点和用户也是序列化对象
这里是用户
package sharedObjects;
import java.io.Serializable;
import java.util.List;
public class User implements Serializable{
private String name;
private String idNumber;
private String connections;
private String rating;
public User(String name){
this.name = name;
this.idNumber = "undefined";
this.connections = "undefined";
this.rating = "undefined";
}
//setters + getters...
场地物体
package sharedObjects;
import java.io.Serializable;
public class Venue implements Serializable{
private String name;
public Venue(String name){
this.name = name;
}
//Setters and Getters...
除了扩展列表之外,BuyListing对象现在是空的
包共享对象
/**
* Item definition including the section.
*/
public class BuyListing extends Listing {
}
无论如何,我可以成功地将一个字符串传递到ConnectToServer.sendListing中,它也可以工作,但在传递BuyListing对象时就不行了
我还读过一些文章,说如果类在不同的包中,它们是不相同的。我不确定这到底意味着什么,但我的服务器端和客户端都有名为“sharedObjects”的包,我在其中保存序列化的对象类
多谢各位
当我传递BuyListing对象时,我在服务器端得到的对象只是“null”
如果收到空值,则发送空值