Hi, I have the following problem only on certain occasions. The app brings information from an external url through a Volley request taking the data from a php file with json data. Out of 900 devices I had a problem on 17, it's little but there is an error. Mostly the errors come from Galaxy devices.
I see the blockage from playConsole:
java.lang.NumberFormatException:
at java.lang.Integer.parseInt (Integer.java:620)
at java.lang.Integer.parseInt (Integer.java:643)
**at com.miapp.yamil.miapp.jugar.onResponse (jugar.java:319)
at com.miapp.yamil.miapp.jugar.onResponse (jugar.java:42)**
at com.android.volley.toolbox.JsonRequest.deliverResponse (JsonRequest.java:83)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run (ExecutorDelivery.java:106)
at android.os.Handler.handleCallback (Handler.java:789)
at android.os.Handler.dispatchMessage (Handler.java:98)
at android.os.Looper.loop (Looper.java:251)
at android.app.ActivityThread.main (ActivityThread.java:6589)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:240)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:767)
In these lines I have a conversion from String to Int
*numero.setText(relleneo.getNumero());
numeros=Integer.parseInt(numero.getText().toString());*
number comes from the volley query and is filled
*relleneo.setNumero(jsonObject.optString("CodPreg"));*
I can't detect what the problem is since it happens only from time to time.
Not: I add, users told me that when this happens the app stops and closes itself.
The problem is that the value you're trying to convert is sometimes not a numeric value, since the json sometimes doesn't have that value.
Remember the use of optString(String name) , if the value you are trying to get does not exist this method returns an empty string (
""
), this value is obviously not numeric.You can use this other version of optString() method
public String optString (String name, String fallback)
and use it in this way defining a default value in case of not finding a value, for example "0", this value would be converted to an integer without problem:
Another option is to validate if the value is numeric, if it is not numeric define a default value that is numeric, using the following method:
you would use the method like this: