从 XMLPullParser下载文件



我是相当新的开发人员。我正在使用AndroidStudio构建一个应用程序,该应用程序使用PullParser从服务器读取XML文件。我需要从xml链接下载一个文件.zip当我按下按钮下载(在listView的原始布局内)时,它应该开始下载文件。这是我的 SitesAdapter 的行布局的代码。

public View getView(final int pos, final View convertView, final ViewGroup parent){
    RelativeLayout row = (RelativeLayout)convertView;
    Log.i("AtlantisSites", "getView pos = " + pos);
    //ViewHolder mainViewHolder = null;
    ViewHolder viewHolder;
    if(null == row){
//No recycled View, we have to inflate one.
        LayoutInflater inflater =(LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = (RelativeLayout)inflater.inflate(R.layout.row_site, null);
        viewHolder = new ViewHolder();
        viewHolder.btnDownload = (Button) row.findViewById(R.id.btnDownload);
        viewHolder.btnDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer pos=(Integer)v.getTag();
                Log.i("AtlantisSites", "getView pos = " + pos);
                String url = getItem(pos).getLink();
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
            }
        });
row.setTag(viewHolder);
    } else{
        viewHolder = (ViewHolder) row.getTag();
    }
viewHolder.btnDetails.setTag(getItem(pos));
public class ViewHolder{
    Button btnDownload;
    Button btnDetails;
}

这是我应该调用 getLink() 来启动下载文件的类.zip

public class DownloadZip {

public void DownloadFromUrlZip(){
    try {

        URL url = new URL (s);
        HttpURLConnection c = (HttpURLConnection)url.openConnection();
        c.setRequestMethod("GET");
        c.setDoOutput(true);
        c.connect();
        String Path = Environment.getExternalStorageDirectory() + "/download/";
        Log.v("AtlantisSites", "PATH: " + Path);
        File file = new File(Path);
        file.mkdirs();
        FileOutputStream fos = new FileOutputStream("AtlantisIssue.zip");
        InputStream is = c.getInputStream();
        byte[] buffer = new byte[1024];
        int len1 = 0;
        while ((len1 = is.read(buffer)) != -1) {
            fos.write(buffer, 0, len1);
        }
        fos.close();
        is.close();
    } catch (IOException e) {
        Log.d("AtlantisSites", "Error: " + e);
    }
    Log.v("AtlantisSites", "Check: ");
}

下载后,我必须解压缩文件并将其保存在内部存储中。关键是我不知道如何调用getLink()开始从中下载。我尝试了很多次和方法。我被困在上面。

拜托,任何帮助都会非常感激。

 Intent i = new Intent(v.getContext(), DownloadZip.class);
 v.getContext().startActivity(i);

我把它写在ViewHolder的按钮内。 并创建一个方法 下载.class

相关内容

  • 没有找到相关文章

最新更新