AnnBnnCnnD 2012-12-10 09:07 采纳率: 0%
浏览 2830
已采纳

在android上的XML文件,怎么样从远程服务器上获取

需要在远程服务器上读取XML文件,然后再修改本地android上的数据。我用XmlPullParser读取数据。

XmlPullParser users;
            try {
                URL xmlUrl = new URL("http://xx.xx.xx.xx/1.xml");
                users = XmlPullParserFactory.newInstance().newPullParser();
                users.setInput(xmlUrl.openStream(), null);

            }

怎么样在安卓上修改?

  • 写回答

3条回答 默认 最新

  • ce_la_vie 2012-12-17 09:06
    关注

    用下面的代码,他会覆盖你从网上下载的文件:

    public static boolean downloadFile(String fileToDownload, File newPath,
                String newFileName) {
            try {
                URL url = new URL(fileToDownload);
                HttpURLConnection urlConnection = (HttpURLConnection) url
                        .openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.setDoOutput(true);
                urlConnection.connect();
                if (!newPath.isDirectory()) {
    
                    CreateLog.createFolder(newPath.toString());
                }
                File file = new File(newPath.toString() + "/" + newFileName);
                if (!file.isFile()) {
                    CreateLog.writeLogToFile(newPath.toString() + newFileName,
                            "%TEMP%");
                }
    
                FileOutputStream fileOutput = new FileOutputStream(file);
                InputStream inputStream = urlConnection.getInputStream();
    
                byte[] buffer = new byte[1024];
                int bufferLength = 0;
    
                while ((bufferLength = inputStream.read(buffer)) > 0) {
                    fileOutput.write(buffer, 0, bufferLength);
                }
                fileOutput.close();
                return true;
    
            } catch (MalformedURLException e) {
                CreateLog.addToLog(e.toString());
                return false;
            } catch (IOException e) {
                CreateLog.addToLog(e.toString());
                return false;
            }
        }
    
    public static void createFolder(String filePath) {
        File createFolder = new File(filePath);
        createFolder.mkdirs();
    }
    

    更简单的方式是Asynctask,代码是在一个新线程里:

    private class GetProblems extends AsyncTask<String, Integer, String> {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }
    
            @Override
            protected String doInBackground(String... params) {
                for (String myUrl : params) {
    
                    try {
    
                        URL url = new URL(myUrl);
                        URLConnection ucon = url.openConnection();
                        ucon.setRequestProperty("Accept", "application/xml");
    
                        InputStream is = ucon.getInputStream();
                        BufferedInputStream bis = new BufferedInputStream(is);
    
                        ByteArrayBuffer baf = new ByteArrayBuffer(50);
                        int current = 0;
                        while ((current = bis.read()) != -1) {
                            baf.append((byte) current);
                        }
                        String str = new String(baf.toByteArray(), "UTF8");
    
                        return str;
    
                    } catch (MalformedURLException e) {
                        CreateLog.addToLog("[GetProblems] " + e.toString());
                    } catch (IOException e) {
                        CreateLog.addToLog("[GetProblems] " + e.toString());
                    }
                }
                return "error";
            }
    
            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);
                // updateProgressBar(values[0]);
            }
    
            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
    
                            ...write result to a file
    
            }
        }
    

    运行AsyncTask:

    new GetProblems().execute("http://myurl.com/xmlfile.xml");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable