2015-12-07 53 views
2

我在這裏有一個JSONlink我如何將JSON轉換爲JAVA對象?

我想將它轉換爲JAVA對象,但我未能加載圖像。

這裏是我的代碼

public class MainActivity extends ActionBarActivity { 
    // Declare Variables 
    JSONObject jsonobject; 
    JSONArray jsonarray; 
    ListView listview; 
    ListViewAdapter adapter; 
    ProgressDialog mProgressDialog; 
    ArrayList<HashMap<String, String>> arraylist; 
    static String url = "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResult=30&q=natok+bangla+mosharrof+karim&key=AIzaSyCR40QlsuX0aFfBV-wEPDsH_jxna1tDFRA"; 

    static String VIDEO_ID = "videoId"; 
    static String TITLE = "title"; 
    //static String DESCRIPTION = "description"; 
    static String THUMBNAILS = "http://img.youtube.com/vi/\" + videoId + \"/hqdefault.jpg\""; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // Get the view from listview_main.xml 
     setContentView(R.layout.listview_main); 
     // Execute DownloadJSON AsyncTask 
     new DownloadJSON().execute(); 
    } 

    // DownloadJSON AsyncTask 
    private class DownloadJSON extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      // Create a progressdialog 
      mProgressDialog = new ProgressDialog(MainActivity.this); 
      // Set progressdialog title 
      mProgressDialog.setTitle("Your Youtube Video is"); 
      // Set progressdialog message 
      mProgressDialog.setMessage("Loading..."); 
      mProgressDialog.setIndeterminate(false); 
      // Show progressdialog 
      mProgressDialog.show(); 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      // Create an array 
      arraylist = new ArrayList<HashMap<String, String>>(); 

      // Retrieve JSON Objects from the given URL address 

      String query = "bangla natok 2015"; 
      query = query.replace(" ", "+"); 
      try { 
       query = URLEncoder.encode(query, "utf-8"); 
      } catch (UnsupportedEncodingException e) { 
       // TODO Auto-generated catch block 
       //e.printStackTrace(); 
      } 

      jsonobject = JSONfunctions.getJSONfromURL("https://www.googleapis.com/youtube/v3/search?part=id%2Csnippet&q=natok+2015&maxResults=50&key=AIzaSyCR40QlsuX0aFfBV-wEPDsH_jxna1tDFRA"); 

      try { 
       // Locate the array name in JSON 
       JSONArray jsonarray = jsonobject.getJSONArray("items"); 

       for (int i = 0; i < jsonarray.length(); i++) { 
        HashMap<String, String> map = new HashMap<String, String>(); 
        jsonobject = jsonarray.getJSONObject(i); 
        // Retrive JSON Objects 
        JSONObject jsonObjId = jsonobject.getJSONObject("id"); 
        map.put("videoId", jsonObjId.getString("videoId")); 
        map.put ("img","http://img.youtube.com/vi/" + VIDEO_ID + "/hqdefault.jpg"); 

        JSONObject jsonObjSnippet = jsonobject.getJSONObject("snippet"); 
        map.put("title", jsonObjSnippet.getString("title")); 

        //map.put("description", jsonObjSnippet.getString("description")); 
        // map.put("flag", jsonobject.getString("flag")); 




        // Set the JSON Objects into the array 
        arraylist.add(map); 
       } 
      } catch (JSONException e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void args) { 
      // Locate the listview in listview_main.xml 
      listview = (ListView) findViewById(R.id.listview); 
      // Pass the results into ListViewAdapter.java 
      adapter = new ListViewAdapter(MainActivity.this, arraylist); 
      // Set the adapter to the ListView 
      listview.setAdapter(adapter); 
      // Close the progressdialog 
      mProgressDialog.dismiss(); 
     } 
    } 

我需要的一切我收到顯示在這裏,但我不能顯示圖像可這兒過得找不到&得到任何形式error.is還有任何問題將JSON轉換爲java對象?

+0

e.getMessage()是什麼?編輯你的問題併發布消息(或者最好是堆棧跟蹤)。 –

+0

我找不到任何形式的錯誤消息@Little Child –

回答

2

它更好地使用GSON在這種情況下,它會將您的json字符串轉換爲java對象而無需手動工作。

添加下面一行到搖籃依賴

compile 'com.google.code.gson:gson:1.7.2' 

現在使用此代碼將JSON字符串轉換成Java對象

POJOClass obj = gson.fromJson(json, POJOClass.class) 

您也可以使用本網站的JSON到POJO類轉換,而不手動創建它

http://www.jsonschema2pojo.org/

讓我知道在發生問題

+0

我只想要圖像我怎麼能得到你可以請你簡單解釋一下? –