2012-10-10 34 views
2

我解析JSON我从服务器Json的下载,但不知道它的规模和结构

public JSONObject getJSONFromUrl() { 

    try { 

     DefaultHttpClient httpClient = getNewHttpClient(); 

     String auth = android.util.Base64.encodeToString(
       ("[email protected]:11111") 
         .getBytes("UTF-8"), android.util.Base64.NO_WRAP); 

     HttpGet get = new HttpGet(
       "https://test2.estafeta.org/mobileestafeta/MobileSurveyReportsService.svc/LoadTabletSurveyTasks?startRowVersion=AAAAAAAAAAA=&count=500"); 

     get.addHeader("Authorization", "Basic " + auth); 

     HttpResponse httpResponse = httpClient.execute(get); 
     //httpResponse.getStatusLine().getStatusCode() 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent();   

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 

     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1")); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 


    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 


    return jObj; 

} 

我从服务器Json的下载,但不知道它的规模和结构

1)告诉我如何查看从服务器接收到的JSON的大小和结构?

2)如何查看jObj的内容?

3)会发生什么,为什么这么多行?

enter image description here

+1

为什么不简单地在浏览器中输入网址?请注意,当我测试服务器似乎非常不适。 –

+0

如果您点击链接 - 不显示任何内容,但您可以请求 –

+0

使用Gson。它是一个来自谷歌的库,可以有效地使用JSON。 –

回答

0

的ADT附带一个名为DDMS一个极好的工具。在Eclipse IDE的右上角,您可以找到perspectives。选择DDMS视角。

接下来,您可以启用您的项目分析。点击你的项目并按绿色的错误(调试)按钮。然后您可以使用DDMS选项卡转至Allocation

在分配中,您可以找到每个指针,以及它指的是什么。所以你可以找到你的jObj变量,并查看它指向的内容和其中的每个对象。

了解如何使用DDMS with tutorials like these。他们会改善你的生活。

相关问题