我是一个相当新的开发人员。我正在使用 AndroidStudio 构建一个应用程序,该应用程序使用 PullParser 从服务器读取 XML 文件。我需要从 xml 链接下载一个 file.zip,当我按下 buttonDownload(在 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() 来开始下载 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: ");
}
下载后我必须解压缩文件并将其保存在内部存储中。关键是我不知道如何调用 getLink() 来开始下载。我尝试了很多次和方法。我坚持下去。
拜托,任何帮助都会非常感激。