服务器不通过ObjectOutputStream接收序列化对象

时间:2014-05-06 22:05:57

标签: java android servlets serialization objectoutputstream

我正在编写一个Android客户端,它将序列化的“BuyListing”对象发送到我的http servlet。我正在使用ObjectOutputStream从客户端发送,而ObjectInputStream则在服务器上接收。当服务器收到序列化对象时,它会尝试使用instanceof将其识别为BuyListing,然后从那里对其进行排序。

当我通过对象流传递字符串时,我当前的实现工作正常,但是当我传递我的BuyListing对象时,我在服务器端获得的对象只是“null”。我想知道为什么会发生这种情况,我怀疑它可能与序列化有关,我不理解。

以下是我发送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();
        }

这是我的服务器端接收代码

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对象扩展了此Listing对象

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...

Venue和User也是序列化对象 这是用户

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对象现在是空的     package sharedObjects;

/**
 * Item definition including the section.
 */
public class BuyListing extends Listing {

}

无论如何,我可以成功地将一个字符串传递给ConnectToServer.sendListing并且它可以工作,但是当我传递一个BuyListing对象时却不行。

我还读过一些地方,如果他们在不同的包中,那些类就不一样了。我不确定这意味着什么,但我的服务器和客户端都有名为“sharedObjects”的包,我保留了序列化的对象类。

谢谢

1 个答案:

答案 0 :(得分:0)

  

当我传递我的BuyListing对象时,我在服务器端获得的对象只是“null”

如果收到空值,则发送空值。