How to insert data on to the server in Android Application - php

I am developing an android application where the user data should be pushed onto the server .How can i do that ? I was able to get data from the server but unable to insert it. Code snippet would be good.I have tried this but it doesn't work.
Java Code :
String data="xyz.org/json.php"+"?"+"&"+URLEncoder.encode("data", "UTF-8") + "="+"order";
URL url;
url = new URL(data);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
In json.php :
$data = urldecode($_GET['data']);
if($data=="order")
{
$query="insert into xyz values('$x','$y','$z')";
$query_run=mysql_query($query);
}

you need a httppost or httpget dependance on the your url method for that you can use Httppost and get follow this url you get some idea and also check this url for asynctask beacuse you have to put all the network related you have to put in the asynctask.

Related

My PHP file doesn't receive data from my android app using POST method

I have a problem with php file when it should receive some data form my android app. I'm using POST method and I can't figure it out why it doesn't work. I've tried searching for answers but everything I tried, it doesn' work. First, I used URLConnection class for connection with server, and after that, I tried with HttpURLConnection, but still doesn't work. I want to transfer data to server exclusively with the POST method, not GET. Could anyone explane me what I'm doing wrong, or did I forget something to add or configure?
Did you try something like this :
URL url = new URL("your_url_here");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setConnectTimeout(10000);
connection.setReadTimeout(10000);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.connect();
byte[] output = "your data to send here".getBytes("UTF-8");
OutputStream os = connection.getOutputStream();
os.write(output);
os.flush();
os.close();

Talking to PHP get commands from Android app

I have an android app and am trying to send data to the PHP on the server. The server gets the php data with
$this->get('uname');
$this->get('pass');
We do use codeigniter if that matters
The Java code, inside of an Async method, I currently have is
InputStream response = null;
URLConnection connection = new URL(urls[0]).openConnection();
connection.setRequestProperty("uname" , Username);
connection.setRequestProperty("pass", Password);
response = connection.getInputStream();
When I run this, the code always returns null but it is supposed to return a JSON array either way. I have checked the URL and it is correct. What am I doing wrong? Thanks!
You code should be like this
For your android side ,as mentioned by #Ichigo Kurosaki at Sending POST data in Android
For Codeigniter side , cosider you function name is user_login
function user_login(){
$uname = $this->input->get_post('uname'); //get_post will work for both type of GET/POST request
$pass = $this->input->get_post('pass');
$result=$this->authenticate->actLogin($uname,$pass ); //considering your authenticating user and returning 1,0 array as success/failure
$this->output
->set_content_type('application/json')
->set_output(json_encode($result));
exit;
}

Arabic from Android through PHP into mysql

I have an android app that calls a restful API in PHP that saves information in mysql. The URL contains Arabic characters in one of its parameters.
When I send this URL from the browser itself, I eventually see the Arabic characters in the DB (and when echoing) but when I call this URL from the app, it echos back (I added echo command on the php side) and store in the DB "?????".
When calling the API, I do the following:
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded; charset=utf-8");
urlConnection.setRequestProperty("accept-charset", "UTF-8");
Am I missing something?

Get PHP Echo in Android

I have some problems with Receiving PHP Echo message from an website.
I have this code:
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost=new HttpPost("http://www.itbstudios.tk/test.php");
HttpResponse response = httpclient.execute(httppost);
String str = EntityUtils.toString(response.getEntity());
I found this code and a lot more here on stackoverflow and is is not working. The app is crahsing on httpclient.execute();
Internet permission is set in android manifest.
Php code is just an <?php echo "test"; ?>
I'm building for API 15 and testing on HTC Device.
Can you guys help me?
I even created an other project and paste the code in on create and i have the same problem.
Thanks!
HttpClient got some bugs in API higher than 10. Use HttpUrlConnection instead.
GET method example:
URL url = new URL("http://www.itbstudios.tk/test.php");
HttpUrlConnection mUrlConnection = (HttpURLConnection) url.openConnection();
mUrlConnection.setDoInput(true);
InputStream is = new BufferedInputStream(mUrlConnection.getInputStream());
String s = readStream(is);

Use database for Android App by downloading with php-script

I'm going nuts! Maybe someone can help me?!
I have an sqlite-database on a running server, which I receive due to an php-script. (To make it clear: I'm calling an php-script which gives me the the database as a response). With the response I'm now trying to "parse" it to an regular *.db file which I later on use for my app.
the app works fine, while placing the *.db into the assets folder. But I need to get the updated database everytime when calling the app. Therefore I need to receive it somehow from the server.
Just for notice: I don't know why they use a php-script for that, but it works perfectly with the iOS-Version of the app. So I am 100% sure that the script does work.
Got any hints or a solution to that?
Thanks!
EDIT: here is what I'm trying to do.
private void copyDatabase() {
InputStream myInputDB = null;
OutputStream myOutputDB = null;
HttpResponse response = null;
// Path to the just created empty db
String dbFilePath = DB_PATH + KeyConstants.DB_NAME;
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost(
"http://USERNAME:PASSWORD#ADRESS/u/db.php");
try {
response = httpClient.execute(httpPost);
//Open your local db as the input stream
myInputDB = response.getEntity().getContent();
//Open the empty db as the output stream
myOutputDB = new FileOutputStream(dbFilePath);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInputDB.read(buffer)) > 0){
myOutputDB.write(buffer, 0, length);
}
//Close the streams
myOutputDB.flush();
myOutputDB.close();
myInputDB.close();
} catch (IOException ioEXC) {
throw new Error("Problem copying database from resource file.");
}
What do You have the problem with? With the Android app or with the PHP script?
I don't understand why there is a whole DB file request-response and not just the data (and better in some lazy reading), but that's not matter of Your case.
If You need to "download" all the DB at every run You should call and implement some method to do so - in the very first Activity call this method within onCreate() method. You can create a simple method within this Activity or in better approach create a simple class for this that will be instanciated and its method called within the first activity's onCreate().
But maybe I just don't understand You question...
EDIT: try reading through this problem: Can I download an SQLite db on /sdcard and access it from my Android app?

Categories