【问题标题】:Calculating percentage within a try catch block在 try catch 块中计算百分比
【发布时间】:2022-11-14 11:05:03
【问题描述】:

我正在尝试计算用于更新引导进度条的产品的百分比评级,但我的问题是它仅在只有一个评级类别返回非零值并且多个评级类别返回无时才有效零值。

到目前为止,这是我尝试过的:

StringRequest productRateStringRequest = new StringRequest(Request.Method.POST, NetworkConstants.URL_PRODUCT_RATINGS,
        response -> {
            try{
                JSONObject ratingObject = new JSONObject(response);
                oneStar = ratingObject.getInt("oneStar");
                twoStars = ratingObject.getInt("twoStar");
                threeStars = ratingObject.getInt("threeStar");
                fourStars = ratingObject.getInt("fourStar");
                fiveStars = ratingObject.getInt("fiveStar");
                totalRating = ratingObject.getInt("totalRatings");

                averageRating.setText(String.format("%s/5", ratingObject.getString("averageRating")));
                oneStarCount.setText(String.format("(%s)", oneStar));
                twoStarCount.setText(String.format("(%s)", twoStars));
                threeStarCount.setText(String.format("(%s)", threeStars));
                fourStarCount.setText(String.format("(%s)", fourStars));
                fiveStarCount.setText(String.format("(%s)", fiveStars));
                
//this is the part that is giving abnormal outputs when more than one none-zero value is returned and works well only when one none-zero value is set

                productRatingBar.setRating((float) ratingObject.getDouble("averageRating"));
                Log.d("Progress", "inflateRating: "+ fourStars / totalRating * 100);
                oneStarProgress.setProgress((oneStar / totalRating) * 100);
                twoStarProgress.setProgress((twoStars / totalRating) * 100);
                threeStarProgress.setProgress((threeStars / totalRating) * 100);
                fourStarProgress.setProgress((fourStars / totalRating) * 100);
                fiveStarProgress.setProgress((fiveStars / totalRating) * 100);
                
            }catch (JSONException e){
                e.printStackTrace();
            }
        },
        error -> {
            Log.d("Error", "Failed with error msg:\t" + error.getMessage());
            Log.d("An Error", "Error StackTrace: \t" + Arrays.toString(error.getStackTrace()));
            try {
                byte[] htmlBodyBytes = error.networkResponse.data;
                Log.e("Error", new String(htmlBodyBytes), error);
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }){
    protected Map<String, String> getParams() {
        Map<String, String> params = new HashMap<>();
        params.put("id", id);
        params.put("api_token", token);
        return params;
    }
};
productRateStringRequest.setRetryPolicy(new DefaultRetryPolicy(
        1000*5,
        3,
        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue productRequestQue = Volley.newRequestQueue(this);
productRequestQue.add(productRateStringRequest);

【问题讨论】:

  • 请更明确地说明“不起作用”的含义:什么行为你观察到你想要发生什么? Edit 你的问题包括该信息。

标签: java android


【解决方案1】:

productRatingBar.setRating((float)ratingObject.getDouble("averageRating"));

这条线是否给出错误?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-01
    • 2021-01-10
    • 2017-05-13
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多