2016-08-18 166 views
0

我有一些使用多個jsonobjects的問題我想使用「posts」和「attachments」jsonobjects。解析多個JsonObject和JsonArray

但我試圖使用該行和另一個for循環的附件jsonObject但它不工作。

String postInfo = jsonObject.getString("attachments"); 

的json看起來像這樣:

{"posts":[ 
     {"title":"Title","content":"Post content"} 

    ] 
    } 
    {"attachments":[ 
     {"url":"http://www.something.com"} 
    ] 
    } 

Java代碼:

public class NewsActivity extends FragmentActivity { 
    ViewPager viewPager; 
int category; 
ArrayList titleList; 
ArrayList postList; 
ArrayList imgList; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_news); 

    Intent i = getIntent(); 
    category=i.getIntExtra("locationInfo",-1); 

    try { 
     String encodedCatName = URLEncoder.encode(Integer.toString(category), "UTF-8"); 

     DownloadTask task = new DownloadTask(); 
     task.execute("http://www.something.co/api/get_category_posts/?id=" + encodedCatName); 


    } catch (UnsupportedEncodingException e) { 

     e.printStackTrace(); 

     // Toast.makeText(getApplicationContext(), "Could not find weather", Toast.LENGTH_LONG); 

    } 


} 

public class DownloadTask extends AsyncTask<String, Void, String> { 

    @Override 
    protected String doInBackground(String... urls) { 
     postList = new ArrayList(); 
     titleList = new ArrayList(); 
     imgList = new ArrayList(); 
     String result = ""; 
     URL url; 
     HttpURLConnection urlConnection = null; 

     try { 
      url = new URL(urls[0]); 

      urlConnection = (HttpURLConnection) url.openConnection(); 

      InputStream in = urlConnection.getInputStream(); 

      InputStreamReader reader = new InputStreamReader(in); 

      int data = reader.read(); 

      while (data != -1) { 

       char current = (char) data; 

       result += current; 

       data = reader.read(); 

      } 

      return result; 

     } catch (Exception e) { 

      Toast.makeText(getApplicationContext(), "Could not find", Toast.LENGTH_LONG); 

     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 


     try { 

      String message = ""; 

      JSONObject jsonObject = new JSONObject(result); 

      String postInfo = jsonObject.getString("posts"); 

      Log.i("Content", postInfo); 

      JSONArray arr = new JSONArray(postInfo); 
      JSONArray attachments = jsonObject.getJSONArray("attachments"); 

      for(int i=0; i< attachments.length(); i++){ 
       String url = ""; 
       url = attachments.getJSONObject(i).getString("url"); 
       imgList.add(url); 
      } 

      for (int i = 0; i < arr.length(); i++) { 

       JSONObject jsonPart = arr.getJSONObject(i); 

       String title = ""; 
       String post = ""; 

       title = jsonPart.getString("title"); 
       post = jsonPart.getString("content"); 


       if (title != "" && post != "") { 

        message += title + ": " + post + "\r\n"; 

        titleList.add(title); 
        postList.add(post); 


       } 

      } 


       viewPager = (ViewPager) findViewById(R.id.view_pager); 
       SwipeAdapter swipeAdapter = new SwipeAdapter(getSupportFragmentManager(),category,titleList,postList,imgList); 
       viewPager.setAdapter(swipeAdapter); 



     } catch (JSONException e) { 

      Toast.makeText(getApplicationContext(), "Could not find ", Toast.LENGTH_LONG); 

     } 


    } 
} 
} 

回答

0

類型與 '附件' 是一個數組,因此,你應該叫什麼如:

JSONArray attachments = jsonObject.getJSONArray("attachments") 
for(int i=0; i< attachments.length(); i++){ 
    attachments.getJSONObject(i).getString("url"); 
} 
+0

我試過了,但它不工作,我編輯了有問題的Java代碼,你可以再看一次,謝謝 –