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 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低