Java 没有';我不理解包含节点列表的程序的流程

Java 没有';我不理解包含节点列表的程序的流程,java,android,Java,Android,在下面的程序中,我不理解NodeList的流程和使用。为什么在这里使用节点列表?在程序中使用它的目的是什么?如果这很重要,请帮助我从NodeList出现的那一行理解它 package net.learn2develop.Networking; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import java.io.IOException; import java.io

在下面的程序中,我不理解NodeList的流程和使用。为什么在这里使用节点列表?在程序中使用它的目的是什么?如果这很重要,请帮助我从NodeList出现的那一行理解它

package net.learn2develop.Networking;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element; 
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class MainActivity extends Activity {
    ImageView img;    

    private class BackgroundTask extends AsyncTask<String, Void, Bitmap> {       
        protected Bitmap doInBackground(String... url) {            
             //---download an image---
            Bitmap bitmap = DownloadImage(url[0]);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return bitmap;
        }       

        protected void onPostExecute(Bitmap bitmap) {         
            ImageView img = (ImageView) findViewById(R.id.img);
            img.setImageBitmap(bitmap);         
        }
    }

    private InputStream OpenHttpConnection(String urlString) 
    throws IOException
    {
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString); 
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection))                     
            throw new IOException("Not an HTTP connection");        
        try{
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            response = httpConn.getResponseCode();                 
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();                                 
            }                     
        }
        catch (Exception ex)
        {
            throw new IOException("Error connecting");            
        }
        return in;     
    }   

    private Bitmap DownloadImage(String URL)
    {        
        Bitmap bitmap = null;
        InputStream in = null;        
        try {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e1) {
            Toast.makeText(this, e1.getLocalizedMessage(), 
                    Toast.LENGTH_LONG).show();
            e1.printStackTrace();
        }
        return bitmap;                
    }

    private String DownloadText(String URL)
    {
        int BUFFER_SIZE = 2000;
        InputStream in = null;
        try {
            in = OpenHttpConnection(URL);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return "";
        }

        InputStreamReader isr = new InputStreamReader(in);
        int charRead;
        String str = "";
        char[] inputBuffer = new char[BUFFER_SIZE];          
        try {
            while ((charRead = isr.read(inputBuffer))>0)
            {                    
                //---convert the chars to a String---
                String readString = 
                    String.copyValueOf(inputBuffer, 0, charRead);                    
                str += readString;
                inputBuffer = new char[BUFFER_SIZE];
            }
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "";
        }    
        return str;        
    }

    private void WordDefinition(String word) {
        InputStream in = null;
        try {
            in = OpenHttpConnection("http://services.aonaware.com/DictService/DictService.asmx/Define?word=" + word);
            Document doc = null;
            DocumentBuilderFactory dbf = 
                DocumentBuilderFactory.newInstance();
            DocumentBuilder db;            
            try {
                db = dbf.newDocumentBuilder();
                doc = db.parse(in);
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }            
            doc.getDocumentElement().normalize(); 

            //---retrieve all the <Definition> nodes---
            NodeList definitionElements = 
                doc.getElementsByTagName("Definition"); 

            String strDefinition = "";
            for (int i = 0; i < definitionElements.getLength(); i++) { 
                Node itemNode = definitionElements.item(i); 
                if (itemNode.getNodeType() == Node.ELEMENT_NODE) 
                {            
                    //---convert the Node into an Element---
                    Element definitionElement = (Element) itemNode;

                    //---get all the <WordDefinition> elements under 
                    // the <Definition> element---
                    NodeList wordDefinitionElements = 
                        (definitionElement).getElementsByTagName(
                        "WordDefinition");

                    strDefinition = "";
                    for (int j = 0; j < wordDefinitionElements.getLength(); j++) {                    
                        //---convert a <WordDefinition> Node into an Element---
                        Element wordDefinitionElement = 
                            (Element) wordDefinitionElements.item(j);

                        //---get all the child nodes under the 
                        // <WordDefinition> element---
                        NodeList textNodes = 
                            ((Node) wordDefinitionElement).getChildNodes();

                        //---get the first node, which contains the text---
                        strDefinition += 
                            ((Node) textNodes.item(0)).getNodeValue() + ". ";    
                    }                    
                    //---display the title---
                    Toast.makeText(getBaseContext(),strDefinition, 
                        Toast.LENGTH_SHORT).show();
                } 
            }            
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();            
        }        
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
package net.learn2develop.Networking;
导入android.app.Activity;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.net.HttpURLConnection;
导入java.net.URL;
导入java.net.URLConnection;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.Toast;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.parserConfiguration异常;
导入org.w3c.dom.Document;
导入org.w3c.dom.Element;
导入org.w3c.dom.Node;
导入org.w3c.dom.NodeList;
公共类MainActivity扩展了活动{
图像视图img;
私有类BackgroundTask扩展异步任务{
受保护的位图doInBackground(字符串…url){
//---下载图像---
位图位图=下载图像(url[0]);
试一试{
睡眠(3000);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回位图;
}       
受保护的void onPostExecute(位图){
ImageView img=(ImageView)findViewById(R.id.img);
设置图像位图(位图);
}
}
私有InputStream OpenHttpConnection(字符串urlString)
抛出IOException
{
InputStream in=null;
int响应=-1;
URL=新URL(URL字符串);
URLConnection conn=url.openConnection();
if(!(HttpURLConnection的连接实例))
抛出新IOException(“非HTTP连接”);
试一试{
HttpURLConnection httpConn=(HttpURLConnection)conn;
httpConn.setAllowUserInteraction(假);
httpConn.setInstanceFollowRedirects(真);
httpConn.setRequestMethod(“GET”);
httpConn.connect();
response=httpConn.getResponseCode();
if(response==HttpURLConnection.HTTP\u OK){
in=httpConn.getInputStream();
}                     
}
捕获(例外情况除外)
{
抛出新IOException(“连接错误”);
}
返回;
}   
私有位图下载图像(字符串URL)
{        
位图=空;
InputStream in=null;
试一试{
in=OpenHttpConnection(URL);
位图=BitmapFactory.decodeStream(in);
in.close();
}捕获(IOE1异常){
Toast.makeText(这是e1.getLocalizedMessage(),
Toast.LENGTH_LONG).show();
e1.printStackTrace();
}
返回位图;
}
私有字符串下载文本(字符串URL)
{
int BUFFER_SIZE=2000;
InputStream in=null;
试一试{
in=OpenHttpConnection(URL);
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
返回“”;
}
InputStreamReader isr=新的InputStreamReader(in);
int-charRead;
字符串str=“”;
char[]inputBuffer=新字符[BUFFER_SIZE];
试一试{
而((charRead=isr.read(inputBuffer))>0)
{                    
//---将字符转换为字符串---
字符串读取字符串=
String.copyValueOf(inputBuffer,0,charRead);
str+=readString;
inputBuffer=新字符[缓冲区大小];
}
in.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
返回“”;
}    
返回str;
}
专用void字定义(字符串字){
InputStream in=null;
试一试{
in=OpenHttpConnection(“http://services.aonaware.com/DictService/DictService.asmx/Define?word=“+字);
单据单据=空;
DocumentBuilderFactory dbf=
DocumentBuilderFactory.newInstance();
文档生成器数据库;
试一试{
db=dbf.newDocumentBuilder();
doc=db.parse(in);
}捕获(ParserConfiguration异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}            
doc.getDocumentElement().normalize();
//---检索所有节点---
节点列表定义元素=
getElementsByTagName文件(“定义”);
字符串strDefinition=“”;
对于(inti=0;iOpenHttpConnection(
"http://services.aonaware.com/DictService/DictService.asmx/Define?word=" + word);
doc = db.parse(in);
//---retrieve all the <Definition> nodes---
NodeList definitionElements = 
    doc.getElementsByTagName("Definition");
<Definition>
    <Word>cow</Word>
    <Dictionary>
        <Id>easton</Id>
        <Name>Easton's 1897 Bible Dictionary</Name>
    </Dictionary>
    <WordDefinition>
        Cow A cow and her calf were not to be killed on the same day (Lev. 22:28; Ex. 23:19; Deut. 22:6, 7). The reason for this enactment is not given. A state of great poverty is described in the words of Isa. 7:21-25, where, instead of possessing great resources, a man shall depend for the subsistence of himself and his family on what a single cow and two sheep could yield.
    </WordDefinition>
</Definition>