我正在尝试为android实现服务器端。第四,在安卓我使用这个代码:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
// Get the deviceID
nameValuePairs.add(new BasicNameValuePair("reg_id", val[1]));
nameValuePairs.add(new BasicNameValuePair("App_Name",val[2]));
nameValuePairs.add(new BasicNameValuePair("App_Version", val[3]));
现在在服务器端,我想访问这些值并将其存储在数据库中。我的php代码:
include('connectdb.php');
$Reg_ID=$_REQUEST['reg_id'];
$App_Name=$_REQUEST['App_Name'];
$App_Version=$_REQUEST['App_Version'];
if($_REQUEST['reg_id']!='NULL' && $_REQUEST['App_Name']!='NULL' && $_REQUEST['App_Version']!='NULL')
{
mysqli_query($con,"INSERT INTO registration (Reg_ID, APP_Name,App_Version)
VALUES ('".$_REQUEST['reg_id']."', '".$_REQUEST['App_Name']."','".$_REQUEST['App_Version']."')");
}
else
{
}
mysqli_close($con);
但在php中,我得到了以下错误:
注意:上的C:''examplep''htdocs''GCM''addReg.php中的未定义索引:reg_id第5行
注意:未定义的索引:上的C:''examplep''htdocs''GCM''addReg.php中的App_Name第6行
注意:未定义的索引:C:''examplep''htdocs''GCM''addReg.php中的App_Version在线7
警告:mysqli_close((要求参数1为mysqli,resource在第24行上的C:''examplep''htdocs''GCM''addReg.php中给出
有什么帮助吗
编辑
public class SendMail extends AsyncTask<String, String, String> {
private String endResult;
HttpResponse response;
@Override
protected String doInBackground(String... val) {
String url=val[0];
//Log.i("Url", url + "");
// Log.d("C2DM", "Sending registration ID to my application server");
HttpPost post = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("reg_id", val[1]));
nameValuePairs.add(new BasicNameValuePair("App_Name",val[2]));
nameValuePairs.add(new BasicNameValuePair("App_Version", val[3]));
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 30000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 30000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient hc = new DefaultHttpClient(httpParameters);
ResponseHandler<String> res = new BasicResponseHandler();
try {
post.addHeader("Content-Type","application/x-www-form-urlencoded");
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
response = hc.execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
Log.i("endResult", "" + endResult);
super.onPostExecute(result);
publishProgress(endResult);
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
String result = values[0];
}
}
(超时代码基于kuester2000的此答案(
include('connectdb.php');
//First, check whether the values exist and only then use them.
if(isset($_REQUEST['reg_id'], $_REQUEST['App_Name'], $_REQUEST['App_Version']))
{
$id = mysqli_real_escape_string($con, $_REQUEST['req_id']) ;
$name = mysqli_real_escape_string($con, $_REQUEST['App_Name']) ;
$version = mysqli_real_escape_string($con, $_REQUEST['App_Version']) ;
$query = "INSERT INTO registration (Reg_ID, APP_Name, App_Version)
VALUES ('{$id}', '{$name}','{$version}') ;" ;
$result = mysqli_query($con, $query) ;
if ($result){
echo "Success" ;
} else {
echo "Failure" ;
}
} else {
echo "Not sufficient data for request" ;
}
我刚刚更正了基本的条件流,并转义了特殊字符以保护您的数据库。它应该起作用。
如果要发布数据,请尝试使用$_POST而不是$_REQUEST。为了清楚起见,在第7行及以下重用指定的变量($Reg_ID、$App_Name、$App_Version(,而不是$_REQUEST[…].