Я получаю ошибку java.lang.NumberFormatException: Invalid int: ""
в своем коде:
//breaking up the response into respective parts
//so we can get values for 'random reviews' string
try {
JSONArray responseObject = new JSONArray(response);
//use StringBuilder, so we can append values
StringBuilder private_review_ids = new StringBuilder();
for (int i = 0; i < responseObject.length(); i++) {
JSONObject obj = responseObject.getJSONObject(i);
//get the "private_review_ids" part of the response, append
//each value into a string private_review_ids
private_review_ids.append(obj.getString("private_review_ids").toString());
System.out.println("private_review_ids: " + private_review_ids);
//check that private_review_ids is not 0
if (private_review_ids.length() != 0){
//make it all into a single string
String private_review_ids2 = private_review_ids.toString();
//we only want the numbers (the review_ids) so get rid of other stuff
private_review_ids2 = private_review_ids2.replaceAll("[^0-9]+", ",");
//get rid of the first ","
private_review_ids2 = private_review_ids2.replaceFirst(",", "");
System.out.println("private_review_ids2:" + private_review_ids2);
//a string array, separated by commas
//because we were getting a stray comma at the start
String[] just_numbers = private_review_ids2.split(",");
//We want 3 random numbers from the array just_numbers
HashSet<Integer> integers = new HashSet<>(3);
Random random = new Random();
if (integers != null) {
//get 3 random numbers
while (integers.size() < 3) {
integers.add(Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]));
}
//UNDO THIS System.out.println("1. the integers are " + integers);
System.out.println("1. the integers are " + integers);
//make a string from integers
random_reviews = integers.toString();
//remove the [ and ] from the string
random_reviews = random_reviews.substring(1, random_reviews.length() - 1);
//UNDO THIS System.out.println("2. random_reviews : " + random_reviews);
System.out.println("2. random_reviews : " + random_reviews);
Toast.makeText(PopulistoListView.this, "random_reviews : " + random_reviews, Toast.LENGTH_LONG).show();
}}
}catch (JSONException e) {
Log.e("MYAPP", "unexpected JSON exception", e);
// Do something to recover ... or kill the app.
}
Я посмотрел ответы здесь на stackoverflow.Насколько я могу судить, я прислушался к совету - используя оператор try catch
и завернув его в оператор if
, но я все еще получаю сообщение об ошибке
integers.add(Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]));
Любые предложения о том, какрешить это пожалуйста?