How to save data persistend so I can use them offline - java

So I have one question, I have to do a chat application in school, with a feature which allows me to see my old conversations offline like in WhatsApp.
It kind of works after I turn immediately internet off and when I start then my app I can see my conversations, but after some time the chats are not loading anymore. I searched a bit on the internet and have read I have to save the chat as a file to the internal storage, but how do I do it I couldn't find anything about that.
This is my code for loading my messages online and offline
public void loadConversations() {
boolean result;
try {
FileInputStream fileInputStream = getApplicationContext().openFileInput("CONVERSATIONS");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
this.conversations = (HashMap<String, ArrayList<Message>>) objectInputStream.readObject();
objectInputStream.close();
fileInputStream.close();
result = true;
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
result = false;
}
if (this.conversations == null) {
this.conversations = new HashMap<>();
}
Log.d(TAG, "loadConversations result: " + result + " size: " + this.conversations.size());
}
public void persistConversations() {
boolean result;
try {
FileOutputStream fileOutputStream = getApplicationContext().openFileOutput("CONVERSATIONS",getApplicationContext().MODE_PRIVATE);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(this.conversations);
objectOutputStream.flush();
objectOutputStream.close();
fileOutputStream.close();
result = true;
} catch (IOException e) {
e.printStackTrace();
result = false;
}
Log.d(TAG, "persistConversations result: " + result);
}
maybe someone can give me an pointer.

There is the best way to make a chat app with firebase real-time database and when a user is offline (No internet connection). Firebase created a local database and store all the details in local and show your user-details and your messages. You don't need to create a separate database. Follow this link to know about firebase realtime database :- https://firebase.google.com/docs/database/android/start/?gclid=EAIaIQobChMInMag3qiV4wIVAh4rCh2fJQWAEAAYASABEgIfFPD_BwE

Related

Apache commons FPTSClient explicit transfer file is incomplete

Iam currently struggling a little bit with the FTPSClient from Apache Commons. See code down below. I try to write a file to FTP Server (vsftpd) when using FTPClient things are working perfectly fine. When using my code snippet I will always get a 451 Error, when debugging and waiting after Util.copyStream() returned everything works fine or settings a Thread.sleep(100). This also does not happen when I do not set the ftpsClient.execProt("P). Does anyone know by what this could be caused.
final FTPSClient client;
client = new FTPSClient("TLS", false);
client.setUseClientMode(true);
client.setDefaultPort(21);
// connect
try {
client.connect("serverAddress", 21);
} catch (SSLException e) {
throw e;
}
// setup any after connected
client.setSoTimeout(300);
client.setListHiddenFiles(true);
client.enterLocalPassiveMode();
FTPClientConfig ftpConfig;
try {
ftpConfig = new FTPClientConfig(client.getSystemType());
} catch (FTPConnectionClosedException e) {
throw e;
} catch (IOException e) {
ftpConfig = new FTPClientConfig();
}
client.configure(ftpConfig);
final FTPSClient ftpsClient = client;
// remove data buffer limit
ftpsClient.execPBSZ(0);
// set data channel encrypted
ftpsClient.execPROT("P");
client.login("user", "password");
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
throw new IOException("Authentication failed: " + client.getReplyString().trim());
}
// postconfigure connection
if (!client.setFileTransferMode(FTP.STREAM_TRANSFER_MODE) || !client.setFileType(FTP.BINARY_FILE_TYPE)) {
throw new IOException("Failed to correctly configure client: " + client.getReplyString().trim());
}
InputStream input;
OutputStream output;
input = new FileInputStream(pathToLocalFile);
output = client.storeFileStream("foobar.txt");
final var number = Util.copyStream(input, output);
System.out.println(number);
input.close();
// Thread.sleep(100);
output.close();
// Must call completePendingCommand() to finish command.
if (!client.completePendingCommand()) {
client.logout();
client.disconnect();
System.err.println("File transfer failed.");
}
This library has been around for a long time, and things change a bit under the hood. Try:
input = new FileInputStream(pathToLocalFile);
boolean result = client.storeFile("foobar.txt", input);
if (result) {
System.out.println("\tFile Transfer Completed Successfully");
}
I have noticed that every once in a while when transferring files to a mainframe, it won't complete. I think it has something to do with the file length, but I've never been able to track it down. I also don't use the stream_transfer_mode.

Arduino HC-05 writes to serial monitor but not Bluetooth (Android)

