2017-10-04 63 views
-3

我打算通过将xml解析为文本来将博客转换为android应用程序。我已经完成了Java代码。现在,我想要在活动列表中显示它。我想为此Java代码列表视图xml代码。你能帮我解决这个问题吗?在Android中为博客作者列表视图

你可以从这里找到XML源:http://vestro-blogger-theme.blogspot.com/feeds/posts/default

public class MainActivity extends AppCompatActivity { 

     String title[]; 
     String content[]; 
     String contentHTML[]; 
     String thumbimage[]; 

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

      // You have to change your blogspot address here 
      // for example : http://YOURBLOGADDRESS.blogspot.com/feeds/posts/default 
      new StartParsing().execute("http://vestro-blogger-theme.blogspot.com/feeds/posts/default"); 
     } 

     private class StartParsing extends AsyncTask<String, Void, Void> { 

      @Override 
      protected Void doInBackground(String... params) { 
       String getURL = params[0]; 

       try { 
        URL url = new URL(getURL); 
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
        DocumentBuilder db = dbf.newDocumentBuilder(); 
        Document doc = db.parse(new InputSource(url.openStream())); 
        doc.getDocumentElement().normalize(); 

        NodeList nodeList = doc.getElementsByTagName("title"); 

        title = new String[nodeList.getLength()-1]; 
        content = new String[nodeList.getLength()-1]; 
        contentHTML = new String[nodeList.getLength()-1]; 
        thumbimage = new String[nodeList.getLength()-1]; 

        for (int i = 0; i < nodeList.getLength()-1; i++) { 
         Node node = nodeList.item(i); 

         title[i] = new String(); 
         content[i] = new String(); 
         contentHTML[i] = new String(); 
         thumbimage[i] = new String(); 

         //Get blogspot title 
         NodeList titleList = doc.getElementsByTagName("title"); 
         Element titleElement = (Element)titleList.item(i+1); 
         titleList = titleElement.getChildNodes(); 
         title[i] = ((Node)titleList.item(0)).getNodeValue(); 

         //Get blog spot content as HTML 
         NodeList contentHTMLList = doc.getElementsByTagName("content"); 
         Element contentHTMLElement = (Element)contentHTMLList.item(i+1); 
         contentHTMLList = contentHTMLElement.getChildNodes(); 
         contentHTML[i] = ((Node)contentHTMLList.item(0)).getNodeValue(); 

         // Make plain text from HTML 
         content[i] = String.valueOf(Html.fromHtml(Html.fromHtml(((Node)contentHTMLList.item(0)).getNodeValue()).toString())); 

         // Get Post content thumbnail 
         int start = contentHTML[i].indexOf("src=\"") + 5; 
         int end = contentHTML[i].indexOf("\"",start); 

         thumbimage[i] = contentHTML[i].substring(start,end); 
        } 
       }catch(Exception e) { 
        Log.d("Log","XML parsing exception = "+e); 
       } 
       return null; 
      } 

      @Override 
      protected void onPostExecute(Void aVoid) { 
       super.onPostExecute(aVoid); 

       // Check the data using log 
       Log.d("Log","Title is"); 
       for(int i=0; i<title.length; i++) { 
        Log.d("Log",title[i]); 
       } 
       Log.d("Log","content is"); 
       for(int i=0; i<content.length; i++) { 
        Log.d("Log",content[i]); 
       } 
       Log.d("Log","thumbimage is"); 
       for(int i=0; i<thumbimage.length; i++) { 
        Log.d("Log",thumbimage[i]); 
       } 
       Log.d("Log","contentHTML is"); 
       for(int i=0; i<contentHTML.length; i++) { 
        Log.d("Log",contentHTML[i]); 
       } 
      } 
     } 
    } 

回答

0

我很认真地惊讶,你为你的博客转换为应用程序解析XML数据。也许你可以实现它,但为什么首先这样做呢?

您可以简单地使用BLOGGER API解析您的博客内容。

Blogger API v3允许客户端应用程序查看和更新​​ Blogger内容。您的客户端应用程序可以使用Blogger API v3至 创建新博客帖子,编辑或删除现有帖子,以及查询符合特定条件的帖子 。

创建基于浏览器的应用程序和移动应用程序,使人们可以从任何地方创建和管理他们的帖子。

您可能擅长XML解析,它可能适合您,但为什么当一个简单的API调用可以获取结果时会使任务复杂化。我强烈建议其他人使用Blogger API代替XML解析。