我正在尝试使用Web服务来存储/检索远程SQL数据库中的信息。由于某种原因,空字符串被插入到DB而不是我想要存储的字符串中。
我需要的是存储字符串deviceID&如果数据库中没有带有deviceID的条目,则为dateStamp。
数据库表名是试验,列是:_id | device_id | INSTALL_DATE
注意:连接并与数据库交互,只存储“”和“”而不是字符串。
有人可以告诉我为什么会这样吗?这是我第一次尝试这种事情,因此遇到了很多麻烦,并试图修改我发现我需要的片段。
这是我的java代码:
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));
}
}
这是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());
}
?>
答案 0 :(得分:2)
您正在发帖:
nameValuePairs.add(new BasicNameValuePair("DeviceID", deviceID));
nameValuePairs.add(new BasicNameValuePair("DateStamp", dateStamp));
但你正在寻找:
$device = $_POST['device_id'];
$date = $_POST['install_date'];
您的PHP正在查找$ _POST中尚未设置的变量。