2012-10-18 89 views
0

我有一個問題... 我試圖將設備的udid存儲到我的數據庫當用戶註冊... 我得到這個錯誤logcat:JSON + PHP + Java的 - Android登錄到mysql

10-18 17:17:08.141: E/JSON(1712): <br /> 
10-18 17:17:08.141: E/JSON(1712): <b>Warning</b>: Missing argument 4 for DB_Functions::storeUser(), called in /home/matbest1/public_html/android_login_api/index.php on line 64 and defined in <b>/home/matbest1/public_html/android_login_api/include/DB_Functions.php</b> on line <b>25</b><br /> 
10-18 17:17:08.141: E/JSON(1712): {"tag":"register","success":0,"error":1,"error_msg":"Error occured in Registartion"} 
10-18 17:17:08.151: E/JSON Parser(1712): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject 
10-18 17:17:08.381: D/dalvikvm(1712): GC_CONCURRENT freed 296K, 5% free 9394K/9799K, paused 2ms+4ms 

我試圖存儲在數據庫中的UDID ... 繼承人的registeractivity:

public JSONObject registerUser(String name, String email, String password, String uid){ 
    // Building Parameters 
    List<NameValuePair> params = new ArrayList<NameValuePair>(); 
    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)); 
    params.add(new BasicNameValuePair("uid", uid)); 

    // getting JSON Object 
    JSONObject json = jsonParser.getJSONFromUrl(registerURL, params); 
    // return json 
    return json; 
} 

註冊:

UserFunctions userFunction = new UserFunctions(); 
      JSONObject json = userFunction.registerUser(name, email, password, uid); 

PHP代碼:

public function storeUser($name, $email, $password, $uid) { 
    $uuid = $uid; 
    $hash = $this->hashSSHA($password); 
    $encrypted_password = $hash["encrypted"]; // encrypted password 
    $salt = $hash["salt"]; // salt 
    $result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())"); 
    // check for successful store 
    if ($result) { 
     // get user details 
     $uid = mysql_insert_id(); // last inserted id 
     $result = mysql_query("SELECT * FROM users WHERE uid = $uid"); 
     // return user details 
     return mysql_fetch_array($result); 
    } else { 
     return false; 
    } 
} 

&

if ($tag == 'register') { 
     // Request type is Register new user 
     $name = $_POST['name']; 
     $email = $_POST['email']; 
     $password = $_POST['password']; 
     $uid = $_POST['uid']; 

     // check if user is already existed 
     if ($db->isUserExisted($email)) { 
      // user is already existed - error response 
      $response["error"] = 2; 
      $response["error_msg"] = "User already existed"; 
      echo json_encode($response); 
     } 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"] = "Error occured in Registartion"; 
       echo json_encode($response); 
      } 
     } 
    } else { 
     echo "Invalid Request"; 
    } 
} else { 
    echo "Access Denied"; 
} 

有人能幫助我嗎?我對此很陌生...非常感謝。

+0

'java.lang.String類型的值不能轉換爲JSONObject' - 看起來好像傳遞給jsonParser的JSON格式錯誤。 –

+0

所以當我解析udid時有一些錯誤? – matteo

+0

閱讀錯誤消息。它會拋出字符串''標籤的開頭。 '
'標籤在JSON中甚至不合法,所以有些東西肯定是錯誤的。看看JSON。 –

回答

1

服務器響應包含在HTML中顯示的PHP錯誤..

所以這是正常的,你的Android應用無法解析任何JSON ..

第一件事情,你需要確保你的android應用程序將所有需要的信息(4個參數PHP等待),因爲PHP錯誤clealry告訴你,它缺少參數#4:$ UID

嘗試 print_r($_REQUEST); 在你的PHP文件的開頭,看看PHP接收所有參數。如果沒有:概率lem是在你的android應用程序(uid是一個字符串?一個整數?您是否需要轉換它?)