I want to get JSON data from an server and store it to database on my server.
I bought API that returns json, which means that server is not mine but I want to take that data and store it to my server.
How to call API, receive data and store it to my server?
P.S. That API only allows access from 5 IP addresses and I want to use it for android app. Instead of getting data directly from that server to android phone, I have to store that data to my server and then call GET method on my server to receive results on the android phone. This way I could even use GCM.
It doesn't have to be php script. If you have better idea or solution please let me know.
You could use
$json = json_decode(file_get_contents("http//example.com/path?query"));
Related
I'm making an android app that will have a backend database (MySQL).
From my research, it seems like one of the ways to connect to a MySQL database is to use PHP to make an HTTP request to the database, which will then return data in the form of a JSON object which is decoded.
PHP is used to make a GET/POST to the database (often using a service like Retrofit)
The database fetches or posts the appropriate data and must alert the client/return data
The database sends back information that is JSON form
The client decodes the returned JSON
Is this the correct logic?
Why is PHP needed to make the request? Is this an acceptable way of interacting with a MySQL database from an android app? Do you have any advice to offer for going about this?
If your are using the LAN MYSQL server in your own device, you can use MYSQL command to connect the server
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://"+serverip+":"+serverport+"/"+dbName;
Connection conn = DriverManager.getConnection(url,username,password);
String queryString = "SELECT * FROM materialdb";
PreparedStatement statement = conn.prepareStatement(queryString);
ResultSet rss = statement.executeQuery(queryString);
results = new ArrayList<FindDetails>();
rss.beforeFirst();
while(rss.next())
{
FindDetails item_details = new FindDetails();
item_details.setMaterial_Code_No(rss.getString("Material_Code_No"));
results.add(item_details);
}
rss.close();
statement.close();
}
catch(Exception e)
{
Toast.makeText(this, "Fail to get main list", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
If the server is on web, the better way of connect to server is PHP
This approach looks like it is for a web server, not a client (much less an Android client). Usually, servers have databases and clients are "consumers" of that data. However, clients also usually have databases to store that data.
PHP is a server-side technology whereby you can use a URL and HTTP to send and retrieve data. PHP can then be used to connect to a database to provide rich content to the client.
In Android, your device (likely) will not have a URL or website. It can have a database and you can use HTTP libraries (like Retrofit - http://square.github.io/retrofit/) to get data from websites (that might use HTTP). But this is passive - your client will have to request data, the server cannot "push" data to it.
For "push" data, you can use Firebase (Google Cloud Messaging) which uses a Google framework and API to have a (reasonably) constant connection to the device to "push" notifications to it. If you follow their rules, you will send a small data packet that tells your client to wake and start making specific HTTP requests to whatever URL you like.
I am working on an Android app where the user fills a form and submits it. I want to know which technique to use to store the data. I want to later process this stored data to form reports. Please suggest me the best way to store data and later to produce report.
The form initially will contain one dropbox, date, radio button and edit text.
I found few options below but dont knw how to proceed:
Using JSON
Using Google cloud
I tried code at below link:
http://androidexample.com/How_To_Make_HTTP_POST_Request_To_Server_-_Android_Example/index.php?view=article_discription&aid=64&aaid=89
But dont know how to run the program as it needs serverside php script. I have XAMPP installed on my machine with Apache server running. I can run normal php code but dont know how to make that php code work with my android app.
I am new to server side data handling
Thank you very much in advance. Please help
since you're working with xampp.you can access your localhost from your android emulator using the ip 10.0.2.2/
you will need to use the AsyncTask class and a JSON parser to send your data to your php script.The JSONparser class will remain constant for most of your whole app. You will only have to change the code in your AsyncTask class depending on what data you want to send and the location(php file ie 10.0.2.2/your_file.php or yourdomainname.com/your_file.php) to process the data.
If you don't want to work on server scripting and requires a simple data storage to the cloud then you can try using Parse
Basically Parse provides you with a library that your app can use to pass values to the server Parse is managing.
I am pretty new with this. I am trying to develop some android application in Qt/Qml to query and update data to a database.
The problem I'm trying to solve:
There is a server with a MySQL database. The android application request some data. I use a very very basic php file that query in the database, format an XML and show that file starting with a header('Content-type: text/xml'); sentence, then some echo "<...>" sentences. This php file receives parameters via $_GET array and performs the query.
This is a little archaic but works fine. My client application use Qt/QML for android. It uses XmlListModel to obtain the data from the server and show that data in a ListView.
So far so good, but here is my real problem:
The user of the android application can change some data, and the modified data must be sent back to the server, who performs an update in the database.
My first attempt was to create another php file and send the data in the URI, get it via $_GET and perform the update to the database. This works but there could be many variables and the URI will become very large. I think might be better to send an XML (or JSON) from the client to the server, and put in that file the data for the update.
So, this is the question: How can I send an XML file from Qt/QML to the server? How does the php file obtain that data? Or maybe there is another, better way for doing this.
Every example I find just shows the server-to-client part, or is written in Java, and I don't know java enough.
Additional notes:
After I can solve this I will fight with security: SSL, avoiding SQL injection or anything, but that will be in another question if necessary.
I didn't use SOAP or similar because the first ideas was simple. Also, I have never used SOAP before. If this is the right way, I would be very grateful if you show me how to use it from Qt/QML in android.
Of course, I can use C++ for the client application since it is using Qt.
I use Qt since I already know it and the app might be useful in desktop as well.
Sorry if I made any english mistake.
Thank you in advance.
I have an idea for a social web app and part of the idea is to pull data from localstorage onto the server database. The reason for this is say you have an idea and you want to put it on your next status for this social app, but you have no signal and wireless anywhere. You write out the status on the app and it saves it to the localstorage for use when they get back to somewhere where they can connect their phone online.
They then get to a place where they can go online, the app then detects that there is a change in the localstorage and then pulls it from there and saves to the server database.
Is this at all possible, if so how would you go about implementing it?
If it's not at all possible, do you think this could be sometime in the future?
I look forward to hearing from your comments and answers on this one as I think it could be quite a big discussion.
Yes it's possible, why wouldn't it be? When you pull data from the local storage, the data works just like any other Javascript variable, there are no restrictions that would stop you sending it to a server other than the lack of Internet connection.
how would you go about implementing it?
First you'd need to detect the connection status, see this question. So when a user tries to update their status, it checks if the connection is online and if it isn't then save it to local storage.
Use the methods in the linked question to detect when the connection comes back up, and at that point pull the data from local storage and send it to the server via ajax, then clear the local storage to prevent duplicate data being sent.
You can periodically check navigator.onLine in your Javascript to get the online status of a device. Note that while
navigator.onLine == false
safely tells you that a device of offline,
navigator.onLine == true
doesn't necessarily mean it has access to the internet and can perform the request, just that it is connected to some sort of network.
Once you assume that the device is not offline you'd send an ajax request (I recommend using jQuery's $.ajax function) in which you POST the data from your localStorage
(localStorage.getItem('dataToPull'))
to a PHP script, which then inserts it into your MySQL database.
If that ajax request fails, and you're sure it's not getting a PHP/MySQL error, it's safe to assume that although the device is connected to a network and navigator.onLine is true, there's no internet connectivity. You can then do some error handling, e.g. poll the device again in 5 minutes.
I need to send all my contacts to server and return back only the contacts that are using my app.I am using HttpPost for sending data to the server. I am thinking of sending it as an string separated by delimiters or as NameValuePair. Is there any better way to do contact sync?
Are you using a database? If so create a php webservice that connect to your database, make a query and return results according the criteria you need. then you need to encode the result in a json array and use json to connect your android app and get the results, here is an example how to use json with android:
http://www.vogella.com/articles/AndroidJSON/article.html
There are many more libraries you can use: gson/json/jackson... all similar, you can make a search on google and chose the one you like better
Instead of json you can use SOAP, I have never used it but you can try : http://code.google.com/p/ksoap2-android/
Direct database connection is not recommended on android