I have a json file on server:
{"images":[
{"url":"...", "likes":"123"},
{"url":"...", "likes":"234"},
{"url":"...", "likes":"345"}
]}
I get the json file on android read it, but if someone likes a picture i want to change the value of the first picture from 123 to 124, is this possible and how can i do this?
The whole point is to change a json value on server,from client side.
Also if this isn't possible how can i make this happen?
Also if i want to get the Top50 rated pictures,how can i sort them and get only the 50 picture,without getting all pictures and sorting them on android ?
Which one is better,initializing the Top50 images,when the user starts the app,or when the user click on the button Top50.I assume that if its when he click the button,there might be some performance issues server side?
My other idea is to have a function server side,which every 10 min,executes automatically and gets the Top50 rated and makes something like json file.So it all happens server side automatically.
To make this happen, client should expose some interface, i.e. function that will allow to modify file on server side. The interface and implementation of this function greatly depends on server itself, i.e. which protocols it handles, what built-in or external modules it supports, which languages are supported, etc... For example, the classic scenario is using apache as HTTP server, CGI enabled, and write CGI function in perl. So, in this case interface would look like http://server.name/like.cgi?image=image123.
How to modify the values on the server ?
For this every like of a photo should be a post request of this sort.
{
"data": [
{
"image_id": 3133456,
"likes": 343
},
{
"image_id": 3133456,
"likes": 343
}
]
}
On parsing this request server updates the corresponding image's like on the server.
How to get the top 50 rated/liked images from the server ?
Again you send a get request to such a url
http://server.getsomething.com/getTop50Images
On server side
On receiving such a request you make a query on the table in your database something like this
select image_id , image_url, likes from image_table limit 50 ORDER BY likes ASC
Now getting those query results and returning them as a json would not be a performance hit until you have huge bulk of data. like some million rows may be in your database.
Response can be something like this
{
"result": [
{
"image_id": 3133456,
"likes": 34400,
"url": "http://flickr.com/someimage"
},
{
"image_id": 3133456,
"likes": 34380,
"url": "http://flickr.com/someimage"
}
]
}
You still avoid using a database yourself but can lease it from clouds services like parse.
However if you won't be using those services then you can take a look at ftp packages for js. Like the neo JavaScript library by apache.
But still a good choice will be to go with the database approach (is quiet simpler).
Related
I'm no back-end developer. So perspective is always appreciated.
I have written a script which requests from an API and creates this huge JSON file I want to save in firebase, how can I accomplish this? And would it be possible to filter this json with python for example; when I add region=eu in the url this returns the objects which have Europe as region or do I absolutely need to request the entire json file and parse in my code (java android) ?
Since there are a few parts to your question:
You can save JSON to Firebase and the data will be mapped to child locations:
Using PUT, we can write a string, number, boolean, array or any JSON object to our Firebase database...When a JSON object is saved to the database, the object properties are automatically mapped to child locations in a nested fashion.
https://firebase.google.com/docs/database/rest/save-data
And for your next question:
And would it be possible to filter this json with python for example; when I add region=eu in the url this returns the objects which have Europe as region
Looks like you should be able to jimmy something together with Firebase's filters, startAt and endAt:
We can combine startAt and endAt to limit both ends of our query.
https://firebase.google.com/docs/database/rest/retrieve-data#section-rest-filtering
For your example you might do something like this:
curl 'https://yourfirebase.firebaseio.com/yourendpoint.json?orderBy="$REGION_NAME"&startAt="EU"&endAt="EU"&print=pretty'
...or do I absolutely need to request the entire json file and parse in my code (java android) ?
The facts that JSON objects are stored hierarchically in Firebase and that you can filter based on those object values makes me think you do not, in fact, have to request the entire JSON file. However, I don't have personal experience with this particular aspect of Firebase, so give it a shot!
As #ackushiw mentions in the comments, you can also use the equalTo query (https://firebase.google.com/docs/reference/js/firebase.database.Query#equalTo):
curl 'https://yourfirebase.firebaseio.com/yourendpoint.json?orderBy="$REGION_NAME"&equalTo="EU"&print=pretty'
It really depends on how you are structuring your JSON. It's generally recommended to make your JSON tree as shallow as possible since all children are loaded when you have a matching query.
FIREBASE DATA:
{
"-id1": {
"region": "eu" // bear in mind queries are case sensitive
"title": "Foo"
"nested": {
"city": "berlin"
}
},
"-id2": {
"region": "other"
"title": "Bar"
"nested": {
"city": "berlin"
}
},
"-id3": {
"region": "eu"
"title": "Baz"
"nested": {
"city": "paris"
}
}
}
Querying with (using the Android API)
.orderByChild("region").equalTo("eu")
would return "-id1" and "-id3"
with
.orderByChild("nested/city").equalTo("berlin")
would return "-id1" and "-id2"
The REST API Returns Unsorted Results: JSON interpreters do not enforce any ordering on the result set. While orderBy can be used in combination with startAt, endAt, limitToFirst, or limitToLast to return a subset of the data, the returned results will not be sorted. Therefore, it may be necessary to manually sort the results if ordering is important.
If you're using a more complex structure I recommend watching this video
https://www.youtube.com/watch?v=vKqXSZLLnHA
I'd also recommend using the firebase library for Android
https://firebase.google.com/docs/android/setup
And Firebase-UI, It does a lot for you.
https://firebaseopensource.com/projects/firebase/firebaseui-android/
I am using Retrofit2 in my application. My problem is that I have a group of persons in my list and all these persons are to be continuously monitored.
Monitoring in the sense that I need to make a retrofit call for each person ,to obtain the real time data of that person.
For example,
Consider person 1 :
My initial request call will be with the following data.
{
"patientId" : "qwerty1",
"time": ""
}
Response is:
{
"data" : [],
"updated_time" : "16519644"
}
Next request for person 1 :
{
"patientId" : "qwerty1",
"time": "16519644"
}
and this repeats. Server side coding is in such a way that only a set of 20 data are provided as response for our request. So to obtain continuous data set we need to make repeated call. I need to do this process for "n" number of persons in real time. Also I need to update UI when there is a response. I am really confused with the concept of sending retrofit request in a loop. Please suggest a way.
I have a quick question about the best practices for data structure in a firebase database.
I want users of my app to be able to maintain a friends list. The firebase documentation recommends creating a schema (not sure if thats the proper word in this context) that is as flat as possible. Because of this I thought it would be a good idea to separate the friends section from the player section in the database like so:
{
"players":{
"player1id":{
"username":"john",...
},
"player2id": ...,
"player3id": ...
}
"friends": {
"player1id"{
"friends":{
"friend1Id":true,
"friend2Id":true
}
},
}
"player2id"{
"friends":{
"friend1Id":true,
"friend2Id":true
}
},
}
}
So my questions are as follows:
Is this a good design for my schema?
When pulling a friends list for one player, will the friends lists of EVERY player be pulled? and if so, can this be avoided?
Also, what would be the best way to then pull in additional information about the friends once the app has all of their IDs. e.g. getting all of their user names which will be stored as a string in their player profile.
Is this a good design for my schema?
You're already thinking in the right direction. However the "friends" node can be simplified to:
"friends": {
"player1id": {
"friend1Id":true,
"friend2Id":true
}
}
Remember that Firebase node names cannot use the character dot (.). So if your IDs are integer such as 1, 2, and 3 everything is OK, but if the IDs are username be careful (for example "super123" is OK but "super.duper" is not)
When pulling a friends list for one player, will the friends lists of EVERY player be pulled? and if so, can this be avoided?
No. If you pull /friends/1 it obviously won't pull /friends/2 etc.
Also, what would be the best way to then pull in additional information about the friends once the app has all of their IDs. e.g. getting all of their user names which will be stored as a string in their player profile.
Loop through the IDs and fetch the respective nodes from Firebase again. For example if user 1 has friends 2, 3, and 4, then using a for loop fetch /players/2, /players/3, and /players/4
Since firebase pull works asynchronously, you might need to use a counter or some other mechanism so that when the last data is pulled you can continue running the completion code.
My Android app is making two GET calls to my Server API. In the first one, is this, where parameter code is a 256 char String.
$.getJSON( myServerEndpoint, {
action: "doStuff1",
username: $("#username").val(),
code: my256charString,
format: "json"
})
.done(function( data ) {
doStuff2Response(data);
});
The second one is this, where parameter code is a 5120 char String. Both reach the same server endpoint.
$.getJSON( myServerEndpoint, {
action: "doStuff2",
username: $("#username").val(),
code: my5120CharString,
format: "json"
})
.done(function( data ) {
doStuff2Response(data);
});
When I call both of them from the same device and same user connected to WiFi or most mobile data providers, it works perfectly.
However, when I connect from a Vodafone data connection, the second request never reaches the server. I cannot find any other explanation than that there is a limit on the length of the parameters with Vodafone.
Any ideas or solutions?
OK, so here it goes. First, read this: What is the maximum length of a URL in different browsers?
Yes, there's a limit in the length of the "URL", but someway I don't know how to explain why it is happening only for vodafone. Plus, I don't even know how the request pass through their servers anyways.
As for the solution, you should consider changing from GET request to POST request when the payload is too big.
Quick solution: Base64-encode the code part of the message. Downside: you must decode on the server. This is a standard function in most languages though.
If you're already using Base64 or somesuch cypher, what about Blobs? https://developer.mozilla.org/en-US/docs/Web/API/Blob
chromano's suggestion is spot-on too, just switch to POST and you will definitely get an unlimited Post Body. Downside: Have to JSON.stringify and JSON.parse for yourself, and if you want to expose this URL to a user (say as a link to share) it now can't carry the same information (URL's are GET requests).
I want every time the user presses on search button, the result has to be saved, because i'm adding 10 latest search history to my app.
In order to make such a feature work, data retrieved should be saved.
I was searching in the documentation and found this :http://developer.android.com/guide/topics/data/data-storage.html
Which class should be used in order to accomplish my work?
To store such kind of data (max 10 items) i would simply store those as JSON in your SharedPreferences. See this chapter of the "Storage Options" page of the Android documentation: https://developer.android.com/guide/topics/data/data-storage.html#pref
use Shared Preferences http://developer.android.com/guide/topics/data/data-storage.html#pref it better way to compare other option .. store it to JSON String ..
JSON EXAMPLE
How many data you want to be stored in the history? and how long it'll be keeped?
if you only want a few history stored let's say about 5-10 you can use SharedPreferences and encode the data to JSON and if you want to get it you can parse the data.
for example the history will be like this
{
"history": [
{
"text": "aaa",
"date": "23-03-2013"
},
{
"text": "bbb",
"date": "22-03-2013"
},
{
"text": "bcccc",
"date": "22-03-2013"
}
]
}
the data will be keeped until your application uninstalled or if you want to clean it you can use SharedPreferences.Editor.clear()
But if you want to store many history data let's say more than 100 data you can used sqlite database then you can query the data everytime user enter the screen where the search function is located I guess it'll be fast enough because you only getting text not a Blob and if you want to clear it just delete it using query