Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - ObjectInputStream keeps on reading the previous value even with .reset()
    text
    copied!<p>I'm building an application for sending files between two Android phones, now i have a ListActivity that retrieves the sdcard and lists the files, when the ListActivity first starts on the two devices a ServerSocket is set up and listening with <code>.accept()</code> ...</p> <p>this thread starts when the ListActivity starts :</p> <pre><code>ReceiveFileSendRequestThread ReceiveFileSendRequestThread = new ReceiveFileSendRequestThread(); ReceiveFileSendRequestThread.start(); </code></pre> <p>and here is the full thread class:</p> <pre><code>static public class ReceiveFileSendRequestThread extends Thread { public void run() { ServerSocket serverSocket; try { serverSocket = new ServerSocket(6789, 200); connectionServ = serverSocket.accept(); requestFileInServer = new ObjectInputStream( connectionServ.getInputStream()); requestFileString = (String) requestFileInServer.readObject(); handler.post(new AcceptFileSendAlertDialogRunnable()); while (okToSend == null) { } if (okToSend == true) { requestFileOutServer = new ObjectOutputStream( connectionServ.getOutputStream()); requestFileOutServer.writeObject("oktosend"); requestFileOutServer.flush(); serverSocket.close(); // // Receive File } else if (okToSend == false) { requestFileOutServer = new ObjectOutputStream( connectionServ.getOutputStream()); requestFileOutServer.reset(); requestFileOutServer.writeObject("notosend"); requestFileOutServer.flush(); serverSocket.close(); ReceiveFileSendRequestThread ReceiveFileSendRequestThread = new ReceiveFileSendRequestThread(); ReceiveFileSendRequestThread.start(); } } catch (IOException e) { Log.d("Connection Error:", "Error binding port!"); e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } </code></pre> <p>And when onLongClick on an item (to send file) the following thread starts:</p> <pre><code>public boolean onItemLongClick(AdapterView&lt;?&gt; parentView, View childView, int position, long id) { // TODO Auto-generated method stub fileClickedName = (((TextView) childView).getText()).toString(); fileClickedPath = file.toString() + "/" + fileClickedName; fileClickedFile = new File(fileClickedPath); SendFileThread SendFileThread = new SendFileThread(); SendFileThread.start(); return true; } </code></pre> <p>SendFile Thread: </p> <pre><code>static public class SendFileThread extends Thread { public void run() { Socket socket = new Socket(); try { socket.connect(new InetSocketAddress(sharedIp, 6789), 200); requestFileOutClient = new ObjectOutputStream( socket.getOutputStream()); requestFileOutClient.writeObject(fileClickedName); requestFileOutClient.flush(); requestFileInClient = new ObjectInputStream( socket.getInputStream()); toSendOrNot = (String) requestFileInClient.readObject(); if (toSendOrNot.equals("oktosend")) { socket.close(); } else if (toSendOrNot.equals("notosend")) { socket.close(); ReceiveFileSendRequestThread ReceiveFileSendRequestThread = new ReceiveFileSendRequestThread(); ReceiveFileSendRequestThread.start(); handler.post(new ClientFileSendAlertDialogRunnable()); } // // } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } </code></pre> <p>Now, when I launch the ListActivity and longClick on an item, the the file name is sent to the second device, the second device pops up an alertDialog asking the user if to accept the file or not, (accepting file is still not ready for now) if the user presses on CANCEL (on the ReceiveFileSendRequestThread) a string is sent to the first device <code>"notosend"</code>, the first user receives the string <code>"notosend"</code> and depending on that the thread closes the socket and re invoke the thread to listen to the second peer if he wants to send another file , AND pops up an alertDialog telling the first user that your file was refused to be received ... >>>> this is totally works >>>> but when the first device attempts to send another file (long press again on a file [item on the list] ) the second user receives the new file name selected by the first user successfully and alertDialog pops up if to accept or cancel BUT the first user receives that the file send was refused ... without having the second user pressing on the cancel button !!! ... i don't know why <code>toSendOrNot = (String) requestFileInClient.readObject();</code> keeps on taking the previous value without even waiting for the second device to write the new object.</p> <p>I would appreciate it if someone could help me with this , Many thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload