When i download data on the web, sometimes it works and sometime it doesn’t.
And my problem is in one int :
runtime: “”
runtime is an int and when i use gson on it can cause this problem :
01-07 21:22:57.602: E/AndroidRuntime(2726): Caused by: java.lang.NumberFormatException: Invalid int: ""
and i tried to some if statement but it doesn’t work.
public int getRuntime() {
if(Integer.valueOf(runtime)==null){
return 0;
}else{
return runtime;
}
}
or even
public int getRuntime() {
if(Integer.valueOf(runtime).equals(null)){
return 0;
}else{
return runtime;
}
}
but nothing works.
Integer.valueOf() expects a String representing an integer. Calling it with an empty string will lead to an exception. You need to test the String before parsing it as an integer:
or, if you always want that runtime is 0 if the string is not a valid integer, then catch the exception:
Now, it it’s gson that parses the string for you, and this string is not always an integer, then the
runtimefield should not be an int, but a String. And you should parse it yourself, as shown above.Given your question, before trying to do anything with gson and android, you should learn the basics of the Java language. You don’t seem to understand the type system in Java and what exceptions are. Read http://docs.oracle.com/javase/tutorial/java/nutsandbolts/