2015-02-23 88 views
0

我有一個基本的身份驗證表格現在工作。現在它只是在登錄成功時發送一個字符串「成功」,不成功時發送「假」。當它成功時,我想返回用戶數據,如UserID用於其他目的。我怎樣才能做到這一點? 這裏是我的PHP代碼:Php到Android獲取返回值

static public function Login($LoginID, $pass) { 
       $result = Db::getInstance()->getRow(' 
       SELECT `UserID` 
       FROM `'._DB_PREFIX_.'cman_users` 
       WHERE `LoginID` = '.$LoginID.' 
       AND `password` = \''.$pass.'\' 
       '); 
//echo "User:Login ($LoginID)<br>"; 
       if(isset($result['UserID']) && $result['UserID'] > 0) 
         return $result['UserID']; 

       else return 0; 
     } 

這裏是我的Android代碼:

httpclient=new DefaultHttpClient(); 
      // httppost= new HttpPost("http://127.0.0.1/API/check.php"); // make sure the url is correct. 
      //add your data 
      //httppost= new HttpPost("http://10.0.2.2:8080/API/check.php"); 
      httppost = new HttpPost("http://cman.avaninfotech.com/api/cman/v1/checkLogin/"); 
      nameValuePairs = new ArrayList<>(2); 

      nameValuePairs.add(new BasicNameValuePair("loginID",loginID.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value']; 
      nameValuePairs.add(new BasicNameValuePair("loginPass",loginPass.getText().toString().trim())); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      //Execute HTTP Post Request 
      response=httpclient.execute(httppost); 

      ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
      final String response = httpclient.execute(httppost, responseHandler); 
      System.out.println("Response : " + response); 
      runOnUiThread(new Runnable() { 
       public void run() { 
        // tv.setText("Response from PHP : " + response); 
        dialog.dismiss(); 
       } 
      }); 

      if(response.equalsIgnoreCase("1")){ 
       runOnUiThread(new Runnable() { 
        public void run() { 
         Toast.makeText(MainActivity.this, "Login Success", Toast.LENGTH_SHORT).show(); 
        } 
       }); 

       startActivity(new Intent(MainActivity.this, HomePage.class)); 
      }else{ 
       showAlert(); 
      } 

     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

回答

1

可能這會幫助你,你會得到響應爲JSON編碼字符串。對不起,我不知道android代碼。

static public function Login($LoginID, $pass) { 
        $result = Db::getInstance()->getRow(' 
        SELECT `UserID` 
        FROM `'._DB_PREFIX_.'cman_users` 
        WHERE `LoginID` = '.$LoginID.' 
        AND `password` = \''.$pass.'\' 
        '); 
    //echo "User:Login ($LoginID)<br>"; 
        if(isset($result['UserID']) && $result['UserID'] > 0) 
          echo json_encode($result['UserID']); 
        else 
         echo json_encode('0'); 
      } 
0

我想回到像用戶ID的用戶信息用於其他目的

既從PHP腳本返回狀態和用戶ID。使用json_encode創建一個json對象。做以下:

1.創建並從PHP返回的JSON對象:

class LoginDetails { 
     public $LoginID = ""; 
     public $status = ""; 

    } 
$obj = new LoginDetails(); 
$obj->LoginID = $LoginID; 
if(isset($result['UserID']) && $result['UserID'] > 0) 
    $obj->status = "1"; 
    else 
    $obj->status = "0"; 

return json_encode($obj); 

2.解析字符串從服務器recived在JSON對象:

JSONObject jsonObject=new JSONObject(response); 
    String strLoginID=jsonObject.optString("LoginID"); 
    int status=jsonObject.optInt("status");