2012-08-16 96 views
1

我每次尝试从android连接到php帐户时都会收到JSON解析器错误。下面你将看到的错误我得到: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); 
      } 

的Android代码: 的Android代码:

public JSONObject getJSONFromUrl(String url,List params){

//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_error:

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 
+3

之前没有建造它的代码是很难说你做错了什么,而是从消息很显然,你想不JSON字符串转换为JSONObject的。 – kosa 2012-08-16 14:51:39

+0

请发布您的代码和您试图去除空白的字符串。 但是,从你的异常看起来你试图将一个无效的json字符串转换为'jsonOBJECT'。 – Sayyam 2012-08-16 14:57:06

+0

我添加了代码 – 2012-08-16 14:57:14

回答

0

由于tolgap在他的评论指出,事实上,你有什么“BR”看起来像你的网页被发送回一个PHP错误,其HTML格式(因此BR)而不是JSON字符串。您应该先尝试修复该错误。

+0

好吧,我明白你在说什么了 – 2012-08-16 15:14:41

0

缺少__在PHP