每次尝试从 android 连接到 php 帐户时,我都会收到 JSON 解析器错误。下面你会看到我得到的错误:
08-16 10:45:23.002: E/JSON Parser(848): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
有人能帮助我吗...
PHP代码:
else{
//Store user
$user = $db->storeUser($name, $email, $password);
if ($user) {
// user stored successfully
$response["success"] = 1;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
}else{
//User failed to store
$response["error"] = 1;
$response["error_msg"] = "Oops something went wrong! Please try late.";
echo json_encode($response);
}
安卓代码: 安卓代码:
公共 JSONObject getJSONFromUrl(字符串 url,列表参数){
//making HTTP request
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader reader;
try {
reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) !=null){
sb.append(line + "n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("Buffer Error", "Error converting result " + e.toString());
}
//Try parse the string to a json object
try{
jObj = new JSONObject(json);
}catch(JSONException e){
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
//return JSON string
return jObj;
注册用户 JSONObject
public JSONObject registerUser(String name, String email, String password){
//building Parameters
List<NameValuePair> params = new ArrayList();
params.add(new BasicNameValuePair("tag", register_tag));
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
//Getting JSON object
JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
//return json
return json;
}
PhP警告:
08-16 10:45:22.990: E/JSON(848): <br />n<b>Warning</b>: mysql_num_rows() expects parameter 1 to be resource, boolean given in <b>C:\xampp\htdocs\thryfting_api\include\DB_Functions.php</b> on line <b>89</b><br />n{"tag":"register","success":0,"error":1,"error_msg":"Oops something went wrong! Please try late."}n
php代码:
链接 89 是 $no_of_rows
public function isUserExisted($email) {
$result = mysql_query("SELECT email from users WHERE email = ' $email'");
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
// user existed
return true;
} else {
// user not existed
return false;
}
}
用户表结构:
create table users(
uid int(11) primary key auto_increment,
unique_id varchar(23) not null unique,
name varchar(50) not null,
email varchar(100) not null unique,
encrypted_password varchar(80) not null,
salt varchar(10) not null,
created_at datetime,
updated_at datetime null
);
mysql_错误:
08-16 14:09:20.550: E/JSON(1028): <br />n<b>Warning</b>: mysql_num_rows() expects parameter 1 to be resource, boolean given in <b>C:\xampp\htdocs\thryfting_api\include\DB_Functions.php</b> on line <b>89</b><br />nNo database selected{"tag":"register","success":0,"error":1,"error_msg":"No database selected"}n