1

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 コード: 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
4

2 に答える 2

0

tolgapが彼のコメントで指摘したように、「br」について何かがあるという事実は、JSON 文字列ではなく HTML 形式 (したがって BR) を使用して、ページが PHP エラーを返しているように見えます。最初にそのエラーを修正してみてください。

于 2012-08-16T15:08:29.587 に答える
0

PHP のコンストラクトの前に __ がありません

于 2012-08-17T14:10:25.393 に答える