Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试使用对PHP的POST请求在Android中检索数据库信息_Php_Android_Http_Post - Fatal编程技术网

尝试使用对PHP的POST请求在Android中检索数据库信息

尝试使用对PHP的POST请求在Android中检索数据库信息,php,android,http,post,Php,Android,Http,Post,我有一个连接到数据库的Android应用程序。为了访问这个数据库,我使用PHP脚本以JSON的形式检索数据库信息,例如 <?php $HOST = '127.0.0.1'; $PORT = "5432"; $DBNAME = "LOLRunes"; $USER = "postgres"; $PASSWORD = "sth"; $conn = pg_connect("host=$HOST port=$PORT dbname=$DBNAME user=$USER password=$PASS

我有一个连接到数据库的Android应用程序。为了访问这个数据库,我使用PHP脚本以JSON的形式检索数据库信息,例如

<?php
$HOST = '127.0.0.1';
$PORT = "5432";
$DBNAME = "LOLRunes";
$USER = "postgres";
$PASSWORD = "sth";

$conn = pg_connect("host=$HOST port=$PORT dbname=$DBNAME user=$USER password=$PASSWORD");

if ($conn) {
    $result = pg_query($conn, "SELECT * FROM champions");

    $data = array();

    while ($row = pg_fetch_assoc($result)) {            
        $data[] = $row;
    } //while

    pg_close($conn);

    echo json_encode($data);
} //if
?>

所以我想我在安卓系统中的POST请求有问题。有什么想法吗?

我通过改变发送帖子数据的方式解决了这个问题:

私有JSONArray MakephPropostRequest(字符串phpUrl,字符串[][]后参数)抛出畸形的DurException、IOException、JSONException{ URL=新URL(主机IP+phpUrl)

HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setRequestMethod(“POST”);
//构建URL参数
字符串urlParameters=“&”;
for(int i=0;i

现在我编写这些URL参数,而不是发送JSON对象

Note;您的PHP易受SQL注入攻击。我应该如何解决它?有没有更好的方法将POST连接到查询中?没有,没有更好的方法连接它。连接是不安全的,您需要使用准备好的语句。这将强制将数据与查询本身分开发送到MySQL服务器,因此消除了SQL注入的可能性。
<?php
$HOST = '127.0.0.1';
$PORT = "5432";
$DBNAME = "LOLRunes";
$USER = "postgres";
$PASSWORD = "sth";

$conn = pg_connect("host=$HOST port=$PORT dbname=$DBNAME user=$USER password=$PASSWORD");

if ($conn) {
    $result = pg_query($conn, "SELECT * FROM runes_keystones WHERE path_id = " . $_POST["path_id"]);

    $data = array();

    while ($row = pg_fetch_assoc($result)) {            
        $data[] = $row;
    } //while

    pg_close($conn);

    echo json_encode($data);
} //if
?>
 <br /><font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: path_id in C:\wamp\www\getRunesKeystones.php on line <i>11</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0003</td><td bgcolor='#eeeeec' align='right'>248464</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\getRunesKeystones.php' bgcolor='#eeeeec'>..\getRunesKeystones.php<b>:</b>0</td></tr></table></font><br /><font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: pg_query(): Query failed: ERROR:  error de sintaxis al final de la entradaLINE 1: SELECT * FROM runes_keystones WHERE path_id =                                                       ^ in C:\wamp\www\getRunesKeystones.php on line <i>11</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0003</td><td bgcolor='#eeeeec' align='right'>248464</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\getRunesKeystones.php' bgcolor='#eeeeec'>..\getRunesKeystones.php<b>:</b>0</td></tr><tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0360</td><td bgcolor='#eeeeec' align='right'>250720</td><td bgcolor='#eeeeec'><a href='http://www.php.net/pg_query' target='_new'>pg_query</a>(  )</td><td title='C:\wamp\www\getRunesKeystones.php' bgcolor='#eeeeec'>..\getRunesKeystones.php<b>:</b>11</td></tr></table></font><br /><font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: pg_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\getRunesKeystones.php on line <i>15</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0003</td><td bgcolor='#eeeeec' align='right'>248464</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\getRunesKeystones.php' bgcolor='#eeeeec'>..\getRunesKeystones.php<b>:</b>0</td></tr><tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0367</td><td bgcolor='#eeeeec' align='right'>251144</td><td bgcolor='#eeeeec'><a href='http://www.php.net/pg_fetch_assoc' target='_new'>pg_fetch_assoc</a>(  )</td><td title='C:\wamp\www\getRunesKeystones.php' bgcolor='#eeeeec'>..\getRunesKeystones.php<b>:</b>15</td></tr></table></font>[]
 private JSONArray makePhpPostRequest(String phpUrl, String[][] postRequest) throws MalformedURLException, IOException, JSONException {
    URL url = new URL(HOST_IP + phpUrl);

    HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();

    httpUrlConnection.setRequestMethod("POST");

    //Build JSON containing POST args
    String requestJSON = "{";
    for (int i = 0; i < postRequest.length; i++) {
        String key = postRequest[i][0];
        String value = postRequest[i][1];

        requestJSON += "\"" + key + "\":\"" + value + "\"";

        if (i < postRequest.length - 1) {
            requestJSON += ",";
        } //if
    } //for
    requestJSON += "}";

    //Writing request JSON
    byte[] jsonByte = requestJSON.getBytes();
    OutputStream os = httpUrlConnection.getOutputStream();
    os.write(jsonByte, 0, jsonByte.length);

    InputStreamReader inputStreamReader = new InputStreamReader((httpUrlConnection.getInputStream()));
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

    String json = "";
    String inputLine;

    while ((inputLine = bufferedReader.readLine()) != null) {
        json += inputLine;
    } //while

    Log.d("jsonResponse", json);

    bufferedReader.close();

    JSONArray jsonArray = new JSONArray(json);

    return jsonArray;
} //makePhpPostRequest
{"path_id":"1"}
    HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
    httpUrlConnection.setDoOutput(true);
    httpUrlConnection.setRequestMethod("POST");

    //Build URL parameters
    String urlParameters = "&";
    for (int i = 0; i < postParameters.length; i++) {
        String key = postParameters[i][0];
        String value = postParameters[i][1];

        urlParameters += key + "=" + value;
    } //for

    //Writing POST parameters
    OutputStream os = httpUrlConnection.getOutputStream();
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
    writer.write(urlParameters);
    writer.flush();
    writer.close();

    InputStreamReader inputStreamReader = new InputStreamReader((httpUrlConnection.getInputStream()));
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

    String json = "";
    String inputLine;

    while ((inputLine = bufferedReader.readLine()) != null) {
        json += inputLine;
    } //while

    bufferedReader.close();

    JSONArray jsonArray = new JSONArray(json);

    return jsonArray;
} //makePhpPostRequest