如何将 HTTP 可调用云函数响应转换为 POJO


 mFunctions
            .getHttpsCallable("addMessage")
            .call(`data`)
            .continueWith(new Continuation<HttpsCallableResult, Data>() {
                @Override
                public Data then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                    // This continuation runs on either success or `failure`, but if the task
                    // has failed then getResult() will throw an Exception which will be
                    // propagated down.
                    Data result = (Data) task.getResult().getData();
                    return result;
                }
            }).addOnCompleteListener(new OnCompleteListener<Data>() {
            @Override
            public void onComplete(@NonNull Task<Data> task) {
                if (!task.isSuccessful()) {
                    Exception e = task.getException();
                    if (e instanceof FirebaseFunctionsException) {
                        FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
                        FirebaseFunctionsException.Code code = ffe.getCode();
                        Object details = ffe.getDetails();
                    }
                    // ...
                }
                // ...
            }
        });

当我将响应对象task.getResult().getData()转换为数据对象时,它会给出java.lang.ClassCastException。我已经检查了 Data 类中的所有字段,并且在返回 json 对象的字段时它们是相似的。 我也尝试从json转换它,如下所示

  Data result =new Gson.jsonFrom(task.getResult().getData().toString(),Data.class);

它给出了错误 com.google.gson.JsonSyntaxException: com.google.gson.stream:MalfromedJsonException:Unterminated Object 在第 1 行第 74 列路径 $。CALLBACK_URL

注意CALLBACK_URL是 json 对象中的一个字段,由云函数返回,CALLBACK_URL包含 URL

更新

数据类

import com.google.gson.annotations.SerializedName;
public class Data{
    @SerializedName("MID")
    private  String MID;
    @SerializedName("ORDER_ID")
    private String ORDER_ID;
    @SerializedName("CHANNEL_ID")
    private String CHANNEL_ID;
    @SerializedName("CUST_ID")
    private String CUST_ID;
    @SerializedName("MOBILE_NO")
    private String MOBILE_NO;
    @SerializedName("EMAIL")
    private String EMAIL;
    @SerializedName("TXN_AMOUNT")
    private String TXN_AMOUNT;
    @SerializedName("WEBSITE")
    private String WEBSITE;
    @SerializedName("INDUSTRY_TYPE_ID")
    private String INDUSTRY_TYPE_ID;
    @SerializedName("SHOP_ID")
    private String SHOP_ID;
    @SerializedName("CHECKSUMHASH")
    private String CHECKSUMHASH;


