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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!