Greetings Stack Overflow community,
I am writing an android app that reads some data off of an HC05 bluetooth module.
I am just trying to read one byte but it just hangs there for a few minutes before it crashes. Maybe its a hardware problem, but perhaps there is some error in my code I do not see. If anybody has a clue as to why I would not be able to read from this module I would greatly appreciate the input.
Thanks!
for (BluetoothDevice iterator : bondedDevices) {
if (iterator.getName().equals("HC-05")) {
BluetoothDevice device = iterator;
try { btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);} catch (IOException ex) {}
try {btSocket.connect();} catch (IOException ex) {}
if (btSocket.isConnected()) {
Toast.makeText(getApplicationContext(), "Connected to HC05", Toast.LENGTH_SHORT).show();
//Write 'r' to arduino to tell it to send data.
try {
outStream = btSocket.getOutputStream();
outStream.write('r');
} catch (IOException ex) {}
int byteCount = 1;
byte[] rawBytes = new byte[byteCount];
try {
inputStream= btSocket.getInputStream();
int a = inputStream.read(rawBytes, 0, 1);
}
Did you try using a Bluetooth terminal app for android? This way you can find out if it's a hardware or software issue.
You could try this app:
https://play.google.com/store/apps/details?id=Qwerty.BluetoothTerminal

Android File.exists() is always true

I'm trying to copy files from the assets folder to the device folder using this function:
public static void copyJSON(Context aContext) {
AssetManager assetManager = aContext.getResources().getAssets();
String[] pFiles = null;
try {
pFiles = assetManager.list("ConfigurationFiles");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
if (pFiles != null) for (String pJsonFileName : pFiles) {
InputStream tIn = null;
OutputStream tOut = null;
try {
tIn = assetManager.open("ConfigurationFiles" + File.separator + pJsonFileName);
String[] pList = aContext.getFilesDir().list(); //just for test
File pOutFile = new File(aContext.getFilesDir(), pJsonFileName);
tOut = new FileOutputStream(pOutFile);
if (pOutFile.exists()) {
copyFile(tIn, tOut);
}
} catch (IOException e) {
Log.e("tag", "Failed to copy asset file: " + pJsonFileName, e);
} finally {
if (tIn != null) {
try {
tIn.close();
} catch (IOException e) {
Log.e("tag", "Fail closing", e);
}
}
if (tOut != null) {
try {
tOut.close();
} catch (IOException e) {
Log.e("tag", "Fail closing", e);
}
}
}
}
}
If I delete the App and run the code, the variable pList is empty as I expect but the pOutFile.exists()returns true ALWAYS!!.
I don't want to copy them again every time I open my App, and I'm doing this because all my app uses JSON to navigate thru all the screens, so If I change any value in my BBDD a WS send a new JSON file and the App respond in accordance for example a button is no longer needed, so the first time you download my App I copy the original JSON and then if you use the app an if you have internet connection you will download a new JSON file that it is more accurate than the one that is in the Bundle and it will be override, this is because as far as I know I can't change the files that are in the assets folder.
I have read everywhere and all say the same use this:
File pOutFile = new File(aContext.getFilesDir(), pJsonFileName);
And then ask for this:
pOutFile.exists()
I don't know what I'm doing wrong.
Thanks for all your help.
put it this way:
File pOutFile = new File(aContext.getFilesDir(), pJsonFileName);
if (pOutFile.exists()) {
tOut = new FileOutputStream(pOutFile);
copyFile(tIn, tOut);
}
and everything should work fine. Remember the FileOutputStream creates the file it should stream to if possible and non existing
The problem is you're essentially creating a file and then checking if it exists.
try {
tIn = assetManager.open("ConfigurationFiles" + File.separator + pJsonFileName);
String[] pList = aContext.getFilesDir().list(); //just for test
File pOutFile = new File(aContext.getFilesDir(), pJsonFileName);
// See here: you're creating a file right here
tOut = new FileOutputStream(pOutFile);
// And that file will be created in the exact location of the file
// you're trying to check:
if (pOutFile.exists()) { // Will always be true if FileOutputStream was successful
copyFile(tIn, tOut);
}
}
You should instead create your FileOutputStream AFTER you've done your existence check.
Source: http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
A file that you have just created without getting an exception always exists. The test is pointless. Remove it.

Why do I get FileNotFoundException when I create and try to write to file on Android emulator?

First off, I am not trying to write to the SDCard. I want to write some information to a file that persists between uses of the app. It is essentially a file to hold favorites of the particular user. Here is what the code looks like:
try {
File file = new File("favorites.txt");
if (file.exists()) {
Log.d(TAG, "File does exist.");
fis = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(fis));
}
else {
Log.d(TAG, "File does not exist.");
return favDests;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
When running this code, we always get the "File does not exist." message in our DDMS log.
We have also tried the following code to no avail:
try {
File file = new File(GoLincoln.FAV_DEST_FILE);
fis = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(fis));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
It is this second portion of code that results in the FileNotFoundException.
I have read multiple tutorials on writing and reading files on Android and I believe I am following them pretty closely, so I am not sure why this code doesn't work successfully. I appreciate any help!
You shouldn't use the File class directly. Use Activity.getCacheDir() to get the cache dir which is specific to your application. Then use new File(cachedir, "filename.tmp") to create the file.
Preferences and SQLLite will both allow you to have persistent data without managing your own files.
To use shared preferences you grab it from your context, then you edit the values like so
mySharedPreferences = context.getSharedPreferences("DatabaseNameWhateverYouWant", 0);
mySharedPreferences.getEditor().putString("MyPreferenceName", "Value").commit();
To get a preference out
mySharedPreferences.getString("MyPreferenceName", "DefaultValue");
This is really the simplest way to do basic preferences, much easier then doing a file. More then strings are supported, most basic data types are available to be added to the Preferences class.

How to cache/save custom class object in Android?

I want to save ArrayList<CustomClass>-object to somewhere in Android storage to retrieve quickly and display data in it.
Is this possible or not? If yes, then which technique will be suitable, SQLite or external storage?
example.
public class MyClass implements Serializable
{
private static final long serialVersionUID = 1L;
public String title;
public String startTime;
public String endTime;
public String day;
public boolean classEnabled;
public MyClass(String title, String startTime, boolean enable) {
this.title = title;
this.startTime = startTime;
this.classEnabled = enable;
}
public boolean saveObject(MyClass obj) {
final File suspend_f=new File(SerializationTest.cacheDir, "test");
FileOutputStream fos = null;
ObjectOutputStream oos = null;
boolean keep = true;
try {
fos = new FileOutputStream(suspend_f);
oos = new ObjectOutputStream(fos);
oos.writeObject(obj);
} catch (Exception e) {
keep = false;
} finally {
try {
if (oos != null) oos.close();
if (fos != null) fos.close();
if (keep == false) suspend_f.delete();
} catch (Exception e) { /* do nothing */ }
}
return keep;
}
public MyClass getObject(Context c) {
final File suspend_f=new File(SerializationTest.cacheDir, "test");
MyClass simpleClass= null;
FileInputStream fis = null;
ObjectInputStream is = null;
try {
fis = new FileInputStream(suspend_f);
is = new ObjectInputStream(fis);
simpleClass = (MyClass) is.readObject();
} catch(Exception e) {
String val= e.getMessage();
} finally {
try {
if (fis != null) fis.close();
if (is != null) is.close();
} catch (Exception e) { }
}
return simpleClass;
}
and calling from activity
if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"MyCustomObject");
else
cacheDir= getCacheDir();
if(!cacheDir.exists())
cacheDir.mkdirs();
MyClass m = new MyClass("umer", "asif", true);
boolean result = m.saveObject(m);
if(result)
Toast.makeText(this, "Saved object", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "Error saving object", Toast.LENGTH_LONG).show();
MyClass m = new MyClass();
MyClass c = m.getObject(this);
if(c!= null)
Toast.makeText(this, "Retrieved object", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "Error retrieving object", Toast.LENGTH_LONG).show();
dont forget to use write_external_storage permissions in manifest file.
This problem isn't really android specific. I mean if you know how to serialize your data either via java.io.Serializable or you have a custom persistence format, you just need to know where to store it.
You can grab a file on the local device via
FileOutputStream stream = null;
try{
stream = mContext.openFileOutput("name.data", Context.MODE_PRIVATE);
ObjectOutputStream dout = new ObjectOutputStream(stream);
dout.writeObject(myArrayList);
dout.flush();
stream.getFD().sync();
} catch(IOException e) { //Do something intelligent
} finally {
stream.close();
}
You will have to use openFileInput() later to read the data back.
Or you can grab External Storage. This is similar however you have to guarantee that it even exists. Like is the external storage connected and even able to be written to. Since you are writing a data structure here and usually external storage is world readable I don't think this is a good idea for your intended purposes (just from what you have put so far).
If your data is structured and is going to be queried many times and it might be rather big to always load up then consider using the sql lite tools that are part of the OS. However I am assuming you don't need this either as a simple list is just that, a linear structures which you could probably seek through in a file (assuming it is less than 1MB of data :)
This issue can be solved by singleton class here in which you can set/get any object eg arrayList in any time. If you used Android Service then this solutuion is not suitable.
Perhaps the solution to your problem can be found here.

Categories