    public Data(){}
    public Data(String MID, String ORDER_ID, String CHANNEL_ID, String CUST_ID, String MOBILE_NO, String EMAIL, String TXN_AMOUNT, String WEBSITE, String INDUSTRY_TYPE_ID, String SHOP_ID, String CHECKSUMHASH) {
        this.MID = MID;
        this.ORDER_ID = ORDER_ID;
        this.CHANNEL_ID = CHANNEL_ID;
        this.CUST_ID = CUST_ID;
        this.MOBILE_NO = MOBILE_NO;
        this.EMAIL = EMAIL;
        this.TXN_AMOUNT = TXN_AMOUNT;
        this.WEBSITE = WEBSITE;
        this.INDUSTRY_TYPE_ID = INDUSTRY_TYPE_ID;
       // this.CALLBACK_URL = CALLBACK_URL;
        this.SHOP_ID = SHOP_ID;
        this.CHECKSUMHASH = CHECKSUMHASH;
    }
    public String getMID() {
        return MID;
    }
    public void setMID(String MID) {
        this.MID = MID;
    }
    public String getORDER_ID() {
        return ORDER_ID;
    }
    public void setORDER_ID(String ORDER_ID) {
        this.ORDER_ID = ORDER_ID;
    }
    public String getCHANNEL_ID() {
        return CHANNEL_ID;
    }
    public void setCHANNEL_ID(String CHANNEL_ID) {
        this.CHANNEL_ID = CHANNEL_ID;
    }
    public String getCUST_ID() {
        return CUST_ID;
    }
    public void setCUST_ID(String CUST_ID) {
        this.CUST_ID = CUST_ID;
    }
    public String getMOBILE_NO() {
        return MOBILE_NO;
    }
    public void setMOBILE_NO(String MOBILE_NO) {
        this.MOBILE_NO = MOBILE_NO;
    }
    public String getEMAIL() {
        return EMAIL;
    }
    public void setEMAIL(String EMAIL) {
        this.EMAIL = EMAIL;
    }
    public String getTXN_AMOUNT() {
        return TXN_AMOUNT;
    }
    public void setTXN_AMOUNT(String TXN_AMOUNT) {
        this.TXN_AMOUNT = TXN_AMOUNT;
    }
    public String getWEBSITE() {
        return WEBSITE;
    }
    public void setWEBSITE(String WEBSITE) {
        this.WEBSITE = WEBSITE;
    }
    public String getINDUSTRY_TYPE_ID() {
        return INDUSTRY_TYPE_ID;
    }
    public void setINDUSTRY_TYPE_ID(String INDUSTRY_TYPE_ID) {
        this.INDUSTRY_TYPE_ID = INDUSTRY_TYPE_ID;
    }
//   public String getCALLBACK_URL() {
//        return CALLBACK_URL;
//    }
//
//    public void setCALLBACK_URL(String CALLBACK_URL) {
//        this.CALLBACK_URL = CALLBACK_URL;
//    }
    public String getSHOP_ID() {
        return SHOP_ID;
    }
    public void setSHOP_ID(String SHOP_ID) {
        this.SHOP_ID = SHOP_ID;
    }
    public String getCHECKSUMHASH() {
        return CHECKSUMHASH;
    }
    public void setCHECKSUMHASH(String CHECKSUMHASH) {
        this.CHECKSUMHASH = CHECKSUMHASH;
    }
}

和日志消息

Log.d("kkkkkkkkkkkk",task.getResult().getData().toString());
指纹

kkkkkkkkkkkkkkkkkk: {EMAIL=ak1542031@gmail.com, MID=xyzSta77011978730175, TXN_AMOUNT=150, ORDER_ID=0PFcGDgAZEgsVglWq9ms, WEBSITE=WEBSTAGING, INDUSTRY_TYPE_ID=Retail, MOBILE_NO=99821144556, SHOP_ID=VxHEt4fSvoQqIVyZ9x1mcNbWoxU2, CHECKSUMHASH=xl/gl/vSTEbL1UrDZB8Pv46HZZUppGnjqFPU6b/gqA/4oHl9oLBawZgqWd9CWiyspIT3j/jwJdlR7aOa6uXiZ2BqeGKDIUNQ9t6CKH6fQ84=, CHANNEL_ID=WAP, CUST_ID=VxHEt4fSvoQqIVyZ9x1mcNbWoxU2}

在云函数##(所有字段均为字符串)

 return {
                MID: paytm_config.MID,
                ORDER_ID: sOrderId,
                CHANNEL_ID: paytm_config.CHANNEL_ID,
                CUST_ID: context.auth.uid,
                MOBILE_NO: data.uPhNumber,
                EMAIL: user.email,
                TXN_AMOUNT: serviceTMS.price + '',
                WEBSITE: paytm_config.WEBSITE,
                INDUSTRY_TYPE_ID: paytm_config.INDUSTRY_TYPE_ID,
                // CALLBACK_URL:'https://securegw-stage.paytm.in/theia/paytmCallback?ORDER_ID='+sOrderId,
                SHOP_ID: data.shopId,
                CHECKSUMHASH: checkSumT,
            };

可调用的云函数以字符串形式返回 JSON 数据。它们不执行 POJO 的编码/解码,因此您必须在客户端代码中自行映射字段。

我实际上认为从JSON自动解码POJO将是一个很好的功能,因为Firebase实时数据库和Cloud Firestore已经这样做了。我建议您为此提交功能请求。

相关内容

  • 没有找到相关文章

最新更新