The difference is that
optString
returns the empty string (""
) if the key you specify doesn't exist.getString
on the other hand throws aJSONException
. UsegetString
if it's an error for the data to be missing, oroptString
if you're not sure if it will be there.
简单来说就是optString会在得不到你想要的值时候返回空字符串”“,而getString会抛出异常。至此,疑惑已经解除。