Content Syncing on Android - java

I am writing an application for android and i wish to be able to sync content from a web server, IE: The layouts XML to the device and cache it so that if the device is not connected to the internet it uses an old version of the XML so as soon as the device is on the internet is able to update and gather the latest XML.
Couldn't find much on the internet, so if someone could point me in the right direction or give me some ideas this would be quite helpful.
Cheers,
Alex

This is how it works with a normal xml file:
Create an on your device HTTP Client
Prepare a GET request to your server
Send the request and retrieve the HTTP Response
Read the content using an InputStreamReader
Write the Stream to a file on the external storage name it e.g. newes_version.xml
catch all exception, and just use the file newest_version.xml file.
But it can't work for Layout files
Why? Every time you load your app on an device, everything (except /raw) in the /res folder is being compiled. Therefore your 'stylsheets' doesn't exist anymore the way they did in your IDE. It's not as simple as in html/css where you can just exchange on file. (Well, you can, put not an runtime)
Solution
If you really want to change your layout on Run time, you would have to load the xml (see above), parse it, and apply the information given by file using the R.id reference of the View elements.

Related

Where can I find WORKING samples of Amazon's S3 Image Upload Code for Android

I have spent the past 2 days struggling with Amazon's S3 SDK for Android. I was able to get the Java one (in Eclipse) working without any problems whatsoever; I could upload pictures, download them, and it would be no problem. Changing gears to Android, however, and I have had no luck. Currently, with this selected code:
AmazonS3Client s3 = new AmazonS3Client( new BasicAWSCredentials(
Constants.AWS_ACCESS_KEY, Constants.AWS_SECRET_ACCESS_KEY ) );
//These are correct, I have already confirmed.
ObjectMetadata metaData = new ObjectMetadata();
metaData.setContentType("jpeg"); //binary data
PutObjectRequest putObjectRequest = new PutObjectRequest(
Constants.BUCKETNAME, Constants.KEY3, new File(selectedImageUri.getPath())
);
//selectedImageUri is correct as well,
//(file:///storage/emulated/0/MyDir/image_1437585138776.jpg)
putObjectRequest.setMetadata(metaData);
s3.putObject(putObjectRequest); //Errors out here
I am getting multiple errors, the most common of which is this:
AmazonHttpClient﹕ Unable to execute HTTP request: Write error: ssl=0xb8cefc10: I/O error during system call, Connection reset by peer
javax.net.ssl.SSLException: Write error: ssl=0xb8cefc10: I/O error during system call, Connection reset by peer
at com.android.org.conscrypt.NativeCrypto.SSL_write(Native Method)
I have done a ton of research and had no luck finding WORKING code. I used this link from Amazon: https://aws.amazon.com/articles/SDKs/Android/3002109349624271 Without it working for me at all. They say up top it is deprecated, but I cannot find any links to working code. If you follow the SDK links to 'android sample code' files, their github repo (here: https://github.com/awslabs/aws-sdk-android-samples) contains zero code on the topic of uploading files (namely pictures).
Does anyone have ANY idea where I can find some working code that shows how to just upload a stupid picture to my bucket?!??! (Wish I knew why this was so simple in Java/ Eclipse and not so in Android / Studio).
PS: I have my api_key in the correct assets folder, my credentials are correct for login, the image is under 5mb, and this is being run on a background (async) thread so as to not be on the main thread.
-Pat
Have you tested to see that file:///storage/emulated/0/MyDir/image_1437585138776.jpg is a useable file? A content URI is Android does not typically map to a file and is typically used with content resolvers. I would double check that is actually a file path and not a content resolver uri path, which is what it looks like.
If that pans out, then double check the internet connection of the device. Can you call any AWS api? Are you behind some kind of firewall or protected wifi?
Finally it is not secure to use embedded credentials in an Android app (understandable if you are just testing locally, but never ship an app with embedded credentials, instead use Amazon Cognito to authenticate). For other example with S3 you can see the Getting Started Guide http://docs-aws.amazon.com/mobile/sdkforandroid/developerguide/getting-started-android.html which has a bunch of S3 example. And the S3 sample on GitHub (which was updated on July 22nd) has an S3 uploader sample using the Transfer Utility, and a step-by-step tutorial along with it.
Hope that helps!
Figured out the answer, though the why still eludes me. Turns out, the issue was with the Buckets on the back-end. One of my buckets was renamed slightly (whitespace) which prevented any uploads to it. I deleted and recreated by bucket and then re-posted it, and it seemed to work just fine.
WestonE, you bring up some excellent tips in your post, making sure this does not go to production with local credentials is a very good call. And to answer your Q, yes, surprisingly, file:///storage/emulated/0/MyDir/image_1437585138776.jpg is indeed a valid file. It may be as simple as the fact that HTC phones have really weird storage location systems.

What is the best way to store text data in a file/files so it can be used in various platforms?

