looking for some solutions? You are welcome.

SOLVED: How to return value after calling web service in Util Class using Volley?

Vishal Vaishnav:

I want to return string value after getting response from web service in Volley and call value in activity. Below is my code;

Utils.java

    public static String getitemCountPrice(String cartId) {

    try {
        if (Utils.isNetworkAvailable(mContext)) {
            HashMap<String, String> params = new HashMap<>();
            params.put(CONSTANTS.API_param_cartid, cartId);
            params.put(CONSTANTS.API_param_token, Utils.getToken());
            JSONObject postdata = new JSONObject(params);

            try {
                YupITApplication.getJsonWithHTTPPostResponse(params, mContext, 1, (id, jsonResult) -> {
                    if (jsonResult.getString(mContext.getString(R.string.status)).equalsIgnoreCase(mContext.getString(R.string.success))) {
                        itemCountPrice = jsonResult.getJSONObject("Data").getString("Count") + ","
                                + jsonResult.getJSONObject("Data").getString("TotalPrice");
                        Log.e("itemCountPrice.............", "" + itemCountPrice);
                        // Here I get value
                    } else {
                        itemCountPrice = "0,0";
                    }
                }, Utils.cartitemcount, postdata);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(mContext, mContext.getString(R.string.no_server_found), Toast.LENGTH_SHORT).show();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return itemCountPrice;
    // Here I get null and this is called before web service call
}

MainActivity.java

Utils.getitemCountPrice(cart_id));

Every time I get null from above method



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent