Android 改装车身,提供空值,但在《邮递员》中实现完美

Android 改装车身,提供空值,但在《邮递员》中实现完美,android,retrofit,retrofit2,Android,Retrofit,Retrofit2,我正在尝试解析JSON { "CALLBACK_URL": "http://159.65.00/api/paytm/response/", "ORDER_ID": "yKaiv4", "CUST_ID": "adhish@abc.com", "TXN_AMOUNT": "1", "CHECKSUMHASH": "j7WzwAh5SFzmP4JWKx9X3mpcv6VxCenaSiWzArjdQgnblpL9YLy4Hio8tHuB2O0ZOiQ1vewsx

我正在尝试解析JSON

{
    "CALLBACK_URL": "http://159.65.00/api/paytm/response/",
    "ORDER_ID": "yKaiv4",
    "CUST_ID": "adhish@abc.com",
    "TXN_AMOUNT": "1",
    "CHECKSUMHASH": "j7WzwAh5SFzmP4JWKx9X3mpcv6VxCenaSiWzArjdQgnblpL9YLy4Hio8tHuB2O0ZOiQ1vewsxPKd52HG9rmxYQ=",
    "WEBSITE": "WEB_STAGING",
    "CHANNEL_ID": "WEB",
    "INDUSTRY_TYPE_ID": "Retail",
    "MID": "DIY123555501617"
}
上面的JSON是我在POSTMAN中得到的,但是当我试图在我的Android Java代码中使用以下POJO类解析JSON时,我的JSONbody元素是空的。POJO类是:

import com.google.gson.annotations.SerializedName;

public class PaytmResponse {

    @SerializedName("MID")
    String mid;

    @SerializedName("INDUSTRY_TYPE_ID")
    String industryTypeId;

    @SerializedName("CHANNEL_ID")
    String channelId;

    @SerializedName("ORDER_ID")
    String orderId;

    @SerializedName("CALLBACK_URL")
    String callbackUrl;

    @SerializedName("TXN_AMOUNT")
    String txnAmount;

    @SerializedName("CUST_ID")
    String custId;

    @SerializedName("CHECKSUMHASH")
    String checksumHash;

    @SerializedName("WEBSITE")
    String webSite;

    public String getMid() {
        return mid;
    }

    public String getIndustryTypeId() {
        return industryTypeId;
    }

    public String getChannelId() {
        return channelId;
    }

    public String getOrderId() {
        return orderId;
    }

    public String getCallbackUrl() {
        return callbackUrl;
    }

    public String getTxnAmount() {
        return txnAmount;
    }

    public String getCustId() {
        return custId;
    }

    public String getChecksumHash() {
        return checksumHash;
    }

    public String getWebSite() {
        return webSite;
    }
}
这就是我在Android代码中使用函数实现的方法:

private void getChecksumFromServer(String movieName, String amount) {

    RetrofitClient retrofitClient = new RetrofitClient(getActivity());
    retrofitInterface = retrofitClient.getClient().create(RetrofitInterface.class);

    Call<PaytmResponse> call = retrofitInterface.generateChecksumHash(movieName,amount,"application/json","Token "+sharedPreferencesHelper.getString(Constants.TOKEN,""));
    call.enqueue(new Callback<PaytmResponse>() {
        @Override
        public void onResponse(Call<PaytmResponse> call, Response<PaytmResponse> response) {


            Logger.d("CODE",response.code()+"");
            Logger.d("MESSAGE",response.message()+"");
            Logger.d("RESPONSE",""+new Gson().toJson(response));
            Logger.d("URL",""+response.raw().request().url());

            if (response.isSuccessful()) {


                String MID = response.body().getMid();

                String INDUSTRY_TYPE_ID = response.body().getIndustryTypeId();

                String CHANNEL_ID = response.body().getChannelId();

                String ORDER_ID = response.body().getOrderId();

                String CALLBACK_URL = response.body().getCallbackUrl();

                String TXN_AMOUNT = response.body().getTxnAmount();

                String CUST_ID = response.body().getCustId();

                String CHECKSUMHASH = response.body().getChecksumHash();

                String WEBSITE = response.body().getWebSite();


                processPayment(MID, INDUSTRY_TYPE_ID, CHANNEL_ID, ORDER_ID, CALLBACK_URL, TXN_AMOUNT, CUST_ID, CHECKSUMHASH, WEBSITE);

            } else {

                ToastHelper.showToast(getActivity(),response.message());
            }
        }

        @Override
        public void onFailure(Call<PaytmResponse> call, Throwable t) {

            Logger.e(TAG, "FAILED : " + t.toString());
        }
    });
}
我在哪里犯了错误?请注意,代码是200 success,其正文如POSTMAN中的JSON响应所示。

响应的.body只能读取一次。您应该将其存储在一个变量中,并多次读取该变量

更好的方法是:不要手动解析响应。相反,使用改型的gson或其他json库解析器。

正如@Tim Castelijns所说,.body只能读取一次。 你必须这样做

PaytmResponse response = response.body();
processPayment(response.getMid(), paymentResponse.getIndustryTypeId(), paymentResponse.getChannelId(), paymentResponse.getOrderId(), paymentResponse.getCallbackUrl(), paymentResponse.getTxnAmount(), paymentResponse.getCustId(), paymentResponse.getChecksumHash(), paymentResponse.getWebSite());

所以还有别的事情,当我用截击尝试同样的请求时,我发现了这一点。截图显示了带有正确键的实际JSON响应,这些键在我的POJO类中丢失,因此这里的值为空。

调试应用程序。Add break point Call=RefughtInterface.GenerateCheckSumHash您尝试过调试吗?从response.body中赋值后,响应是什么?它只给我{}
PaytmResponse response = response.body();
processPayment(response.getMid(), paymentResponse.getIndustryTypeId(), paymentResponse.getChannelId(), paymentResponse.getOrderId(), paymentResponse.getCallbackUrl(), paymentResponse.getTxnAmount(), paymentResponse.getCustId(), paymentResponse.getChecksumHash(), paymentResponse.getWebSite());