我把头发拉出来。我可以使用相同的代码写入数据库(期望它是INSERT INTO命令),但这不起作用。
PHP:
$id = $_POST['id'];
//$id = '000000000002';
$uri = $_POST['uri'];
//$uri='sdfgdsfg';
try {
$stmt = $conn->prepare("UPDATE USERS SET PHOTO=? WHERE ID=?");
$stmt->execute(array($uri, $id));
$response["success"] = 1;
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
$response["success"] = 0;
}
echo json_encode($response);
JAVA:
try {
paramsC = new ArrayList<NameValuePair>();
paramsC.add(new BasicNameValuePair("id", userid));
paramsC.add(new BasicNameValuePair("uri", "hello"));
JSONParser jsonParser; jsonParser = new JSONParser();
JSONObject json = jsonParser.makeHttpRequest(set_profile_photo_uri, "POST", paramsC);
try {
int success = json.getInt("success"); //error points to this line
if (success == 1) {
// successfully updated
Log.d("SIGNUP", "success");
} else {
Log.d("SIGNUP", "fail");
}
}catch(JSONException e){
//Log.e("log_tag", "Error parsing data "+e.toString());
//Log.e("log_tag", "Failed data was:\n");
}
} catch (Exception e) {
e.printStackTrace();
}
我收到错误:
02-17 21:37:45.104: E/JSON Parser(21276): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
02-17 21:37:45.104: W/System.err(21276): java.lang.NullPointerException
02-17 21:37:45.124: W/System.err(21276): at com.example.myapp.TabFragment1$8.onClick(TabFragment1.java:171)
但是,此代码有效,但为什么?:
try {
$stmt = $conn->prepare("INSERT INTO USERS (USERNAME, PASSWORD, NAME, EMAIL, GENDER, DATEOFREG_PHONE, DATEOFREG_SV) VALUES
(:username, :password, :name, :email, :gender, :dateofreg_phone, :dateofreg_sv)");
$query_params = array(
':username' => $username,
':password' => $password,
':name' => $name,
':email' => $email,
':gender' => $gender,
':dateofreg_phone' => $dateofreg_phone,
':dateofreg_sv' => $dateofreg_sv
);
$stmt->execute($query_params);
$response["success"] = 1;
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
$response["success"] = 0;
}
echo json_encode($response);
当我将PHP代码输入浏览器时,它可以正常工作。
答案 0 :(得分:1)
通过评论回答关闭此问题:确保将查询发送到正确的网址。