android - 适用于Android的Youtube Gdata

标签 android html youtube

如何获取youtube feed标签并将其转换为android中的字符串/整数。
我想使用标签,以及属性:totalUploadViewCount?

https://developers.google.com/youtube/2.0/reference#Response_codes_retrieving_feeds

最佳答案

这可以为您提供一个起点。

 WebRequest response = new WebRequest("https://gdata.youtube.com/feeds/api/channelstandardfeeds/most_viewed?v=2", PostType.GET);
 String responseXML = response.Get();
 Document doc = XMLfunctions.XMLfromString(responseXML);
 NodeList nodes = doc.getElementsByTagName("yt:channelStatistics");
 String totalUploadViewCount = nodes.item(0).getAttributes().getNamedItem("totalUploadViewCount").getNodeValue(); 
 Log.d("", totalUploadViewCount);    

WebRequest.class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

public class WebRequest {
  public enum PostType{
    GET, POST;
  }

  public String _url;
  public String response = "";
  public PostType _postType;
  CookieStore _cookieStore = new BasicCookieStore();

  public WebRequest(String url) {
    _url = url;
    _postType = PostType.POST;
  }

  public WebRequest(String url, CookieStore cookieStore) {
    _url = url;
    _cookieStore = cookieStore;
    _postType = PostType.POST;
  }

  public WebRequest(String url, PostType postType) {
    _url = url;
    _postType = postType;
  }

  public String Get() {
    HttpClient httpclient = new DefaultHttpClient();

    try {
      // Create local HTTP context
      HttpContext localContext = new BasicHttpContext();

      // Bind custom cookie store to the local context
      localContext.setAttribute(ClientContext.COOKIE_STORE, _cookieStore);

      HttpResponse httpresponse;
      if (_postType == PostType.POST)
      {
        HttpPost httppost = new HttpPost(_url);
        httpresponse = httpclient.execute(httppost, localContext);
      }
      else
      {
        HttpGet httpget = new HttpGet(_url);
        httpresponse = httpclient.execute(httpget, localContext);
      }

      StringBuilder responseString = inputStreamToString(httpresponse.getEntity().getContent());

      response = responseString.toString();
    }
    catch (UnknownHostException e) {
      e.printStackTrace();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      // When HttpClient instance is no longer needed,
      // shut down the connection manager to ensure
      // immediate deallocation of all system resources
      httpclient.getConnectionManager().shutdown();
    }

    return response;
  }

  private StringBuilder inputStreamToString(InputStream is) throws IOException {
    String line = "";
    StringBuilder total = new StringBuilder();

    // Wrap a BufferedReader around the InputStream
    BufferedReader rd = new BufferedReader(new InputStreamReader(is,Charset.forName("iso-8859-9")));
    // Read response until the end
    while ((line = rd.readLine()) != null) {
      total.append(line);
    }

    // Return full string
    return total;
  }
}

XMLfunctions.class
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XMLfunctions {

  public final static Document XMLfromString(String xml) {

    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
      DocumentBuilder db = dbf.newDocumentBuilder();
      InputSource is = new InputSource();
      is.setCharacterStream(new StringReader(xml));
      doc = db.parse(is);
    }
    catch (ParserConfigurationException e) {
      System.out.println("XML parse error: " + e.getMessage());
      return null;
    }
    catch (SAXException e) {
      System.out.println("Wrong XML file structure: " + e.getMessage());
      return null;
    }
    catch (IOException e) {
      System.out.println("I/O exeption: " + e.getMessage());
      return null;
    }

    return doc;
  }
}

关于android - 适用于Android的Youtube Gdata,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10127939/

相关文章:

android - arraylist 没有正确更新

android - com.crashlytics.android.CrashlyticsMissingDependencyException 在 android studio 项目上的 gradle 'testDebug' 期间

html - 使用 CSS 实现类似 TikTok 的滚动效果

android - 将 WebView 和 Browser 用于 YouTube Video api 有什么不同?

android - 如何用 MVP 实现 PhoneStateListener

android - java.io.IOException : Is a directory Android 异常

html - 如何使分离器远离天然药丸

javascript - 如何在 mysqli 和 php 中保存 bootstrap 2 或 3 导航选项卡内容?

html - iframe “YouTube search result”在Facebook应用上

php - parse_str()突然不起作用