0

Android用のサーバー側を実装しようとしています。Forthis 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ではこれらのエラーが発生しています:

注意: 未定義のインデックス: 5 行目の C:\xampp\htdocs\GCM\addReg.php の reg_id

注意: 未定義のインデックス: C:\xampp\htdocs\GCM\addReg.php の 6 行目の App_Name

注意: 未定義のインデックス: C:\xampp\htdocs\GCM\addReg.php の 7 行目の App_Version

警告: mysqli_close() は、パラメーター 1 が mysqli であると想定します。リソースは、24 行目の C:\xampp\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];



    }

}
4

2 に答える 2

1
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" ;
}

データベースを保護するために、条件の基本的な流れを修正し、特殊文字をエスケープしました。それはうまくいくはずです。

于 2013-04-11T11:10:16.730 に答える
0

データを POST する場合は、$_REQUEST の代わりに $_POST を使用してみてください。わかりやすくするために、7 行目以降では、割り当てられた変数 ($Reg_ID、$App_Name、$App_Version) を $_REQUEST[...] の代わりに再利用します。

于 2013-04-11T11:00:18.757 に答える