【发布时间】:2018-10-31 14:34:47
【问题描述】:
我遇到了一个奇怪的问题,我找不到解决方案。我重新审视了过去完美运行的旧代码,但现在不知何故无法正常运行。
该项目是一个与数据库连接的安卓应用程序。我有一个登录活动,其中将执行一个发布请求以检查用户是否存在于数据库中。问题在于,与发布请求一起发送的参数并没有真正发送。
将 android 代码分开,我尝试单独测试发布请求。我将wampserver 3.1.4 与PHP 5.6.3、MySQL 5.7 和Apache 2.4 一起使用。要从 post 请求中检索参数,我正在使用此代码 sn-p:
$username = null;
$password = null;
var_dump($_POST);
if (isset($_POST['PARAM_EMAIL']))
$username = $mysqli->real_escape_string($_POST['PARAM_EMAIL']);
if (isset($_POST['PARAM_PASSWORD']))
$password = $mysqli->real_escape_string($_POST['PARAM_PASSWORD']);
我使用postman 测试请求并使用var_dump() 检查参数内容。
这是我如何编写请求以及得到什么结果的示例:
http://192.168.2.103/GestionDesCamions/check.php?PARAM_EMAIL=user@xx.com&PARAM_PASSWORD=xx
<pre class='xdebug-var-dump' dir='ltr'>
<small>C:\wamp64\www\GestionDesCamions\check.php:12:</small>
<b>array</b>
<i>(size=0)</i>
<i>
<font color='#888a85'>empty</font>
</i>
</pre>false
它说发布请求不包含任何参数,因此我最终得到错误,因为找不到用户。
我使用了 !empty() 而不是 isset() 并且得到了相同的结果。我测试了其他请求,特别是 GET 请求,它们工作正常。知道如何解决这个问题吗?我正在使用 Windows 10 作为操作系统。
编辑:
这是我在我的 android 应用程序中使用的 POST 方法:
public boolean SendToUrl(String strURL,
ArrayList<NameValuePair> nameValuePairs) {
// Creer un buffer
StringBuilder builder = new StringBuilder();
// Creer un client Http
HttpClient client = new DefaultHttpClient();
// Creer un obejet httppost pour utiliser la methode post
HttpPost httppost = new HttpPost(strURL);
try {
// UrlEncodedFormEntit() : An entity composed of a list of
// url-encoded pairs
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// recuperer la reponse
HttpResponse response = client.execute(httppost);
StatusLine statusLine = response.getStatusLine();
// recuperer le ack
int statusCode = statusLine.getStatusCode();
// si ack =200 connexion avec succee
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
// erreur du chargement
Toast.makeText(_mContext, "pas de connexion", Toast.LENGTH_LONG)
.show();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return Boolean.parseBoolean(builder.toString());
}
【问题讨论】:
-
尝试
$_GET,因为您似乎在查询字符串上传递参数 -
效果很好!但它不适用于android应用程序。一个解决方案也是将其更改为来自应用程序的 GET 请求!但我仍然不明白为什么会发生这种情况,因为过去发布请求工作得很好。如果我没记错的话,使用 POST 请求是通过登录来做的正确事情!还是我错过了什么?
-
Android 应用使用 POST 还是 GET?
-
它使用 POST !我刚刚用我在我的 android 应用程序中使用的 POST 方法编辑了我的帖子
标签: php android mysql wampserver