Reputation: 24500
I parse JSON data and there is book id , it is a big number:
"Books":[{"ID":88401542,"Title":"Building and Testing with Gradle","SubTitle":"Understanding Next-Generation Builds","Description":"Build and test software written in Java and many other languages with Gradle, the open source project automation tool that's getting a lot of attention. This concise introduction provides numerous code examples to help you explore Gradle, both as a b ..."},{"ID":2676913857...}
long book_id = 0 ;
try {
book_id = jsonBook.getInt(BOOK_ID);
Log.v(LOG_TAG, "book id: " + );
it prints me some stupid negative number:
book id: -1814548740
what is the reason? can it be because I print it without casting? my log statement should be:
Log.v("book id: " + new Long (book_id)) ;
or
Log.v("book id: " + new Long (book_id.toString())) ;
or
Log.v("book id: " + new String ( new Long(book_id))) ;
Upvotes: 0
Views: 107
Reputation: 69460
Since your Book_ID is an Long you should use getLong
to get the value:
book_id = jsonBook.getLong(BOOK_ID);
Upvotes: 1
Reputation: 810
You use getInt()
to read a long
. If the long value you're trying to read is large than an int then what you get is normal.
Upvotes: 1
Reputation: 11969
I think your problem lies in that:
book_id = jsonBook.getInt(BOOK_ID);
If getInt(BOOK_ID)
returns a number that overflow the int
type, then you should not expect it to work correctly. (I guessed int
because of the method name).
You can either check for your JSON API if it has a getLong
method, either store as string, and use Long.valueOf
, Long.parseLong
and so on...
Upvotes: 1
Reputation: 62864
Probably this value exceeds the Integer.MAX_VALUE
. Better use BigInteger
:
BigInteger book_id = BigInteger(jsonBook.getText(BOOK_ID));
I'm not sure if getText()
is correct, but the idea is to fetch the value as a String
.
Upvotes: 1