2013-01-20 29 views
0

我已经成功地创建一个认证使用httppost从Java Android代码通过PHP到MySQL。然而,我遇到问题时会创建一个新用户。我检查了其他用户的多个帖子,我认为它看起来不错。另外,我已经创建了一个简单的html表单来测试新用户的php创建,并且这也起作用。httppost Android,价值不通过数据库创建用户

任何想法将不胜感激。这是我的Java代码和PHP下面。谢谢!

try { 
      // create new array list 
      nameValuePairs = new ArrayList<NameValuePair>(); 

      // place them in an array list 
      nameValuePairs.add(new BasicNameValuePair("username", user)); 
      nameValuePairs.add(new BasicNameValuePair("password", pass)); 

      // add array list to http post 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      // assign executed form container to response 
      response = httpclient.execute(httppost); 

      // check status code, 
      if (response.getStatusLine().getStatusCode() == 200) { 
       // assign response entity to htpp entitiy 
       entity = response.getEntity(); 

       // chekc if entity is not null 
       if (entity != null) { 

        Log.i("RESPONSE",EntityUtils.toString(entity)); 

        Intent intent = new Intent(CreateAccount.this, 
          MainActivity.class); 
        startActivity(intent); 

        // } 

        // display a taost saying login was a success 
        Toast.makeText(CreateAccount.this, user, 
          Toast.LENGTH_SHORT).show(); 
        // } 

       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
      // display toast when there is a connection error 
      Toast.makeText(getBaseContext(), "ConnectionError", 
        Toast.LENGTH_SHORT).show(); 
     } 

PHP代码:

//get form data 

$user = mysql_real_escape_string($_POST['user']); 

$pass = mysql_real_escape_string($_POST['pass']); 

echo "INSERT INTO androidlogin (user, pass) VALUES ('$user','$pass')"; 

//insert data 
$insert = mysql_query("INSERT INTO androidlogin (user, pass) VALUES ('$user','$pass')"); 
if($insert){ 
$arr2 = array("user" => $user, "pass" => $pass); 
echo json_encode($arr2); 

} 

回答

0

投递值被称为 “用户名” 和 “密码”,而服务器将检查 “用户” & “通行证”。你必须使用相同的密钥。

+0

非常感谢。有效!!!! – user1720683

+0

没问题,别忘了接受答案;) – AndacAydin