I have this error, when I want to scan product with my app… [139] NetworkUtility.shouldRetryException: Unexpected response code 502 for https://world.openfoodfacts.net/api/v2/product/4000415851705?fields=product_name,quantity,nutriments,nutriscore_data.
This is my code…
if (result != null) {
if (result.getContents() != null) {
String barcode = result.getContents();
url = “https://world.openfoodfacts.net/api/v2/product/” + barcode + “?fields=product_name,quantity,nutriments,nutriscore_data”;
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, response -> {
try {
JSONObject productObject = response.getJSONObject("product");
productName = productObject.getString("product_name");
productQuantity = productObject.optString("quantity", "Unknown");
if (productObject.has("nutriments")) {
JSONObject nutriments = productObject.getJSONObject("nutriments");
carbohydrates100g = nutriments.optDouble("carbohydrates_100g", 0.0);
carbohydratesUnit = nutriments.optString("carbohydrates_unit", "");
carbohydratesValue = nutriments.optDouble("carbohydrates_value", 0.0);
energy = nutriments.optDouble("energy", 0.0);
energyKcal = nutriments.optDouble("energy-kcal", 0.0);
energyKcal100g = nutriments.optDouble("energy-kcal_100g", 0.0);
energyKcalUnit = nutriments.optString("energy-kcal_unit", "");
sugars = nutriments.optDouble("sugars", 0.0);
sugars100g = nutriments.optDouble("sugars_100g", 0.0);
sugarsUnit = nutriments.optString("sugars_unit", "");
sugarsValue = nutriments.optDouble("sugars_value", 0.0);
nutrimentsInfo = String.format("<br/>Carbohydrates:<font color='#808080'> %.2f %s (%.2f g/100g)</font><br/>Energy:<font color='#808080'> %.2f %s (%.2f kcal/100g)</font><br/>Sugars:<font color='#808080'> %.2f %s (%.2f g/100g)</font>",
carbohydratesValue, carbohydratesUnit, carbohydrates100g,
energy, energyKcalUnit, energyKcal100g,
sugarsValue, sugarsUnit, sugars100g);
addRowToTable(tableLayout, "Carbohydrates", String.format("%.2f %s (%.2f g/100g)", carbohydratesValue, carbohydratesUnit, carbohydrates100g));
addRowToTable(tableLayout, "Energy", String.format("%.2f %s (%.2f kcal/100g)", energy, energyKcalUnit, energyKcal100g));
addRowToTable(tableLayout, "Sugars", String.format("%.2f %s (%.2f g/100g)", sugarsValue, sugarsUnit, sugars100g));
} else {
Toast.makeText(MainActivity.this, "Nutriments data not available", Toast.LENGTH_LONG).show();
}
JSONObject nutriScoreData = productObject.getJSONObject("nutriscore_data");
energyPoints = nutriScoreData.getInt("energy_points");
energyValue = nutriScoreData.getDouble("energy_value");
sugarsPoints = nutriScoreData.getInt("sugars_points");
nutriScoreInfo = String.format("<br/>Energy points:<font color='#808080'> %d</font><br/>Energy value: <font color='#808080'> %.2f</font><br/>Sugars points: <font color='#808080'> %d</font><br/>Sugars value: <font color='#808080'> %.2f</font>", energyPoints, energyValue, sugarsPoints, sugarsValue);
addRowToTable(tableLayout, "Energy points", String.format("%d", energyPoints));
addRowToTable(tableLayout, "Energy value", String.format("%.2f", energyValue));
addRowToTable(tableLayout, "Sugar points", String.valueOf(sugarsPoints));
nutriInfo += nutriScoreInfo + " " + nutrimentsInfo;
} catch (Exception e) {
// Handle exception if needed
}
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View customTitleView = LayoutInflater.from(MainActivity.this).inflate(R.layout.costom_alert_dialog_title, null);
((TextView) customTitleView.findViewById(R.id.title_text)).setText(Html.fromHtml("<br/><b>Scanning result</b>", Html.FROM_HTML_MODE_LEGACY));
builder.setCustomTitle(customTitleView);
builder.setMessage(Html.fromHtml("\n <b>Product details <br/></b>Scanned barcode: <font color='#808080'>" + result.getContents() + "</font><br/>Product name: <font color='#808080'>" + productName + "</font><br/>Product quantity: <font color='#808080'>" + productQuantity + "</font><br/><br/><b>Expiration date</b> <br/>Choose date:<font color='#808080'> Select date</font><br/><br/><b>Nutriments info:</b>", Html.FROM_HTML_MODE_LEGACY ));
builder.setView(customView);
builder.setPositiveButton("Add", (dialog, which) -> {
if (isDateSelected) {
handleAddButtonClick();
} else {
Toast.makeText(MainActivity.this, "Please select a date before finishing", Toast.LENGTH_SHORT).show();
showDialogAgain(customView);
}
}).setNegativeButton("Scan Again", (dialog, which) -> scanCode())
.setNeutralButton("Set date", (dialog, which) -> datePickerDialog.show());
AlertDialog dialog = builder.create();
dialog.show();
}, error -> {
if (error.networkResponse != null && error.networkResponse.statusCode == 502) {
// Handle 502 Bad Gateway error
Toast.makeText(MainActivity.this, "Server error, please try again later.", Toast.LENGTH_SHORT).show();
} else {
// Handle other errors
Toast.makeText(MainActivity.this, "An error occurred, please try again.", Toast.LENGTH_SHORT).show();
}
});
Volley.newRequestQueue(this).add(request);
} else {
Toast.makeText(this, "No results", Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}