I’m trying to use a web service to store/retrieve info from a remote SQL database. For some reason empty strings are being inserted into the DB rather than the strings I want stored.
What I need is to store the strings deviceID & dateStamp if no entry in DB exists with deviceID.
DB table name is trials, and the columns are: _id | device_id | install_date
Note: Connects and interacts with database, just stores “” and “” instead of the strings.
Can someone please tell me why this is? This is my first time I’ve tried this kind of thing so having a lot of trouble and trying to modify snippets I found to my needs.
Here is my java code:
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.widget.Toast;
public class LicenseCheck extends Activity
{
String deviceID="",dateStamp="";
byte[] data;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
InputStream inputStream;
List<NameValuePair> nameValuePairs;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final String android_id = Secure.getString(getApplicationContext().getContentResolver(),Secure.ANDROID_ID);
deviceID = android_id;
Date now = new Date();
dateStamp = String.valueOf(now);
try
{
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://99.57.234.12/Main.php");
// Add your data
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("device_id", deviceID));
nameValuePairs.add(new BasicNameValuePair("install_date", dateStamp));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
inputStream = response.getEntity().getContent();
data = new byte[256];
buffer = new StringBuffer();
int len = 0;
while (-1 != (len = inputStream.read(data)) )
{
buffer.append(new String(data, 0, len));
}
inputStream.close();
} catch (Exception e) {
Toast.makeText(LicenseCheck.this, "error"+e.toString(), Toast.LENGTH_LONG).show();
}
if(buffer.charAt(0)=='Y')
{
Toast.makeText(LicenseCheck.this, "working: " + buffer.toString() + ".", Toast.LENGTH_LONG).show();
Move_to_next();
LicenseCheck.this.finish();
} else {
Toast.makeText(LicenseCheck.this, "Trial Started", Toast.LENGTH_LONG).show();
Move_to_next();
LicenseCheck.this.finish();
}
}
public void Move_to_next()
{
startActivity(new Intent(this, MainMenu.class));
}
}
Here is the PHP:
<?php
require_once('Connections.php');
mysql_select_db($database_localhost,$localhost);
$device = $_POST['device_id'];
$date = $_POST['install_date'];
$query_search = "select * from trials where device_id = '".$device."'";
$query_exec = mysql_query($query_search) or die(mysql_error());
$rows = mysql_num_rows($query_exec);
if($rows --> 0)
{
echo "Y";
$result = mysql_query("SELECT device_id,install_date FROM trials WHERE device_id = '".$device."'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
echo $row[0]; // device_id
echo $row[1]; // install_date
} else {
echo "N";
mysql_query("INSERT INTO trials (device_id, install_date) VALUES ( '".$device."', '".$date."')") or die (mysql_error());
}
?>
You’re posting:
But you’re looking for:
Your PHP is looking for variables in $_POST that haven’t been set.