繁体   English   中英

如何在 android 中使用 retrofit @Query 注释进行过滤

[英]How can I use the retrofit @Query annotation for filtration in android

我在下面有一个 PHP 文件

<?php
require dirname(__FILE__).'/../DbConnection/conn.php';

if($conn){
    $sqlStr = "SELECT
    YEAR(OrderBill.BillingDate) AS year,
    SUM(MultiPay.Amount) AS growth_rate
    FROM Rst_TblMultiPayModes MultiPay
    LEFT OUTER JOIN Rst_TblOrderBills OrderBill
    ON MultiPay.OrderBillCode = OrderBill.OrderBillCode
    WHERE OrderBill.isBillCleared=1 AND OrderBill.isMerged=0
    AND MultiPay.PaymentMode IN(1,2,3,4,6,7)
    $_GET['mselectperiod']

    GROUP BY
    YEAR(OrderBill.BillingDate)";

    $sqlResult = mysqli_query($conn,$sqlStr);
    $response = array();

    while($row = mysqli_fetch_assoc($sqlResult)){
        array_push($response,$row);
    }
    
    echo json_encode($response);
    mysqli_close($conn);
}else{
    echo "Host Connection Error";
}
?>

这个 PHP 文件在我的浏览器中执行时工作正常,如下所示Salegrowthratechartyear.php 的执行结果

现在说到Android,我通过创建接口使用Retrofit库如下

public interface ApiInterface {
    @GET("{PHP_QRY_FILE_NAME}")
    Call<List<Growth>> getGrowthInfo(@Path(value="PHP_QRY_FILE_NAME",encoded = true) String mPhpQryFileName);

}

然后我调用如下

call = ChartApiClient.getApiClient().create(ApiInterface.class).getGrowthInfo("salesgrowthratechartyear.php);

以上也可以正常工作,但只有当我在 PHP 文件中删除 $_GET['mselectperiod'] 时。 当我想在我的界面中的 Php 文件中使用 $_GET['mselectperiod'] 时,问题就来了,如下所示

public interface ApiInterface {
    @GET("{PHP_QRY_FILE_NAME}")
    Call<List<Growth>> getGrowthInfo(@Path(value="PHP_QRY_FILE_NAME",encoded = true) String mPhpQryFileName, @Query("mselectperiod") String mWhereClause);

}

然后我调用如下

call = ChartApiClient.getApiClient().create(ApiInterface.class).getGrowthInfo("salesgrowthratechartyear.php",SalesGrowthChart.mFinalSelectPeriodQry);

请注意,“SalesGrowthChart.mFinalSelectPeriodQry”是一个接收变化字符串值的变量,如下所示

"AND YEAR(BillingDate)>=2019 AND YEAR(BillingDate)<=2022"

什么都没发生。 我错过了什么?

将界面修改为

public interface ApiInterface {
@GET("{PHP_QRY_FILE_NAME}")
Call<List<Growth>> getGrowthInfo(@Path(value="PHP_QRY_FILE_NAME",encoded = true) String mPhpQryFileName, @Query(value="mselectperiod",encoded = true) String mWhereClause);

}

我省略了'Value='并且它应该被编码为 true(,encoded = true)

然后在我的 PHP 文件中,我为$_GET编辑如下

 <?php require dirname(__FILE__).'/../DbConnection/conn.php'; $mselectperiod = $_GET["mselectperiod"]; if($conn){ $sqlStr = "SELECT YEAR(OrderBill.BillingDate) AS year, SUM(MultiPay.Amount) AS growth_rate FROM Rst_TblMultiPayModes MultiPay LEFT OUTER JOIN Rst_TblOrderBills OrderBill ON MultiPay.OrderBillCode = OrderBill.OrderBillCode WHERE OrderBill.isBillCleared=1 AND OrderBill.isMerged=0 AND MultiPay.PaymentMode IN(1,2,3,4,6,7) $mselectperiod GROUP BY YEAR(OrderBill.BillingDate)"; $sqlResult = mysqli_query($conn,$sqlStr); $response = array(); while($row = mysqli_fetch_assoc($sqlResult)){ array_push($response,$row); } echo json_encode($response); mysqli_close($conn); }else{ echo "Host Connection Error"; }?>

它工作得很好。 . .

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM