Android: Uploading text file data to MySQL database - java

My android app was able to generate text files with key value pairs. Now, I want to upload those data to an online MySQL Database.
My Idea is, my App will communicate with a PHP file and so this PHP file will parse the text file and then insert it to MySQL database.
My question is, how will android upload and communicate with the PHP file? Any codes and other ideas out there?

You use these classes.
For examples on how to use them, SO is full of related question. Just search for "android http request".
Two things, first don't use a text file, just send the key value pairs in a HTTP post request, second remember to do all http requests asynchronously, you can use for example AsyncTask to do this.

Related

Android studio jDBC not connecting to the mysql server with jdbc [duplicate]

This question already has answers here:
Can an Android App connect directly to an online mysql database
(8 answers)
Closed 6 years ago.
I have a website already setup which uses mysql database. I want to know how can i connect my app to that database.
What i wanna achieve is that my app should make a request to find a table of a defined "ID" or name. The table contains links to images or image names. I want the app to retrieve those images and display them on the android app.
What are the ways this can be done?
Can I use PHP to develop an android app?
Android does not support MySQL out of the box. The "normal" way to access your database would be to put a Restful server in front of it and use the HTTPS protocol to connect to the Restful front end.
Have a look at ContentProvider. It is normally used to access a local database (SQLite) but it can be used to get data from any data store.
I do recommend that you look at having a local copy of all/some of your websites data locally, that way your app will still work when the Android device hasn't got a connection. If you go down this route then a service can be used to keep the two databases in sync.
The one way is by using webservice, simply write a webservice method in PHP or any other language . And From your android app by using http client request and response , you can hit the web service method which will return whatever you want.
For PHP You can create a webservice like this. Assuming below we have a php file in the server. And the route of the file is yourdomain.com/api.php
if(isset($_GET['api_call'])){
switch($_GET['api_call']){
case 'userlogin':
//perform your userlogin task here
break;
}
}
Now you can use Volley or Retrofit to send a network request to the above PHP Script and then, actually the php script will handle the database operation.
In this case the PHP script is called a RESTful API.
You can learn all the operation at MySQL from this tutorial. Android MySQL Tutorial to Perform CRUD.
Yes you can connect your android app to your PHP to grab results from your database. Use a webservice to connect to your backend script via ASYNC task and http post requests. Check this link for more information Connecting to MySQL
Use android vollley, it is very fast and you can betterm manipulate requests.
Send post request using Volley and receive in PHP
Basically, you will create a map with key-value params for the php request(POST/GET), the php will do the desired processing and you will return the data as JSON(json_encode()). Then you can either parse the JSON as needed or use GSON from Google to let it do the parsing.

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?

How to read or write data from a MySQL cPannel Database in Java for an Android App

Any tutorials out there on how to send and received data to and from a MySQL database hosted on a server using cPannel? I'm trying to make an android app that connects to the internet, and it needs to read and write data to and from the hosted database. The database is a MySQL one and is hosted on godaddy. I've been on the internet all day today looking, so any help would be greatly appreciated.
Well you need to write a webservice which will get the data from your mysql database and send it to your android app.
First of all you need a server which you have already. Create space to write php scripts there to interact withe the database that is hosted there.
Second Write a php script on your server which can retrieve the required data from the MySQL database. Encode the retrieved data inside a JSON object and send it back to your app. From your app you have to call the URL where the webservice is hosted, and the php script will do the interaction with the database, retrieve the results and send it back to your app in JSON.
Third - Follow the same method to send and retrieve data to server. Create multiple webservice as per your requirements.
This tutorial excellently illustrates what you are looking for. This example uses a local server. You can skip some of the steps there. Hope it helps. URL: http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/
If you still got any questions, comment below. I will be happy to help.
Thanks!

How can i Get back source data of data contained in a script PHP with Java

I explain, I tried to open my database .sqlite contained in my server in Java with jdbc:sqlite:mydb, but it doesn't work because my database ".sqlite" is distant so i creat a script php based on my server which gets back me all of my databases.
So my question How can i Get back source data of data contained in a script PHP with Java?
Thanks a lot!
PS : Sorry for my english (I'm french)
One way would be to create a service in PHP which offers the data, so that your Java application can consume it.
E.g. PHP can expose the data in an XML or JSON format, for instance over HTTP. Your Java application would then call the HTTP URL and receive the data in JSON/XML/whatever, and then parse it to extract the data from that format.
There are other ways as well, e.g. a service bus could broker the data between the different systems, but that requires much more setup (though in the long run, it does have value, if you keep adding data that you want to expose to more and more systems).
You can create PHP WebService server and write some functions to operate that databases data for fixing your problems. Then just use Java to connect with soap to your created WebService functions.
How to start webservice, try to read this one:
http://greatgandhi.wordpress.com/2009/11/25/create-a-php-webservice-in-5min-using-php-soap-and-wsdl-technology-nusoap/
Its the best way i know, if you need to send some data back to your remote db. Its a little more complicated, but much more correct way to operate remote data.

Java: how to upload files to server and handle authentication?

I have a small linux vps. I have written a Java client application, which needs to connect and submit large string data and images. The string data will be stored as regular text files on the server, and will be parsed by another Java application that will run on the server and use this uploaded files and images.
The next part of the problem is, because this Java client will be run by several users, I need some way to uniquely identify each uploaded file to the currently logged in user session on the website (the user needs to login on the website, to be able to run the tasks). Any suggestions or more efficient patterns ?
Don't write the stuff to files. Punch the uploaded data into 'raw' database tables by user ID. The batch job job can pull the data out, parse/format/fold/spindle/mutilate, and stuff the results into the real tables, then delete the raw data.

Categories