安卓-数据不';无法发布到PHP脚本
你好 我的android应用程序需要将安卓-数据不';无法发布到PHP脚本,php,android,post,Php,Android,Post,你好 我的android应用程序需要将POST数据发送到PHP脚本,但是脚本没有检测到正在发送的数据。由于我对Java Android编程比较陌生,如果我忽略了一个简单的概念,请原谅 由于我从未涉足过ajax,因此我想要一个不实现ajax的解决方案,这就是为什么“”和“”中的解决方案不符合我想要的解决方案 以下是我的相关源代码: insertContact.java public void sendRequest() { SendtoPHP sendtoPHP = new SendtoP
POST
数据发送到PHP
脚本,但是脚本没有检测到正在发送的数据。由于我对Java Android编程比较陌生,如果我忽略了一个简单的概念,请原谅
由于我从未涉足过ajax
,因此我想要一个不实现ajax
的解决方案,这就是为什么“”和“”中的解决方案不符合我想要的解决方案
以下是我的相关源代码:
insertContact.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!");
}
}
当向Web服务器发送数据时,您应该遵循“非常接近”命令。它说您应该使用
=
作为数据的分隔符,当您将其包含在数据中时,您会意外地将其通过urlcoder
,从而更改字符
通过将urlcoder
更改为分别对键侧(左)和值侧(右)进行编码,它将正确发送数据
String data = URLEncoder.encode("contactNo", "UTF-8")
+ "="
+ URLEncoder.encode("98765432", "UTF-8");
在PHP文件中的if语句之前,尝试在脚本中转储
var\u dump($\u POST)
,看看是否有任何结果。另外,尝试转储var\u dump(文件php://input“)
查看是否将数据作为输入流而不是普通的POST-params获取。您的将数据从android发布到php
链接确实符合您的问题。当使用post方法时,您必须通过名称-值对
发送数据,如该链接的问题中所述,使用建议的答案,我会遇到以下异常:I/System.out:java.io.FileNotFoundException:https://a/foo.php
帮助!
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!
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)
String data = URLEncoder.encode("contactNo", "UTF-8")
+ "="
+ URLEncoder.encode("98765432", "UTF-8");