在JSON解析中处理isNull()方法的方法

时间:2013-08-26 09:10:14

标签: android json wcf

从服务器解析一些json数据时,我遇到了严重的问题。当服务器返回一些json数据时,我的程序处理得很好。但是当服务器返回null时,我的应用程序给了我例外。

我搜索了许多问题和答案。最大的答案是使用isNull()方法来处理JSON的null返回类型。但在编写isNull方法之前,我们必须编写

  JSONObject json = new JSONObject("returned string");

当有数据用于解析时,这句话工作正常,但是当字符串为空时,它给出了Exception。

我认为我的问题可能会让人感到困惑。所以我在这里给出了一些代码。

    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)

谢谢

1 个答案:

答案 0 :(得分:0)

我用不同的方式解决了它。但它工作正常。

我想我必须首先专注于弦乐。

解决方案是这样的:

while ((next = bufferedReader.readLine()) != null) {

  Log.d("next value", next);
  Log.d("Here lat", String.valueOf(lati));
  if (!next.contentEquals("null")) {
  JSONObject json = new JSONObject(next);
  Log.d("Json not null", "Ok");
  serverLatitude = json.getString("Latitude");
  serverLongitude = json.getString("Longitude");

      } else {
    emptyRow = true;
    Log.d("Json is null", "Ok");
    serverLatitude = String.valueOf(lati);
    serverLongitude = String.valueOf(longi);
    }
}

我看到实际上返回的字符串包含“null”。所以我只是把“null”放在if条件下,现在它完美无缺。

我知道这不是解决这个问题的最佳方法。如果有任何人有解决这个问题的好方法,请在这里提供。谢谢