【问题标题】:How can I use the retrofit @Query annotation for filtration in android如何在android中使用改造@Query注解进行过滤
【发布时间】:2022-01-04 12:43:45
【问题描述】:

我在下面有一个 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 文件时,它可以正常工作,如下所示 Execution Result Of 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)

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

【问题讨论】:

    标签: java php android


    【解决方案1】:

    修改界面

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

    而且效果很好。 . .

    【讨论】:

      猜你喜欢
      • 2021-07-15
      • 1970-01-01
      • 2020-12-13
      • 2017-10-03
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多