Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检测Android中rss源的编码_Android_Xml_Encoding_Rss - Fatal编程技术网

检测Android中rss源的编码

检测Android中rss源的编码,android,xml,encoding,rss,Android,Xml,Encoding,Rss,我正在尝试使用XmlPullParser解析XML 我想获取提要的编码 比如说 <?xml version="1.0" encoding="ISO-8859-1"?> 及 这里有两种不同的编码我想检测编码 谁能 我已经尝试过getInputEncoding()您可以这样做: XmlPullparser parser=新的XmlPullparser(inputstream in,null); 这将自动检测编码您可以这样做: //URL_FEED Example: ht

我正在尝试使用XmlPullParser解析XML

我想获取提要的编码

比如说

    <?xml version="1.0" encoding="ISO-8859-1"?>


这里有两种不同的编码我想检测编码

谁能
我已经尝试过
getInputEncoding()

您可以这样做: XmlPullparser parser=新的XmlPullparser(inputstream in,null);
这将自动检测编码

您可以这样做:
//URL_FEED Example: http://jovemnerd.ig.com.br/feed/rss/
final HttpGet httpget = new HttpGet(URL_FEED);

//Connect 
final HttpResponse response = httpclient.execute(httpget);

//Get Entity connection
HttpEntity entity = response.getEntity();

//Get InputStream
InputStream feed = entity.getContent();
XmlPullparser parser=新的XmlPullparser(inputstream in,null); 这将自动检测编码

//URL_FEED Example: http://jovemnerd.ig.com.br/feed/rss/
final HttpGet httpget = new HttpGet(URL_FEED);

//Connect 
final HttpResponse response = httpclient.execute(httpget);

//Get Entity connection
HttpEntity entity = response.getEntity();

//Get InputStream
InputStream feed = entity.getContent();


@我补充说,米苏萨克comment@leus谢谢你的代码,我将测试它是否工作,将标记为正确,但给你+1帮助我,非常感谢:)@leus我有一个如下的xml One test Des test.com/Two test Des test.com/但是根据你的代码,编码返回空值。我从其他xml中获得UTF-8,这些xml有“UTF-8”,但没有“iso-8859-1”。你能帮我吗check@MishuSarker我更新了detect Encoding from head xml:
String GetEncodingFromEntity(HttpEntity实体)
@MishuSarker我添加了comment@leus谢谢你的代码,我将测试它是否工作,将标记为正确,但给你+1帮助我,非常感谢:)@leus我有一个如下的xml One-Test Des-Test.com/Two-Test Des-Test.com/但是根据您的代码,编码返回null。我从其他xml中获得UTF-8,这些xml有“UTF-8”,但没有“iso-8859-1”。你能帮我吗check@MishuSarker我更新了detect enconding from head xml:
String getencondingfromtentity(HttpEntity entity)
您读过这个吗?谢谢你的推荐。我会检查一下,让你知道:)你读过这个吗?谢谢你的推荐。我会检查并让您知道:)
//Convert InputStrean to InputSource
final InputSource source = new InputSource(feed);

//If encondind is not detect , then read head xml and set enconding   
if(source.getEncoding()==null){ //THIS THE PROBLEM
    //The enconding is null, but in entity have head with type enconding
    source.setEncoding(getEncondingFromEntity(entity));
}

/*Now your InputSource have the correct enconding, then use "source" in your parse. Ex:*/
final XMLReader xmlreader = parser.getXMLReader();
final RSSHandler handler = new RSSHandler(config);
xmlreader.setContentHandler(handler);
xmlreader.parse(source);
private String getEncondingFromEntity(HttpEntity entity){
  if(entity.getContentType()!=null){
    //Content-Type: text/xml; charset=ISO-8859-1
    //Content-Type: text/xml; charset=UTF-8
      for(String str : entity.getContentType().getValue().split(";")){
          if(str.toLowerCase().contains("charset")){
              return str.toLowerCase().replace("charset=","").replace(";","").replace(" ","");
          }
      }
  }
  return null;
}
//Open Connection with URL XML Content
        URL url;
        InputStream feedStream = null;
        HttpURLConnection urlConnection = null;
//This URL have enconding ISO-8859-1
        url = new URL("http://feeds.feedburner.com/99vidaspodcast");
        urlConnection = (HttpURLConnection) url.openConnection();
        feedStream = new BufferedInputStream(urlConnection.getInputStream());

//Read and Parse XML with correct Enconding

        RSSFeed feed= parser.parse(feedStream,getEncondingFromEntity(urlConnection.getContentType()));

----------------------
//Detect Enconding

        private String getEncondingFromEntity(String contentType){
          if(contentType!=null){
           for(String str : contentType.split(";")){
            if(str.toLowerCase().contains("charset")){
              return str.toLowerCase().replace("charset=","").replace(";","").replace(" ","");
            }
           }
          }
          return null;
        }

------------------------
//Apply Enconding and Parse XML    
    private RSSFeed parse(SAXParser parser, InputStream feed, String enconding)
          throws SAXException, IOException {
        if (parser == null) {
          throw new IllegalArgumentException("RSS parser must not be null.");
        } else if (feed == null) {
          throw new IllegalArgumentException("RSS feed must not be null.");
        }

        final InputSource source = new InputSource(feed);
        if(source.getEncoding()==null && enconding!=null){
            //Magic :]
            source.setEncoding(enconding);
        }
        final XMLReader xmlreader = parser.getXMLReader();
        final RSSHandler handler = new RSSHandler(config);

        xmlreader.setContentHandler(handler);
        xmlreader.parse(source);

        return handler.feed();
      }

    -------------------------