我有一个链接到数据库的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
?>
然后我尝试从Android向此PHP脚本发出HTTP请求。
当我执行GET请求时,一切正常。问题是,在某些PHP脚本中,我需要发出POST请求,主要是查询参数,例如:]
<?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
?>
现在,如果我通过带有发送POST数据的表单的简单HTML网页访问此php,一切正常,并且在浏览器中得到的结果是JSON数组。
但是从Android Studio,我得到的响应是HTML代码,例如:
<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>[]
当我想获取一个JSON数组时。
Android中POST请求的代码:
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
如您所见,我通过从String数组构建的JSON发送POST参数。我已经检查过请求JSON是否正确构建:
{"path_id":"1"}
所以我想我在Android中对POST请求做错了。有什么想法吗?
我通过更改发送POST数据的方式解决了它:
[私有JSONArray makePhpPostRequest(String phpUrl,String [] [] postParameters)抛出MalformedURLException,IOException,JSONException {URL url =新URL(HOST_IP + phpUrl);
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
现在我写那些URL参数而不是发送JSON对象