有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    要解析YouTube搜索的视频Json提要,请使用以下链接:
    https://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q=my+love+by+shovon

    Here "my love by shovon" is searched item.

    使用任何json解析器解析它,并获取搜索项目的YouTube视频使用这个Json解析器

    public JSONObject getJson()
    {
        DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
        HttpPost httppost = new HttpPost("https://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q=my%20love%20by%20shovon");
        // Depends on your web service
        httppost.setHeader("Content-type", "application/json");
    
        InputStream inputStream = null;
        String result = null;
        HttpResponse response = null;
        try {
            response = httpclient.execute(httppost);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }           
        HttpEntity entity = response.getEntity();
    
        try {
            inputStream = entity.getContent();
        } catch (IllegalStateException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        // json is UTF-8 by default i beleive
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        StringBuilder sb = new StringBuilder();
    
        String line = null;
        try {
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        result = sb.toString();
        JSONObject job = null;
        try {
            job = new JSONObject(sb.toString());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return job;
    }