• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps application hangs at .readObject() and no streaming occurs

manish411

Lurker
I was able to instantiate ObjectInputStream but now my program hangs at .readObject() 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.
Below are the code snippets from the andriod appication client side
This is the class whose object is to be send
Code:
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 to the client
Code:
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:
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:
    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");
  


      ObjectInputStream obin = new ObjectInputStream(staticsocket.i);
        System.out.println("Initialized objecinputstream");
//THIS IS WHERE THE EXECUTION STOPS
     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 now stops at
Code:
  FileClass file = (FileClass)obin.readObject();
 
If there is nothing to read from the stream, it will wait for something to become readable. If this is what you mean by "hang", then I would first make sure that something is actually there to be read.
 
Back
Top Bottom