1
我试图用android应用程序上传数据库中的一些数据。到现在为止一切工作正常,但现在我需要添加另一列,所以我修改了代码,现在看起来像手机发送的数据不能被我的PHP文件读取。我的应用程序的代码的最重要的部分是:
private static void post(String endpoint, Map<String, String> params)
throws IOException {
URL url;
try {
url = new URL(endpoint);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("invalid url: " + endpoint);
}
StringBuilder bodyBuilder = new StringBuilder();
Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
// constructs the POST body using the parameters
while (iterator.hasNext()) {
Entry<String, String> param = iterator.next();
bodyBuilder.append(param.getKey()).append('=')
.append(param.getValue());
if (iterator.hasNext()) {
bodyBuilder.append('&');
}
}
String body = bodyBuilder.toString();
Log.v(TAG, "Posting '" + body + "' to " + url);
byte[] bytes = body.getBytes();
HttpURLConnection conn = null;
try {
Log.e("URL", "> " + url);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
//conn.setFixedLengthStreamingMode(bytes.length);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
// post the request
OutputStream out = conn.getOutputStream();
Log.v(TAG, "Has posted" + bytes);
out.write(bytes);
out.close();
// handle the response
int status = conn.getResponseCode();
if (status != 200) {
Log.v(TAG, "Post Failed");
throw new IOException("Post failed with error code " + status);
}
} finally {
if (conn != null) {
conn.disconnect();
}
}
在它看起来像应用程序已经能够以字节格式有效地发布代码的logcat:
V/Alvaro Lloret GCM﹕ Posting email=llor&name=hola&arduinoweb=jaj®Id=APA' to http://youdomotics.com/mysecurity1/register.php
E/URL﹕ > http://website.com/mysecurity1/register.php
V/RenderScript﹕ Application requested CPU execution
V/RenderScript﹕ 0xaec16400 Launching thread(s), CPUs 4
V/Alvaro Lloret GCM﹕ Has posted[[email protected]
V/GCMRegistrar﹕ Setting registeredOnServer status as true until 2015-09-11 20:21:30.364
V/GCMBaseIntentService﹕ Releasing wakelock
然后,代码接收与PHP的岗位是:
<?php
// response json
$json = array();
if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["regId"])) {
require("config.php");
$con = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$name = $_POST["name"];
$email = $_POST["email"];
$arduinoweb = $_POST["arduinoweb"];
$gcm_regid = $_POST["regId"]; // GCM Registration ID
include_once './gcm.php';
$query = "INSERT INTO gcm_users_new(name, email, gcm_regid, arduinoweb, created_at) VALUES('$name', '$email', '$gcm_regid', '$arduinoweb', NOW())";
mysqli_query($con, $query);
} else {
// user details missing
}
?>
此代码工作完全没有新的参数arduinoweb,但自从我加入这个其他参数,该行不会被添加到databa SE。如果我评论条件if(isset ...),那么文件在表中添加一行,但它是空的...
任何想法?
谢谢!
$ _POST [“name”]将接受从android发送的参数的名称类似的其他参数在Android中接受来自android – KOTIOS
我不认为这个问题是不是在Android应用程序中的代码不在PHP文件,也许是服务器,这是在goDaddy,但我真的找不到解决方案 – Alvaro