How to get last created file from an FTP directory? - android

I first want to get a list of files stored in an FTP directory and then get the name of last created file using timestamp. And I'm getting an alert box: Activity is not responding. After checking logcat entry, I notice that the code never reach line :
Log.e("FTP", "number of filenames: " + count);
But I get to Log.e("FTP", "Connexion successful "); So connexion to the server seems ok.
It Seems like something going wrong out there. Can someone help me deal with it. Or show me a simple way to get the last created file from an the FTP server director?
FTPClient ftpClient = new FTPClient();
try
{
ftpClient.connect(InetAddress.getByName(Fonctions.address), Integer.parseInt(Fonctions.port));
if (ftpClient.login(Fonctions.login, Fonctions.pass))
{
Log.e("FTP", "Connexion successful ");
String workDir = ftpClient.printWorkingDirectory();
//Log.e("FTP", "workdir:" + workDir);
int count = ftpClient.listNames().length;
Log.e("FTP", "number of filenames: " + count);
FTPFile [] dossier = new FTPFile[count];
FTPFile back = new FTPFile();
dossier = ftpClient.listDirectories("Sarelo_FTP");
back = dossier[0];
Log.e("FTP", "Avant boucle " + back);
int buf = 0;
for (int i=0;i<(dossier.length) - 1;i++)
{
for (int j=1;j<dossier.length;j++)
{
buf = back.getTimestamp().compareTo(dossier[j].getTimestamp());
if (buf == -1)
back = dossier[j];
}
}
Log.e("FTP", "fichier final le plus récent: " + back.getName());
}
else{
Log.e("Restore FTP", "Error while connecting to FTP server");
}
}
catch(IOException e)
{
String title = "Error connecting to FTP server";
String msg = "Please check your parameters and connexion info: login, password,port number";
f.alert(c, title, msg).show();
Log.e("Restore FTP", "Error while connecting to FTP server", e);
}
P.S: I can't get the list of files in the directory so, I don't know if my code to retrieve the last created file is working. Any help on that would also be appreciated.
[Edit] This is my AsyncTask to retrieve the list of files in the directory. But it's still not working. I'm not getting Application Not Responding anymore, but It not seems to do anything else. Execution get stuck at the same point (can't reach Log.e("FTP", "number of filenames: " + count); )
class getFilesFromFtp extends AsyncTask
{
#Override
protected String doInBackground(Object... params)
{
int count = 0;
try
{
Log.e("FTP", "avant names: " + count);
count = ftpClient.listNames().length;
Log.e("FTP", "names: " + count);
handler.sendMessage(handler.obtainMessage());
}
catch (IOException e) {
Log.e("FTP", "Error getting number of files ", e);
}
return null;
}
}
Thanks for help.

You must not execute long running code on UI thread. Thia blocks UI redraw and event handling. It also produces ANR.
You should run it in the background thread, preferably via 'AsyncTask'.

First problem solved. I just needed to activate FTP data connection passive mode like that:
ftpClient.enterLocalPassiveMode();
before line int count = ftpClient.listNames().length;.
Hope this will help other people. Thanks to #Peter Knego driving me to AsynkTask. I learned something new. :)

Related

Empty Android log file using FileHandler()

I'm trying to set up a log handler to output the Android log to file to external storage. The code below creates the log file, but no output is sent to the file, so something is obviously wrong with how the handler is configured. Or, perhaps this arrangement cannot be expected to work at all?
The function is called in onCreate() from the main activity.
private void logToFile(String path) {
try {
// Get package name
String packageName = MainActivity.class.getPackage().getName();
String logfileName = path + "/" + packageName + ".log";
Logger logger = Logger.getLogger(packageName);
logger.setLevel(Level.FINE);
FileHandler fileTxt = new FileHandler(logfileName);
SimpleFormatter formatterTxt = new SimpleFormatter();
fileTxt.setFormatter(formatterTxt);
logger.addHandler(fileTxt);
Toast.makeText(this, "Logging to " + logfileName, Toast.LENGTH_LONG).show();
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
Log.i(TAG, "logging to filesystem enabled");
}
To write to the logger declared above (and, thus, the attached handler which writes to a file), the following should be used instead of Log.i(TAG, "message")
private static final Logger logger = Logger.getLogger(TAG);
public void someFunction() {
logger.info("message")
}
These log messages will also appear in logCat/debugger, with the supplied TAG.
P.S. Java logging makes my head hurt...
I was frustrated at having to use Logger instead of standard Logcat Log.d(), Log.e(), etc. so I started using this Frankenstein's monster solution of reading from Logcat into a LogRecord and saving that using FileHandler.
This means you can limit the log file size easily, and retain your detailed Android logs.
But this isn't going to give you continuous output to file. If you don't mind pressing a button or calling it once a session though, then it shouldn't really matter since Logcat is constantly updated anyway.
(I strongly recommend calling from a non-UI thread.)
FileHandler fh=null;
String name;
if ( 0 == Environment.getExternalStorageState().compareTo(Environment.MEDIA_MOUNTED))
name = Environment.getExternalStorageDirectory().getAbsolutePath();
else
name = Environment.getDataDirectory().getAbsolutePath();
name += "/yourapp/yourapp";
try {
fh = new FileHandler(name, 1024*1024, 7, true); //Limit to 7 x 1MB files.
fh.setFormatter(new SimpleFormatter());
//Try to read Logcat.
try {
//Dumps the entire logcat to std output.
Process processD = Runtime.getRuntime().exec("logcat -v long -d");
BufferedReader bufferedReaderD = new BufferedReader(new InputStreamReader(processD.getInputStream()));
String lineD;
while ((lineD = bufferedReaderD.readLine()) != null){
//Send to the file handler.
fh.publish(new LogRecord(Level.ALL, lineD));
}
//Clear the logcat storage. Don't feel like rewriting old records.
Process processC = Runtime.getRuntime().exec("logcat -c");
} catch (IOException e) {
Log.e(TAG, "Could not get Logcat logs.");
e.printStackTrace();
}
} catch (Exception e) {
Log.e("MyLog", "FileHandler exception", e);
} finally {
if (fh != null)
fh.close();
}

Strange bluetooth locking behavior on Samsung Express

I am having a strange issue with reading an writing to/from the Bluetooth Socket on my Samsung Express (4.1.2). The issue does not present itself when I run the App on my Google Nexus 4 (4.3)
When I attempt to do any action on the socket i get a "genlock attach lock open" msg in my Logcat. Normally followed quickly with a "genlock close" . The issue here is that if I attempt to write onto the socket I get a "genlock close" , the data is sent successfully, but I cannot access new data in the input stream until the lock opens again.
Also there are numerous "availableNative" messeges popping up in the Logcat.
Here is the code where I send a command , wait for 0.5 second and attempt to read the input buffer:
public boolean ReadFaultTable()
{
byte[] in_packet = new byte[100];
int err_byte_count;
int err_symp_count;
if(!(BTCheck()))
{
Log.i(TAG,"ReadFaultTable() BT Fail!");
//Toast.makeText(getApplicationContext(), "Socket not open for fault read", Toast.LENGTH_SHORT).show();
return false;
}
byte [] out_packet = new byte[1];
out_packet[0]= 0x0a;//READ_CURRENT_FAULT_CODES_CMD
byte []full_packet=AddDRCheckSum(out_packet);
connectedThread.clearBuffer();
try{
Thread.sleep(200);
}
catch(InterruptedException e){}
connectedThread.write(full_packet);
Log.i("Read Faults", "After Write");
sleepForProcessingTime(500);
Log.i("Read Faults", "0.5 Secs, Num Bytes = "+Integer.toString(connectedThread.bytesAvail()));
if(connectedThread.bytesAvail() > 3)
{
//Log.i("Read Faults", "bytes avail = " + Integer.toString(connectedThread.bytesAvail()));
for (int i =0;i<3;i++)
{
Log.i("Read Faults", "In here");
if((in_packet[i]=connectedThread.readByte())==-1)
{
Log.i(TAG, "End of Input stream reached on iteration= "+ Integer.toString(i));
return false;
}
Log.i("Read Faults", "Byte read in =" +Integer.toHexString(in_packet[i]));
}
}
else
{
Log.i("Read Faults", "No Bytes to read .. return false");
return false;
}
/*
if(connectedThread.bytesAvail() > 0)
{
in_packet= new byte[connectedThread.bytesAvail()];
in_packet = connectedThread.read();
Log.i("Read Faults","Bytes Read in =" + printCMD(in_packet));
}
*/
Log.i("Read Faults", "Made it here with "+ printCMD(in_packet));
int fault_table_index =0;
if (in_packet[0] == 0x0A && in_packet[1] == 0x0E )
{
Log.i("Read Faults","in packet passed checks");
err_byte_count=in_packet[2];
fault_table= new boolean[err_byte_count*8];
Log.i("Read Faults","err byte count = "+ Integer.toString(err_byte_count));
for (int i=0;i<(err_byte_count);i++)
{
int val =connectedThread.readByte();
Log.i("Read Faults","Next byte read in =0x" + Integer.toHexString(val));
in_packet[3+i]=(byte)val;
int mask =0x01;
for (int x = 0; x < 8; x++)
{
if ((val & mask)== mask)
{
Log.i("Read Faults","Fault Index "+ Integer.toString(fault_table_index +1) + "true");
fault_table[fault_table_index++]=true;
}
else
{
Log.i("Read Faults","Fault Index "+ Integer.toString(fault_table_index +1) + "false");
fault_table[fault_table_index++] = false;
}
mask <<= 1;
}
}
Log.i("Read Faults","Made it to here");
in_packet[3 + err_byte_count] = (byte)connectedThread.readByte();
err_symp_count = in_packet[3 + err_byte_count];
symp_table = new byte[err_symp_count];
for (int i=0; i < err_symp_count; i++)
{
in_packet[3 + err_byte_count + 1 + i] = (byte)connectedThread.readByte();
symp_table[i] = in_packet[3 + err_byte_count + 1 + i];
}
return true;
}
else
{
return false;
}
}
If I was to run this command once the bytes will never reach the inputStream , but if i ran the command again, all of the bytes are there from the first time, it's like a flood gate gets opened. I'm stumped. Can anyone help ? (Note the write and read commands all work else where in the app and the Bluetooth socket is functioning for other commands and responses .
Please ask if you require further info.
Just found a work around. For whatever reason the phone did not like the idea of reading more than one byte off the Bluetooth Socket at a time. So I implemented a byte by byte approach that has provided an acceptable patch. I didn't pick up on the socket reporting one byte available before because I was checking for at least 3 bytes on the socket before reading in (this makes sense for my purposes).
Phone continually reports 1 byte available on the inputStream, so you take a byte while the inputStream reports one.
Would still be interested in an explanation if someone has one. Thanks

Android client socket doesn't send data after some time

I'm developing an Android real-time-data app that sends data (floats and ints) to a server on the local subnet via a TCP socket. The problem I'm facing is that after sending some data simultaneously the socket doesn't send anymore data at all. I debugged the app and it shows that data is being sent but doesn't show up on the server. After this happens if I close the connection the server doesn't even get the notification that the connection has been terminated which it should according to my design model. Meanwhile I get an exception on the app saying it can not write to a broken pipe. This tells me that the problem is with the app because I also did test using a desktop app and I can send huge amounts of data to the server and it gets delivered.
And please keep in mind that the data size I'm talking about here is 252 bytes per packet.
Here's my class I'm using. (This runs in an AsyncTask object )
public class Network
{
private Socket handle;
public static enum TASK
{
TASK_CONNECT, TASK_SEND, TASK_CLOSE
}
public Network()
{
}
public String lastError = "";
public boolean Connect(String host, int port)
{
try
{
lastError = "Connecting to server.";
handle = new Socket(host, port);
handle.setTcpNoDelay(true); //
handle.setSendBufferSize(SIZE_OF_PACKET); ///==> These don't seem to help at all
handle.setKeepAlive(true); ///
return true;
}catch(IOException e)
{
lastError += e.getMessage() != null ? " "+ e.getMessage() : "";
return false;
}
}
private void err(String e){
System.err.println(e);
}
private boolean SendPacket(byte buffer[])
{
OutputStream oStream = null;
err("sending: " + buffer.length + " bytes");
try
{
lastError = "Obtaining output stream.";
oStream = handle.getOutputStream();
lastError = "Error sending data.";
oStream.write(buffer);
oStream.flush();
return true;
}catch(Exception e)
{
lastError += e.getMessage() != null ? " "+ e.getMessage() : "";
}
return false;
}
public void Close()
{
try{ handle.close(); handle = null; }catch(Exception e){} // swallow exception
}
}
I send my data in a loop depending on how many numbers I have. I tried a Google search but didn't find anything relevant. Has anyone experienced this before? It's making me mad now.
EDIT: Wireshark shows incoming "red" packets that don't reach the desktop app (server)
Look at this picture.
You can see the first few have Len > 0 the red ones have 0.
I think it's time Google interfaced the USB so we can use it. At least that'd would have been my first option.
Should you not be calling oStream.close() after you flush the stream, given that you never use it again?
Also, you say that this is being run in an AsyncTask object. Is it possible that multiple threads could be attempting to send packets at the same time? If so, you might need some form of synchronisation around the SendPacket method.
Ok. I solved the issue by using UDP instead. Thank you all.
But I still didn't find the source of the problem.

Android TCP app hanging on inStream.readline()

This is a continuation of this question because it my orginal question was answered, but it did not solve the bug.
Question:
How do I fix the code hanging on this line inStream.readline()
My Intent:
This is in a thread that will loop through checking if there is an outMessage, if there is, it will send the message.
Next it will check it if there is anything in the in-stream, if there is, it will send it to the handler in my main activity.
Lastly, it will sleep for 1 second, then check again.
This should allow me to read/write multiple times without needing to close and open the socket.
Problem:
It is reading and writing better, but still not working properly
What is happening now:
If outMessage is initialized with a value, upon connection with the server, the socket:
writes and flushes the value (server receives & responds)
updates value of outMessage (to null or to "x" depending on how i have it hard-coded)
reads and shows the response message from the server
re-enters for the next loop
IF i set outMessage to null, it skips over that if statements correctly then hangs; otherwise, if i set outMessage to a string (lets say "x"), it goes through the whole if statement, then hangs.
The code it hangs on is either of the inStream.readline() calls (I currently have one commented out).
Additional info:
- once connected, I can type in the "send" box, submit (updates the outMessage value), then disconnect. Upon re-connecting, it will read the value and do the sequence again until it get stuck on that same line.
Changes since the referenced question:
- Made outMessage and connectionStatus both 'volatile'
- added end-of-line delimiters in neccesary places.
Code:
public void run() {
while (connectionStatus != TCP_SOCKET_STATUS_CONNECTED) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while (connectionStatus == TCP_SOCKET_STATUS_CONNECTED) {
try {
if (outMessage != null){
OutStream.writeBytes(outMessage + "\n");
OutStream.flush();
sendMessageToAllUI(0, MAINACTIVITY_SET_TEXT_STATE, "appendText" , "OUT TO SERVER: " + outMessage);
outMessage = "x";
}
Thread.sleep(100);
// if (InStream.readLine().length() > 0) {
String modifiedSentence = InStream.readLine();
sendMessageToAllUI(0, MAINACTIVITY_SET_TEXT_STATE, "appendText" , "IN FROM SERVER: " + modifiedSentence);
// }
Thread.sleep(1000);
} catch (IOException e) {
connectionLost();
break;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
The thread that makes the socket:
public void run() {
setName("AttemptConnectionThread");
connectionStatus = TCP_SOCKET_STATUS_CONNECTING;
try {
SocketAddress sockaddr = new InetSocketAddress(serverIP, port);
tempSocketClient = new Socket(); // Create an unbound socket
// This method will block no more than timeoutMs. If the timeout occurs, SocketTimeoutException is thrown.
tempSocketClient.connect(sockaddr, timeoutMs);
OutStream = new DataOutputStream(tempSocketClient.getOutputStream());
InStream = new BufferedReader(new InputStreamReader(tempSocketClient.getInputStream()));
socketClient = tempSocketClient;
socketClient.setTcpNoDelay(true);
connected();
} catch (UnknownHostException e) {
connectionFailed();
} catch (SocketTimeoutException e) {
connectionFailed();
} catch (IOException e) {
// Close the socket
try {
tempSocketClient.close();
} catch (IOException e2) {
}
connectionFailed();
return;
}
}
Server:
public static void main(String[] args) throws IOException {
String clientSentence;
String capitalizedSentence;
try {
ServerSocket welcomeSocket = new ServerSocket(8888);
SERVERIP = getLocalIpAddress();
System.out.println("Connected and waiting for client input!\n Listening on IP: " + SERVERIP +"\n\n");
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
while(true)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
clientSentence = inFromClient.readLine();
System.out.println("clientSentance == " + clientSentence);
String ip = connectionSocket.getInetAddress().toString().substring(1);
if(clientSentence != null)
{
System.out.println("In from client ("+ip+")("+ System.currentTimeMillis() +"): "+clientSentence);
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence + '\n');
System.out.println("Out to client ("+ip+"): "+capitalizedSentence);
}
}
} catch (IOException e) {
//if server is already running, it will not open new port but instead re-print the open ports information
SERVERIP = getLocalIpAddress();
System.out.println("Connected and waiting for client input!\n");
System.out.println("Listening on IP: " + SERVERIP +"\n\n");
}
}
Thanks in advance!
Edits:
added the server code after updating
I tried messing around with setting the SoTimout for the socket but took that back out
Your server is specifically designed to receive exactly one line from a client and send exactly one line back. Look at the code:
while (true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(
connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
String ip = connectionSocket.getInetAddress().toString()
.substring(1);
System.out.println("In from client (" + ip + "): "
+ clientSentence);
if (clientSentence != null) {
capitalizedSentence = clientSentence.toUpperCase() + '\n';
System.out.println("Out to client (" + ip + "): "
+ capitalizedSentence);
outToClient.writeBytes(capitalizedSentence + "\n");
}
Notice that inside the loop it accepts a new connection, reads exactly one line, and then writes exactly one line. It doesn't close the connection. It doesn't sanely end the conversation. It just stops reading.
A client that worked with this server would have to connect, send exactly one line, read exactly one line back, and then the client would have to close the connection. Your client doesn't do that. Why? Because you had no idea that's what you had to do. Why? Because you had no design ... no plan.
So that's your specific issue. But please, let me urge you to take a huge step back and totally change your approach. Before you write a single line of code, please actually design and specify a protocol at the byte level. The protocol should say what data is sent, how messages are delimited, who sends when, who closes the connection, and so on.
Otherwise, it's impossible to debug your code. Looking at the server code above, is it correct? Well, who knows. Because it's unclear what it's supposed to do. When you wrote the client, you assumed the server behaved one way. Was that assumption valid? Is the server broken? Who knows, because there's no specification of what the server is supposed to do.
You need to check if there is data available:
if (InStream.available > 0) {
String modifiedSentence = InStream.readLine();
sendMessageToAllUI(0, MAINACTIVITY_SET_TEXT_STATE, "appendText" , "IN FROM SERVER: " + modifiedSentence);
}
But to be honest, even that is not ideal because you have no gurantee that the eond-of-line will have been received. If the server sends a few bytes but never sends the end-of-line then you will still be blocking forever. Production socket code should never rely on readLine but instead read into a buffer and check that buffer for end-of-line (or whatever criteria your protocol needs).
Didn't read closely enough, I thought InStream was an InputStream instance. InputStream has available. InputStreamReader has ready (which in turn calls InputStream.available. As long as you keep a refernce to either of these then you can see if data is available to be read.

Problem Reading Saved File : Android

I am read & write username & password to android device using my application to internal storage. I am successful writing to the device, but while reading I get error. On start of the aplication I read that file and I get "The applicaton (...) has stopped unexpectedly. Please try again." with "Force Close" button and the app closes. I tried different ways to read, but all showed same results. I write my data as username + "\n" + passwrd + "\n". This is the code for reading data :
private static String ReadFromFile(String fileName) {
String text = "";
FileInputStream fis = null;
byte[] fileData;
try {
StringBuffer sb = new StringBuffer();
int c = 0;
fis = AppContext.openFileInput(fileName);
if (fis.available() != 0) {
fileData = new byte[fis.available()];
c = fis.read(fileData);
Log.i(TAG, "Read Byes = " + c );
java.util.StringTokenizer stk = new java.util.StringTokenizer(new String(fileData), "\n");
text = stk.toString();
} else
throw new IOException("fis.available() <= 0"); /*
c = (char)fis.read();
while (c != -1){
if (c != -1)
sb.append(c);
c = (char)fis.read();
}
text = sb.toString();
*/
fis.close();
} catch (FileNotFoundException e) {
text = "null " + e.getMessage();
} catch (IOException e) {
text = "null " + e.getMessage();
} finally {
fis = null;
}
return text;
}
/*
try {
StringBuffer sb = new StringBuffer();
RandomAccessFile raf = new RandomAccessFile(ENC_LOGIN_FILE, "r");
while ((str1 = raf.readLine()) != null){
sb.append(str1);
}
str1 = sb.toString();
} catch (FileNotFoundException e) {
errorMessage = "File Not Found: " + e.getMessage();
//e.printStackTrace();
} catch (IOException ioe) {
errorMessage = "IOExcp: " + ioe.getMessage();
//e.printStackTrace();
}*/
No path is used and the filename directly is given to write & read file. I checked out Problem facing in reading file from Internal memory of android from where I tried StringTokenizer.
Can anyone help me know where am I going wrong. The following permission are set :
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"></uses-permission>
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS"></uses-permission>
<uses-permission android:name="android.permission.READ_SYNC_STATS"></uses-permission>
<uses-permission android:name="android.permission.READ_LOGS"></uses-permission>
Also how do I debug the application. How to check the data written in Log. If I use SOP, where does it dispaly - it doesn't display in Console window ? Please help me, I am n newbie in android development.
One more thing to clarify : My motto is to store username and password in such a way that user must not be able to read them directly. I have already used MODE_PRIMITIVE. Is their a need to use JSONObject to save this data. I had a look at JSON in couple of sites & API but couldn't make out why and where it should be used. Using MODE_PRIMITIVE I believe even the user can't read the file. Only my application can read it. Then I don't think I should use JSON. Please correct me if I am wrong.
Any help is highly appreciated.
Thanks.
A suggestion:
If you are only saving username and password then use SharedPreferences instead of saving the data in a file. It is a much secure way and plus very hassle free.
Use this blog article on How to use Shared Preferences:
http://saigeethamn.blogspot.com/2009/10/shared-preferences-android-developer.html
fis = AppContext.openFileInput(fileName);
fileData = new byte[fis.available()];
java.util.StringTokenizer stk =
new java.util.StringTokenizer(new String(fileData), "\n");
It appears to me that you're trying to create a new String using an uninitialized array as input. You've asked for the array to be a specific size, but you haven't done anything to read data into the array. (Note that available() only asks for the size of the data that can be read without blocking; it doesn't actually read the data. For that you need the read() method.)
Try this:
fis = AppContext.openFileInput(fileName);
fileData = new byte[fis.available()];
fis.read(fileData);
java.util.StringTokenizer stk =
new java.util.StringTokenizer(new String(fileData), "\n");
You should check the return value of fis.read(), but give this a try to see if it lets you progress further.

Categories

Resources