【问题标题】:When trying to get Xml data from URL, it returns null尝试从 URL 获取 Xml 数据时,它返回 null
【发布时间】:2011-08-02 08:14:05
【问题描述】:

我正在尝试获取 xml 数据并使用异步任务对其进行解析。这是我所做的: 在 OnCreate 方法中,我将 url 作为字符串。我测试了我的网址,它不返回 null。也有权限连接到互联网。

            startDownload start = new startDownload();
            start.execute(url.toString());  

还有我的异步课程:

protected class startDownload extends AsyncTask<String, Void, String>{
    @Override
    protected void onPreExecute() {

        eczaDialog = ProgressDialog.show(ListViewXML.this,"", "Loading...");
    }
    @Override
    protected String doInBackground(String... aurl) {
        try {
            URL url = new URL(aurl[0]);

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

当我调试我的代码时,我看到这个 doc 变量返回 null。我不明白问题出在哪里。我希望你能帮助我找出答案。

【问题讨论】:

    标签: java android xml


    【解决方案1】:

    您必须获取 xml 的内容。可以这样用,代码返回字符串中的内容,之后就可以创建一个XML对象了:

    public static String getStringPage(String url){
        StringBuffer stringBuffer = new StringBuffer();
        BufferedReader bufferedReader = null;
        HttpClient httpClient = null;
        HttpGet httpGet = null;
        URI uri = null;
        HttpResponse httpResponse = null;
        InputStream inputStream = null;
        String HTMLCode = null;
    
    
        //Create client and a query to get the page
            httpClient = new DefaultHttpClient();
            httpGet = new HttpGet();
    
            //Set the query with the url in parameter
            try {
                uri = new URI(url);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            httpGet.setURI(uri);
    
            //Execute the query
            try {
                httpResponse = httpClient.execute(httpGet);
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            //Create a buffer to get the page
            try {
                inputStream = httpResponse.getEntity().getContent();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    
            //Get the buffer caracters
        try {
            HTMLCode = bufferedReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (HTMLCode!= null){
            stringBuffer.append(HTMLCode);
            stringBuffer.append("\n");
            try {
                HTMLCode = bufferedReader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        //Return the string of the page code
        return stringBuffer.toString();
    }
    

    【讨论】:

    • 理解并完成它需要时间,因为我是 Android 上的菜鸟。非常感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多