在使用改进

时间:2018-05-16 07:42:47

标签: android gson retrofit2 textview

我正在尝试解析一个简单的json(如下所述),并将值设置为textViews。我正在使用改造和GSON转换器。但是在将值设置为textviw时,它将返回null。 我已经将值解析为recyclerview,这很容易。但是在简单的回答中,我可能会犯一些小错误但却无法找到它。

帮助将不胜感激。

ItemDescriptionInterface

public interface ItemDescriptionInterface {
    @GET("getProductDetailByProductId?ProductId=3")
    Call<JsonObject> ITEM_DESCRIPTION_RESPONSE_CALL();
}

活动

private void GetItemDescription(){

    Retrofit retrofit2 = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();


    ItemDescriptionInterface apiService = retrofit2.create(ItemDescriptionInterface.class);
    Call<JsonObject> jsonCall = apiService.ITEM_DESCRIPTION_RESPONSE_CALL();
    jsonCall.enqueue(new Callback<JsonObject>() {
        @Override
        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {

            String jsonString = response.body().toString();
            Gson gson = new Gson();
            ItemDescriptionModel model = gson.fromJson(jsonString, ItemDescriptionModel.class);
            price.setText(model.getResult().getActualPrice());//Here its not getting 

        }
        @Override
        public void onFailure(Call<JsonObject> call, Throwable t) {
            String msg = (t.getMessage() == null) ? "Login failed!" : t.getMessage();
            Log.d("descriptionofproduct", msg);
        }
    });

我得到的JSON响应:

{
  "status": "Success",
  "response_code": 200,
  "result": [
    {
      "PId": "3",
      "ProductId": "3",
      "VendorId": "admin",
      "ProductName": "Golden Green",
      "ProductAlias": "golden-green-full-rim-",
      "MarketPrice": "500",
      "ActualPrice": "450",
      "PurchasePrice": "450",
      "Style": "3",
      "DefaultImage_url": "http:\/\/lensclone.tk\/test\.png"
    }
  ]
}

模型

public class ItemDescriptionModel {

@SerializedName("ActualPrice")
private String price;

@SerializedName("ProductDetails")
private String productDetails;

@SerializedName("DefaultImage_url")
private String imgurl;

@SerializedName("ProductName")
private String ProductName;

public ItemDescriptionModel(String price, String productDetails, String imgurl, String productName) {
    this.price = price;
    this.productDetails = productDetails;
    this.imgurl = imgurl;
    ProductName = productName;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

public String getProductDetails() {
    return productDetails;
}

public void setProductDetails(String productDetails) {
    this.productDetails = productDetails;
}

public String getImgurl() {
    return imgurl;
}

public void setImgurl(String imgurl) {
    this.imgurl = imgurl;
}

public String getProductName() {
    return ProductName;
}

public void setProductName(String productName) {
    ProductName = productName;
}

}

我想只显示实际价格。

如果我遗失了某些内容,请告诉我。

3 个答案:

答案 0 :(得分:0)

成功收到有效回复后,您无法正确解析json。 model对象不包含ActualPrice,必须从获取的json中进一步解析。

您需要为响应对象(ItemDescriptionModel)以及响应json中的任何嵌套对象创建模型类,例如result。您还可以使用JsontoJava等可以生成所需模型类的在线工具。

根据需要创建模型类后,

替换

price.setText(model.getPrice());

price.setText(model.getResult().getPrice());

答案 1 :(得分:0)

改变这一点。

public static async Task<IEnumerable<T>> ExecuteDataReaderAsync<T>(string sql, Func<TdDataReader, T> action)
    {
        using (var connection = new TdConnection(TDConnstring))
        {
            await connection.OpenAsync();
            using (var cmd = connection.CreateCommand())
            {
                cmd.CommandText = sql;
                using (var dr = await cmd.ExecuteReaderAsync())
                {
                   return dr.Select(r => action(r)).ToList();
                }
            }
        }
    }

public static class Extensions
{

    public static IEnumerable<T> Select<T>(
        this TdDataReader reader, Func<TdDataReader, T> action)
    {

        while (reader.Read())
        {
            yield return action(reader);
        }
    }
}

答案 2 :(得分:0)

<强> ItemDescriptionInterface

public interface ItemDescriptionInterface {
    @GET("getProductDetailByProductId?ProductId=3")
    Call<ItemDescriptionModel> ITEM_DESCRIPTION_RESPONSE_CALL();
}

<强>活动

Retrofit retrofit2 = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    ItemDescriptionInterface apiService = retrofit2.create(ItemDescriptionInterface.class);

    Call<ItemDescriptionModel> jsonCall = apiService.ITEM_DESCRIPTION_RESPONSE_CALL();
    jsonCall.enqueue(new Callback<ItemDescriptionModel>() {
        @Override
        public void onResponse(Call<ItemDescriptionModel> call, Response<ItemDescriptionModel> response) {

            ItemDescriptionModel model = (ItemDescriptionModel) response.body();
            price.setText(model.getResult().get(0).getActualPrice());
        }
    }

<强>模型

public class ItemDescriptionModel {

@SerializedName("status")
private String status;

@SerializedName("response_code")
private String response_code;

@SerializedName("result")
private List<Results> result;

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getResponse_code() {
    return response_code;
}

public void setResponse_code(String response_code) {
    this.response_code = response_code;
}

public List<Results> getResult() {
    return result;
}

public void setResult(List<Results> result) {
    this.result = result;
}

private class Results {

    @SerializedName("PId")
    private String PId;

    @SerializedName("ProductId")
    private String ProductId;

    @SerializedName("VendorId")
    private String VendorId;

    @SerializedName("ProductName")
    private String ProductName;

    @SerializedName("ProductAlias")
    private String ProductAlias;

    @SerializedName("MarketPrice")
    private String MarketPrice;

    @SerializedName("ActualPrice")
    private String ActualPrice;

    @SerializedName("PurchasePrice")
    private String PurchasePrice;

    @SerializedName("Style")
    private String Style;

    @SerializedName("DefaultImage_url")
    private String DefaultImage_url;

    public String getPId() {
        return PId;
    }

    public void setPId(String PId) {
        this.PId = PId;
    }

    public String getProductId() {
        return ProductId;
    }

    public void setProductId(String productId) {
        ProductId = productId;
    }

    public String getVendorId() {
        return VendorId;
    }

    public void setVendorId(String vendorId) {
        VendorId = vendorId;
    }

    public String getProductName() {
        return ProductName;
    }

    public void setProductName(String productName) {
        ProductName = productName;
    }

    public String getProductAlias() {
        return ProductAlias;
    }

    public void setProductAlias(String productAlias) {
        ProductAlias = productAlias;
    }

    public String getMarketPrice() {
        return MarketPrice;
    }

    public void setMarketPrice(String marketPrice) {
        MarketPrice = marketPrice;
    }

    public String getActualPrice() {
        return ActualPrice;
    }

    public void setActualPrice(String actualPrice) {
        ActualPrice = actualPrice;
    }

    public String getPurchasePrice() {
        return PurchasePrice;
    }

    public void setPurchasePrice(String purchasePrice) {
        PurchasePrice = purchasePrice;
    }

    public String getStyle() {
        return Style;
    }

    public void setStyle(String style) {
        Style = style;
    }

    public String getDefaultImage_url() {
        return DefaultImage_url;
    }

    public void setDefaultImage_url(String defaultImage_url) {
        DefaultImage_url = defaultImage_url;
    }
}}