【发布时间】:2017-01-31 12:07:35
【问题描述】:
美好的一天!
我的 android 应用程序需要将 POST 数据发送到 PHP 脚本,但是该脚本没有检测到正在发送的数据。由于我对 Java Android 编程比较陌生,如果我忽略了一个简单的概念,请原谅我。
由于我从未涉足过ajax,我想要一个不实现ajax 的解决方案,这就是为什么“Get post data from Android to PHP”和“Posting data from android to PHP”中的解决方案不符合我想要的解决方案的原因.
下面是我的相关源码:
插入Contact.java
public void sendRequest() {
SendtoPHP sendtoPHP = new SendtoPHP();
sendtoPHP.execute(new String[] {
"https://a/foo.php"
});
}
private class SendtoPHP extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
BufferedReader reader = null;
String text = "";
try {
String data = URLEncoder.encode("contactNo=98765432", "UTF-8");
String type = "application/x-www-form-urlencoded";
URL url = new URL(urls[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", type);
conn.setRequestProperty("Content-Length", String.valueOf(data.length()));
OutputStream outputStream = conn.getOutputStream();
outputStream.write(data.getBytes());
System.out.println("test success!");
// server response
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while ((line = reader.readLine()) != null) {
// Append server response in string
sb.append(line + "\n");
}
text = sb.toString();
} catch (Exception e) {
System.out.println(e);
} finally {
try {
reader.close();
return text;
} catch (Exception e) {
System.out.println(e);
return "Failed to close reader.";
}
}
}
@Override
protected void onPostExecute(String result) {
System.out.println(result);
System.out.println("SUCCESS!");
}
}
foo.php
<?php
if (isset($_POST['contactNo'])) {
$contactNo = $_POST['contactNo'];
//require_once 'dbHandler.php';
//$con = new dbConnect();
$querymysql = new mysqli("my_db_ip", "my_username", "my_password", "my_db");
$result = $querymysql -> query("call insertContacts($contactNo)");
if ($result) {
echo "Insert successful!";
} else {
echo "Insert failed!";
}
$result -> close();
$querymysql -> close();
} else {
echo "Hi! Welcome to foo.php. You have reached a dead end as you are physically connecting
to our backend scripts.<br/>This page will unfortunately serve you no purpose. Sorry for the inconvenience
caused.";
}
Android Studio 控制台日志
I/System.out: test success!
I/System.out: Hi! Welcome to foo.php. You have reached a dead end as you are physically connecting
I/System.out: to our backend scripts.<br/>This page will unfortunately serve you no purpose. Sorry for the inconvenience
I/System.out: caused.
I/System.out: SUCCESS!
正如我们从控制台日志中看到的,POST 数据没有发送到 PHP 脚本,因为出现了不需要的回显消息。
任何帮助将不胜感激! :)
编辑:
我已经包含了根据@FerryBig 的答案修改我的代码后出现的错误的堆栈跟踪。
W/System.err: java.io.FileNotFoundException: https://a/foo.php
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:250)
W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java)
W/System.err: at name.calvin.testdbconnect.insertContact$SendtoPHP.doInBackground(insertContact.java:62)
W/System.err: at name.calvin.testdbconnect.insertContact$SendtoPHP.doInBackground(insertContact.java:41)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:305)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
W/System.err: at java.lang.Thread.run(Thread.java:761)
【问题讨论】:
-
在 PHP 文件中的 if 语句之前,尝试将
var_dump($_POST)转储到脚本中,看看是否有任何结果。另外,尝试转储var_dump(file('php://input'))以查看您是否将数据作为输入流而不是普通的 POST 参数。 -
您的
Posting data from android to php链接确实符合您的问题。使用 post 方法时,您必须通过name value pair发送您的数据,如该链接的问题中所述