簡體   English   中英

服務器不通過ObjectOutputStream接收序列化的對象

[英]Server does not receive serialized objects through ObjectOutputStream

我正在編寫一個Android客戶端,該客戶端將序列化的“ BuyListing”對象發送到我的http servlet。 我正在使用ObjectOutputStream從客戶端發送,而ObjectInputStream在服務器上接收。 當服務器收到序列化的對象時,它將嘗試使用instanceof將其識別為BuyListing,然后從那里對其進行排序。

當我通過對象流傳遞字符串時,我當前的實現工作正常,但是當我傳遞BuyListing對象時,在服務器端獲得的對象只是“空”。 我可以弄清楚為什么會發生這種情況,並且我懷疑這可能與我不了解的序列化有關。

這是我的客戶端代碼,用於發送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...

地點和用戶也是序列化對象,這里是用戶

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

除了擴展Listing包sharedObjects之外,BuyListing對象現在為空。

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

}

無論如何,我可以成功地將一個字符串傳遞給ConnectToServer.sendListing,它將起作用,但是當我傳遞一個BuyListing對象時卻不能。

我還在某處讀到,如果類在不同的程序包中,它們是不相同的。 我不確定這到底意味着什么,但是我的服務器端和客戶端都具有名為“ sharedObjects”的程序包,其中保留了序列化的對象類。

謝謝

當我傳遞我的BuyListing對象時,我在服務器端獲得的對象只是“空”

如果收到空值,則發送空值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM