Php到android获取返回值


Php to android getting the return value

我现在有一个基本的身份验证表单。现在,当登录成功时,它只发送一个字符串"success",而当登录不成功时,则发送一个"false"。当它成功时,我想返回用户数据,如UserID,用于其他目的。我该怎么做?这是我的Php代码:

 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;
        }

这是我的安卓代码:

 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();
        }
    }

这可能会对你有所帮助,你会得到Json编码字符串的响应。对不起,我不知道安卓代码。

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');
            }

我想为其他目的返回用户数据,如UserID

从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.解析json对象中从服务器接收的字符串:

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