JSON Parser错误

时间:2012-08-16 14:50:27

标签: java android json

每次尝试从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);
            }

Android COde: 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代码:

link 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

2 个答案:

答案 0 :(得分:0)

正如tolgap在他的评论中指出的那样,你有一些关于“br”的事实看起来你的页面正在发送一个PHP错误,其HTML格式(因此是BR)而不是JSON字符串。您应该首先尝试修复该错误。

答案 1 :(得分:0)

在php中构造之前缺少__