2013-03-05 86 views
0

我有一個活動,其中的值通過JSON獲取。 for循環運行。我正在接收來自JSON的獲取對象中存在的所有值。但是,我想將所有值附加到ArrayList,即image_urls。任何人都可以幫助我嗎?如何將循環值添加到數組列表?

public class AndroidJSONParsingActivity extends ListActivity { 

    private static String url = "http://www.ttt.com/album_pro/array_to_encode"; 

    JSONArray folio = null; 
    ArrayList<String> urlList = new ArrayList<String>(); 
    public static ArrayList<String> image_urls = new ArrayList<String>(); 

    @SuppressLint("NewApi") 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
       .permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 

     JSONParser jParser = new JSONParser(); 
     try { 
      JSONObject json = jParser.getJSONFromUrl(url); 
      // Log.v("URL",json.toString()); 

      JSONObject seo = json.getJSONObject("SEO"); 
      // Log.v("seo",seo.toString()); 
      JSONArray folio = seo.getJSONArray("Folio"); 
      Log("ARRAY-FOLIO0" + folio); 

      JSONArray image_urls1 = new JSONArray(); 
      String s1 = seo.getString("Folio"); 
      for (int i = 0; i < folio.length(); i++) { 
       String m = folio.getString(i); 
       Log.v("M" + i, m); 
       image_urls.add(folio.getString(i) + ","); 
       Log("JSON-ARRAY-->" + image_urls.add(folio.getString(i))); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private void Log(String string) { 
     Log.v("SEO-TEST", string); 

    } 
} 

以下是logcat的響應我得到

03-05 13:58:25.948: V/M0(3241): http://www.tt.com/media/imagepath/portfolio_images/http_arctictern.tt_folio_portfolio_a_grade_motor_school.html_a_poftfolio_2013_01_11.png 
03-05 13:58:25.948: V/SEO-TEST(3241): JSON-ARRAY-->true 
03-05 13:58:25.948: V/M1(3241): http://www.tt.com/media/imagepath/portfolio_images/http_arcticterntt_folio_portfolio_abc_tarps.html_a_poftfolio_2013_01_10.png 
03-05 13:58:25.948: V/SEO-TEST(3241): JSON-ARRAY-->true 
03-05 13:58:25.948: V/M2(3241): http://www.tt.com/media/imagepath/portfolio_images/http_arctictern.tt_folio_portfolio_able_scale.html_a_poftfolio_2013_01_10.png 
03-05 13:58:25.948: V/SEO-TEST(3241): JSON-ARRAY-->true 
03-05 13:58:25.948: V/M3(3241): http://www.tt.com/media/imagepath/portfolio_images/http_arctictern.tt_folio_portfolio_accent_aluminum_windows.html_a_poftfolio_2013_01_10.png 
03-05 13:58:25.948: V/SEO-TEST(3241): JSON-ARRAY-->true 
03-05 13:58:25.948: V/M4(3241): http://www.tt.com/media/imagepath/portfolio_images/http_arctictern.tt_folio_portfolio_access_health.html_a_poftfolio_2013_01_10.png 
03-05 13:58:25.948: V/SEO-TEST(3241): JSON-ARRAY-->true 
03-05 13:58:25.948: V/M5(3241): http://www.tt.com/media/imagepath/portfolio_images/http_arctictern.tt_folio_portfolio_acmeda__zipscreen.html_a_poftfolio_2013_01_11.png 
03-05 13:58:25.948: V/SEO-TEST(3241): JSON-ARRAY-->true 
03-05 13:58:25.948: V/M6(3241): http://www.tt.com/media/imagepath/portfolio_images/http_arctictern.tt_folio_portfolio_aditya_birla_minerals.html_a_poftfolio_2013_01_10.png 
03-05 13:58:25.948: V/SEO-TEST(3241): JSON-ARRAY-->true 
03-05 13:58:25.948: V/M7(3241): http://www.tt.com/media/imagepath/portfolio_images/http_arctictern.tt_folio_portfolio_advanced_corporate_badges.html_a_poftfolio_2013_01_10.png 
03-05 13:58:25.948: V/SEO-TEST(3241): JSON-ARRAY-->true 
03-05 13:58:25.948: V/M8(3241): http://www.tt.com/media/imagepath/portfolio_images/http_arctictern.tt_folio_portfolio_aerotech_fans.html_a_poftfolio_2013_01_10.png 
03-05 13:58:25.948: V/SEO-TEST(3241): JSON-ARRAY-->true 
03-05 13:58:25.948: V/M9(3241): http://www.tt.com/media/imagepath/portfolio_images/http_arctictern.tt_folio_portfolio_agriculture_victoria_services_.html_a_poftfolio_2013_01_10.png 
03-05 13:58:25.948: V/SEO-TEST(3241): JSON-ARRAY-->true 
+1

使用StringBuilder並追加每個元素,然後使用toString()函數獲取完整字符串。 – 2013-03-05 08:24:05

+0

將JSON處理邏輯移至AsyncTask。在onCreate()方法中,你也不能在UI線程上建立網絡。將代碼移動到異步任務,並調用onResume()方法 – kumar 2013-03-05 08:25:36

+0

中的異步任務發佈帶有問題的json字符串。你用目前的代碼得到任何錯誤? – 2013-03-05 08:30:14

回答

0
ArrayList<String> values = new ArrayList<String>(); 
for (int i=0; i<folio.size(); ++i) { 
    values.add(folio.getString(i)); 
} 
+0

我試着做同樣的事情,但我在logcat – Shweta 2013-03-05 08:30:17

+0

中獲得附加測試爲「true」,你可以發佈日誌嗎? – lokoko 2013-03-05 08:31:30

+0

posted ..plz hav a luk – Shweta 2013-03-05 08:35:28

0

,因爲你已經把"image_urls.add(folio.getString(i))"在你的消息,如果項目成功添加這條線將返回true,你的日誌貓印花真.. 現在請查看如下

ArrayList<String > image_urls1 = new JSONArray(); //Use arrayList here 
       String s1 = seo.getString("Folio"); 
       for (int i = 0; i < folio.length(); i++) { 
        String m = folio.getString(i); 
        Log.v("M" + i, m); 
        image_urls1 .add(folio.getString(i) + ",");//adding here 
        Log("JSON-ARRAY-->" + image_urls1.get(i)); // printing the current element 
       } 
0

true表示您的添加成功。 您必須創建其他for循環數組才能獲取增加的值。 另外這款

for (int i = 0; i < folio.length(); i++) { 
    String m = folio.getString(i); 
    Log.v("M" + i, m); 
    image_urls.add(folio.getString(i) + ","); 
    Log("JSON-ARRAY-->" + image_urls.add(folio.getString(i))); 
} 

將建立在每個迭代上由於字符串連接3個StringBuilder對象。

+0

關於StringBuilder對象的好處 – 2013-03-05 18:50:37