javaxml-parsingonclickzipandroid-xmlpullparser

Download file from XMLPullParser


I am quite new developer. I am building an app with AndroidStudio that read an XML file from server with PullParser. I need to download a file.zip from xml link and when I press the buttonDownload(inside the raw Layout of listView) it should start to download file. Here is the code of my row layout of my 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;
}

and here is the class where I should call the getLink() to start the download file.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: ");

}

After the download I have to unzip file and save it in the internal storage. the point is that I don't know how to call the getLink() to start downloading from it. I tried so many times and ways. I am stuck on it.

Please, any helps will be really apreciate.


Solution

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

    I wrote this inside my button in the ViewHolder. and create a method Download.class