从服务器解析一些 json 数据时,我遇到了一个严重的问题。当它服务器返回一些 json 数据时,我的程序可以很好地处理它。但是当服务器返回 null 时,我的应用程序给了我异常。
我搜索了很多问题和答案。最多的答案是使用 isNull() 方法来处理 JSON 的 null 返回类型。但在编写 isNull 方法之前,我们必须编写
JSONObject json = new JSONObject("returned string");
当有用于解析的数据时,这句话工作得很好,但是当字符串为空时它给了我异常。
我认为我的问题可能会让人难以理解。所以我在这里给出一些我的代码。
URL Url = new URL(URL + "?userId=3");
HttpURLConnection urlConnection = (HttpURLConnection) Url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// gets the server json data
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));
String next = "";
while ((next = bufferedReader.readLine()) != null)
{
StringTokenizer tokens = new StringTokenizer(next);
while(tokens.hasMoreTokens())
{
Log.d("Tokens", tokens.nextToken());
}
Log.d("next value", next);
JSONObject json = new JSONObject(next);
if (json.isNull("Location")) {
Log.d("Json is null", "Ok");
serverLatitude = String.valueOf(lati);
serverLongitude = String.valueOf(longi);
}
else {
Log.d("Json not null", "Ok");
lat = json.getString("Latitude");
lon = json.getString("Longitude");
if (lat != null && lon != null) {
serverLatitude = lat;
serverLongitude = lon;
} else {
}
}
当服务器中有数据时它工作正常。但是当服务器返回 null 它给我错误。所以任何人都可以帮助我如何处理这个问题
编辑
这是服务器返回 null 时的 logcat。
08-26 14:53:22.996: D/Tokens(31397): null
08-26 14:53:22.996: D/next value(31397): null
08-26 14:53:22.996: D/Here lat(31397): 12.90871405
08-26 14:53:22.996: W/System.err(31397): org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONObject
08-26 14:53:22.996: W/System.err(31397): at org.json.JSON.typeMismatch(JSON.java:107)
08-26 14:53:22.996: W/System.err(31397): at org.json.JSONObject.<init> (JSONObject.java:158)
08-26 14:53:22.996: W/System.err(31397): at org.json.JSONObject.<init>(JSONObject.java:171)
08-26 14:53:22.996: W/System.err(31397): at com.monday.trackloc.NewService$FetchCordinates.fetchLastInsertedRow(NewService.java:264)
08-26 14:53:22.996: W/System.err(31397): at com.monday.trackloc.NewService$FetchCordinates.insertDataToServer(NewService.java:126)
08-26 14:53:22.996: W/System.err(31397): at com.monday.trackloc.NewService$FetchCordinates.onPostExecute(NewService.java:108)
08-26 14:53:22.996: W/System.err(31397): at com.monday.trackloc.NewService$FetchCordinates.onPostExecute(NewService.java:1)
08-26 14:53:22.996: W/System.err(31397): at android.os.AsyncTask.finish(AsyncTask.java:417)
08-26 14:53:22.996: W/System.err(31397): at android.os.AsyncTask.access$300(AsyncTask.java:127)
08-26 14:53:22.996: W/System.err(31397): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
08-26 14:53:23.006: W/System.err(31397): at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 14:53:23.006: W/System.err(31397): at android.os.Looper.loop(Looper.java:130)
08-26 14:53:23.006: W/System.err(31397): at android.app.ActivityThread.main(ActivityThread.java:3806)
08-26 14:53:23.006: W/System.err(31397): at java.lang.reflect.Method.invokeNative(Native Method)
08-26 14:53:23.006: W/System.err(31397): at java.lang.reflect.Method.invoke(Method.java:507)
08-26 14:53:23.006: W/System.err(31397): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-26 14:53:23.006: W/System.err(31397): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-26 14:53:23.006: W/System.err(31397): at dalvik.system.NativeStart.main(Native Method)
08-26 14:53:23.006: D/AndroidRuntime(31397): Shutting down VM
08-26 14:53:23.006: W/dalvikvm(31397): threadid=1: thread exiting with uncaught exception (group=0x40018560)
08-26 14:53:23.006: E/AndroidRuntime(31397): FATAL EXCEPTION: main
08-26 14:53:23.006: E/AndroidRuntime(31397): java.lang.NumberFormatException:
08-26 14:53:23.006: E/AndroidRuntime(31397): at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)
08-26 14:53:23.006: E/AndroidRuntime(31397): at java.lang.Double.parseDouble(Double.java:318)
08-26 14:53:23.006: E/AndroidRuntime(31397): at com.monday.trackloc.NewService$FetchCordinates.insertDataToServer(NewService.java:130)
08-26 14:53:23.006: E/AndroidRuntime(31397): at com.monday.trackloc.NewService$FetchCordinates.onPostExecute(NewService.java:108)
08-26 14:53:23.006: E/AndroidRuntime(31397): at com.monday.trackloc.NewService$FetchCordinates.onPostExecute(NewService.java:1)
08-26 14:53:23.006: E/AndroidRuntime(31397): at android.os.AsyncTask.finish(AsyncTask.java:417)
08-26 14:53:23.006: E/AndroidRuntime(31397): at android.os.AsyncTask.access$300(AsyncTask.java:127)
08-26 14:53:23.006: E/AndroidRuntime(31397): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
08-26 14:53:23.006: E/AndroidRuntime(31397): at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 14:53:23.006: E/AndroidRuntime(31397): at android.os.Looper.loop(Looper.java:130)
08-26 14:53:23.006: E/AndroidRuntime(31397): at android.app.ActivityThread.main(ActivityThread.java:3806)
08-26 14:53:23.006: E/AndroidRuntime(31397): at java.lang.reflect.Method.invokeNative(Native Method)
08-26 14:53:23.006: E/AndroidRuntime(31397): at java.lang.reflect.Method.invoke(Method.java:507)
08-26 14:53:23.006: E/AndroidRuntime(31397): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-26 14:53:23.006: E/AndroidRuntime(31397): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-26 14:53:23.006: E/AndroidRuntime(31397): at dalvik.system.NativeStart.main(Native Method)
谢谢你