cannot instantiate ObjectInputStream

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

cannot instantiate ObjectInputStream

Postby manish411 » Sun Apr 01, 2012 8:29 am

I am making an android app in which I want to transfer file from my server pc to my android phone. The server reads the file , encapsulates it into a bytearray and then writes it as an object of a class to the ObjectOutputStream.

This is the class whose object is to be send
Code: Select all
import java.io.Serializable;
public class FileClass implements Serializable{
public byte file[];
public FileClass(byte file[])
{
  this.file = file;   
}
}


this is the function for sending the object
Code: Select all
void sendvideoobject(String f)throws Exception
   {
      String filepath = f;
       
       File file = new File(filepath);
       byte[] mybytearray  = new byte[(int)file.length()];
       FileInputStream fin = new FileInputStream(filepath);
       fin.read(mybytearray);
       FileClass myfile = new FileClass(mybytearray);
       ObjectOutputStream ob = new ObjectOutputStream(out);
       ob.flush();
       ob.writeObject(myfile);
      ob.flush();
      System.out.println("Finished sending file");
}


All the streams are initialized in a staticsocket class as static objects at the time of connection in a separtate thread.
Code: Select all
package com.android;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.StreamCorruptedException;
import java.net.Socket;

import android.util.Log;

public class staticsocket{
public static Socket socket;
public static OutputStream o;
public static String servername;
public static DataOutputStream dout;
public static DataInputStream din;
public static ObjectInputStream obin;
public static ObjectOutputStream obout;   
public static InputStream i;
public static BufferedInputStream bi;
public static BufferedOutputStream bo;
public static void setsocket(InputStream in,OutputStream out)

   {
      try{
      i = in;
      o = out;
   
   
     dout = new DataOutputStream(o);
      din = new DataInputStream(i);
      bi = new BufferedInputStream(i);
      bo = new BufferedOutputStream(o);
      System.out.println("Finished intializing streams");
       
      }
      catch(Exception e)
      {
         e.printStackTrace();
      }
      }

}


This is the download function which is implemented at the client side at a separate thread running.
Code: Select all
   public void run() {
      // TODO Auto-generated method stub
       try
        {
          byte buffer[] = new byte[1024];
       String filepath =null;     
   
     System.out.println("started downloading");
     ObjectOutputStream obout = new ObjectOutputStream(staticsocket.o);
     obout.flush();
     System.out.println("Initialized objectoutputstream");
 

//THIS IS WHERE THE EXECUTION JUST HANGS
     ObjectInputStream obin = new ObjectInputStream(staticsocket.i);
        System.out.println("Initialized objecinputstream");
    FileClass file = (FileClass)obin.readObject();
          FileOutputStream of = new FileOutputStream("/mnt/sdcard/Raul.mp4");
     BufferedOutputStream bo = new BufferedOutputStream(of);
     bo.write(file.file);
           System.out.println("Finished receiving file");

   }
catch(Exception e)
{}
   }
}


The execution hangs just after Initialized objectoutputStream.
I have read that objectoutputstream on the other side has to be initialized before. But still after initializing the objectoutputstream on the server and flushing it the application on the client android is not able to intialize the objectinputstream. However the same algorithms works if I run the client application as a simple java code.
manish411
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Apr 01, 2012 8:26 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: Exabot [Bot] and 4 guests