I'm developing a knowledge base java application, where I can store and retrieve annotations with its title, date when the note was created (SQL datetime), content, tags about the annotation, etc.
It can be done easily with a database (I'm using SQL Server 2014), but the main problem is that the server is running on my own PC and it has to be always on and running the SQL Server. Also, I would like to extend the application by storing and retrieving this kind of data on mobile apps for Android and iOS.
Is there any other way to store that type of data in some files so it can be uploaded to some cloud storage like Dropbox ? After storing it on Dropbox, all I would have to do is sync the app with dropbox, get the files and read/write stuff.
UPDATE: Thanks for all the answers they helped me a lot. The best solution for me is to replace SQL Server with SQlite, as Gabe Sechan commented. Now I can make changes on the database without the need of a server running 24/7 and I can use the same database on Android and iOS apps.
You can use just a basic ajax call to pull content from a Dropbox "public" URL.
function(contenturl,intoselector,callback){
if (contentwindow.currenttopic!==contentID){
jQuery.ajax({
type:'GET',
url:'//www.corsproxy.com/'+contenturl,
dataType:'text',
async:true,
success:function(data){
intoselector.html(data);
if (jQuery.type(callback)==="function")
callback();
}
});
}
Notice that this example pulls through corsproxy so that you don't receive any XSS errors, so the url you pass needs to not contain a protocol itself.
If you want to pull a JSON or XML string that is stored in the file, then you might need to play around with the dataType and contenttype options in the ajax call.
This can also be done using Google spreadsheets:
Reading:
Create a spreadsheet and publish it on the web
Use one of the many available Javascript libraries for pulling data from Google spreadsheets:
http://jlord.us/sheetsee.js/ (which uses Tabletop.js)
http://chriszarate.github.io/sheetrock/
Writing:
You can use a Google app script for writing to the spreadsheet (reference) OR
You can create a Google form linked to the spreadsheet and simply fill the form from your mobile app whenever you want to add some data to the sheet (reference)
Of all the cloud services, when it comes to Android, Dropbox's Sync API is one of the easiest to implement. Do you need specific code examples on how to sync with Dropbox?

Overriding Tigase OfflineMessages class

I'm developing a chat application using Tigase server in the background for providing the XMPP communication. Besides that I'm using smack/asmack as a library in my application.
With every message send between the users, for the sake of the functionalities I want to implement I'm including package extensions via smack's Presence.setProperty() method,which are correctly send if both of the users are online. When one of the users is offline the Tigase server saves that message and then delivers it when the user becomes online again. The thing is that Tigase don't save the message including the packet extensions, so when the user is online again it receives only "parts" of the message send.
I found the Tigase source and found the place in the OfflineMessages class part of the xmpp.impl package, where I need to insert a line of code in order for msgoffline plugin to save the offline messages using extensions.
What is the easiest way to override the OfflineMessages class in Tigase jar file, without building the whole source code of the project?
I'm using Tigase-5.1.5-b3164 version on ubuntu server and Eclipse as IDE.
I do not know Smack library, hence I do not know what Presence.setProperty() method does. But if it does something to a presence packet (adding an extra payload to the presence status) then this will not be saved to offline message storage because.... presences are not stored in offline message storage.
If you want to modify OfflineMessages to make it store more different data than it stores now (like a specific presence packets) then there is a method: savePacketForOffLineUser with a big IF at the beginning. This is where the decision is made on what to store in an offline storage.
But, please be careful not to store ALL presences, it will kill your service and your database.
The best way to add your own plugins is to make a copy of the existing Tigase plugin (OfflineMessgae), modify it to your preferences, change a name of the plugin and ID of the plugin. Then you just put a jar with your code into Tigase jars directory and modify configuration to load your plying instead of the default one. This way you do not have to recompile Tigase sources and will be also easier for you to update to a new version.

scalable file upload/download permissions

What would be a scalable file upload/download system/database?
I'm building a website where users can login, upload images that are private, but truly private. I can't upload them to a map on the harddisk of a server, since that would not scale (what happend if we add more servers?) and it wouldn't be private since everyone could go:
http://127.372.171.33/images/private_picture.png
and download the file.
I am building the project in Play Framework (scala/java)
How do websites like flickr handle these kind of things? Do they put them in a database? And what kind of database would be suitable for this situation?
Thanks for help
I can't tell you how those big sites handle it but putting those images into a database might be one way.
Another way would be to put the files into a virtual filesystem that spans a cluster of servers or distribute them onto different servers and just don't make the directories that contain the images visible to the webserver. Thus nobody should be able to open the image just using the server and the path on that server.
To actually deliver the images you could them implement some streaming service that sends a bytestream to the browser for display (like the webservers would do as well). This service could first check the download permissions for the requested image.

GWT - Client-Side File Uploads

I have been messing around with GWT uploads lately. I want to have the ability to upload an XML file from the client, and get the contents of that file (display the contents in a TextArea).
From what I have found on the net, it seems I would have to upload the file to the server, and then get the contents of the file. I do not particularly like the idea of allowing file uploads to the server (even if they are only XML). Is there anyway to pull the contents of a file that the client specifies without sending it to the server?
Thanks
Recent (decent?) browsers implement the "HTML5" File API that's quite easy to use in GWT using JSNI.
See also: https://developer.mozilla.org/en/Using_files_from_web_applications
Because of security restrictions you cannot access the file on the client side alone. It has to be sent to the server for processing.

Categories