Is there any method to get data from an Android app to a website?
If it is from website to website, with "file_get_contents" it is possible. But then any idea on getting data from Android apps?
Thanks in advance!
Using this simple php script you can get data from android:
<?php
$filename="datatest.html";
file_put_contents($filename,$_POST["fname"]."<br />",FILE_APPEND);
file_put_contents($filename,$_POST["fphone"]."<br />",FILE_APPEND);
file_put_contents($filename,$_POST["femail"]."<br />",FILE_APPEND);
file_put_contents($filename,$_POST["fcomment"]."<br />",FILE_APPEND);
$msg=file_get_contents($filename);
echo $msg; ?>
In android can use HttpPost
to accomplish this by something like this:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/mypage.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("fname", "jake"));
nameValuePairs.add(new BasicNameValuePair("fphone", "9999999"));
nameValuePairs.add(new BasicNameValuePair("femail", "xyz@live.com"));
nameValuePairs.add(new BasicNameValuePair("fcomment", "Help"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
Replace your data with the fields you want to set and send in the constructor of BasicNameValuePair
.
Here is the actual Source
You could execute post requests in android to your website. Although this has to executed from a different thread, since android doesn't allow network operations on the main thread.