2012-09-04 94 views
0

我每天都在學習更多關於android開發和json代碼的知識。 但現在我被困在這個; 我可以從我的在線數據庫中獲取我的值並顯示它,但是我看到了整個json代碼。 我希望看到我希望它展示的部分。android:創建json對象或數組?

這是我的代碼,我認爲這是很基本的,但我也學習:)

正如你可以看到我正準備從網頁的價值,並把它在我的TextView,但我會喜歡把它放在一個JSONObject或JSONArray中,不知道女巫更好。

有人可以幫我一下嗎?

隨着親切的問候

public class Bordje extends Activity{ 

public void onCreate (Bundle savedInstanceState) { 
    try 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.bordje); 

     //This is out textview element, obtained by id from XML Layout 
     TextView myListView = (TextView)findViewById(R.id.netResult2); 

     //Lets connect to the internet 
     try { 
      String result = ""; 
      //create new client object 
      HttpClient httpclient = new DefaultHttpClient(); 
      //now post to the url 
      HttpPost httppost = new HttpPost("http://www.wvvzondag2.nl/android/leesbordje.php"); 
      //execute url 
      HttpResponse response = httpclient.execute(httppost); 
      //get message from the response 
      HttpEntity entity = response.getEntity(); 
      //get the content from message 
      InputStream webs = entity.getContent(); 

      //convert response to string 
      try{ 
       BufferedReader reader = new BufferedReader(new InputStreamReader(webs, "iso-8859-1"),8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
       } 
       //slow our inputstream 
       webs.close(); 
       //puts the resut into a string 
       result=sb.toString(); 
      }catch(Exception e){ 
       Log.e("log_tag", "Error converting result "+e.toString()); 
      } 

      //Parsing the JSON Data 
      try{ 
       JSONArray jArray = new JSONArray(result); 
       for(int i=0;i<jArray.length();i++){ 
         JSONObject json_data = jArray.getJSONObject(i); 
         json_data.getString("introtext"); 
         //Get an output to the screen 
         //then here should be some code that displays text? 
         //myListView.setText(Html.fromHtml(json_data)); ? 
       } 

      }catch(JSONException e){ 
        Log.e("log_tag", "Error parsing data "+e.toString()); 
      } 

     }catch(Exception e){ 
      Log.e("log_tag", "Error in http connection"+e.toString()); 
     } 

    } 
    catch (Exception e) 
    { 
     //this is the line of code that sends a real error message to the log 
     Log.e("ERROR", "ERROR IN CODE: " + e.toString()); 
    } 
} 
+0

因此,去你的網站給你的整個數據集,你想從那裏解析它?你打算使用任何你要解析的數據嗎?如果沒有,你正在運行成不需要如果數據不應該被曝光(可能的安全風險)2)大量的帶寬需求(Web服務器正在推動更多的數據1)暴露數據的問題,並且您的用戶需要使用更多數據才能使用它)。 http://federmanscripts.com/2010/12/12/php-routing/可能是你感興趣的(不是PHP用戶,所以不確定質量,但它至少包含路由)。 – Robert

回答

2

這不是一個真正的問題,其中一個是更好的。 JSON對象和JSON數組是兩個不同的東西。

JSON數組是一個有序的(like)項(http://www.json.org/javadoc/org/json/JSONArray.html)。

JSONArray jsonArray = new JSONArray("[JSON TEXT]"); 
String textToDisplay = jsonArray.getString(index); //return String at index 

JSON對象是一個映射(http://www.json.org/javadoc/org/json/JSONObject.html)。

JSONObject jsonObj = new JSONObject("[JSON TEXT]"); 
String textToDisplay = jsonObj.getString("key"); //returns String value 

然後,在獲得數據後,像前面一樣將其設置在文本視圖中。

myListView.setText(textToDisplay); 
+0

首先感謝您的快速回復,我在其中添加了一些代碼。 但我需要放更多的東西,我猜想顯示的數據。 –

+1

在您的循環中,您可以調用 json_data.getString(「introtext」); 而不將返回的字符串分配給變量。看到我更新的回覆。如果您需要爲數組中的每個項目獲取此值,則需要連接循環中的文本並調用setText(fullString)。那有意義嗎? – loeschg

+0

是的,這很有道理:) 謝謝你的幫助它現在正在工作:D –

0

如果從服務器獲取有效的JSON,你可以根據它的對象,你越來越所以沒有說法哪一個更好的問題,純粹做一個JSONArray或其JSONObject。但在你的情況下,它最有可能是一個JSONArray。

那麼要實現這一點,您可以使用gson將有效的json字符串轉換爲JSONObjectJSONArray

您將使用.toJson().fromJSON(object)方法。