【发布时间】:2015-05-13 12:23:01
【问题描述】:
我知道这个问题已经回答了很多次了。我尝试了不同的链接,但我无法解决这个问题。
Java 代码:
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Username",u));
params.add(new BasicNameValuePair("Email",e));
params.add(new BasicNameValuePair("Password", p));
params.add(new BasicNameValuePair("Confirmpassword", cp));
JSONObject json = jsonParser.makeHttpRequest(url_logindetails, "POST", params);
PHP 代码:
$response = array();
// check for required fields
if (isset($_POST['Username']) || isset($_POST['Email'])|| isset($_POST['Password']) ||isset($_POST['Confirmpassword']) ) {
$Username = $_POST['Username'];
$Email = $_POST['Email'];
$Password = $_POST['Password'];
$Confirmpassword = $_POST['Confirmpassword'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO login(UserName, email,password,confirmpassword VALUES('$Username','$Email','$Password','$Confirmpassword')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Company successfully added.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
【问题讨论】:
-
我认为您的服务器响应是 html 字符串而不是 JSON 字符串。
-
是的,这是真的,但我不知道如何纠正它。可以给我代码吗?
-
使用 jsonlint 验证响应。你的 php 代码似乎没问题。可能是一些数据库连接错误,这是导致 html 输出的原因。
-
对我帮助不大......有没有人可以帮助我解决这个问题?
标签: php android json httprequest