C# ProgressDialog不能很好地工作
您好,我正在从服务器获取zip文件,我已经为它编写了代码 我也在展示这个对话 这是代码C# ProgressDialog不能很好地工作,c#,xamarin.android,C#,Xamarin.android,您好,我正在从服务器获取zip文件,我已经为它编写了代码 我也在展示这个对话 这是代码 public class DownloadImageFromUrl : AsyncTask<string, string, string> { private ProgressDialog pd; private Context context; public DownloadImageFromUrl(Context context) { this
public class DownloadImageFromUrl : AsyncTask<string, string, string>
{
private ProgressDialog pd;
private Context context;
public DownloadImageFromUrl(Context context)
{
this.context = context;
}
protected override void OnPreExecute()
{
pd = new ProgressDialog(context);
pd.SetMessage("Downloading file. Please wait...");
pd.Indeterminate = false;
pd.Max = 100;
pd.SetProgressStyle(ProgressDialogStyle.Horizontal);
pd.SetCancelable(true);
pd.Show();
base.OnPreExecute();
}
protected override void OnProgressUpdate(params string[] values)
{
base.OnProgressUpdate(values);
pd.SetProgressNumberFormat(values[0]);
Log.Verbose("values", "" + values[0]);
pd.Progress = int.Parse(values[0]);
}
protected override void OnPostExecute(string result)
{
pd.Dismiss();
}
protected override string RunInBackground(params string[] @params)
{
var storagePath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
string filePath = System.IO.Path.Combine(storagePath.AbsolutePath, "testfile.zip");
int count;
try
{
URL url = new URL("http://shadikidukan.co.in/abhi/AAA.zip");
filename = Path.GetFileName(new Uri(url.ToString()).AbsolutePath);
System.Console.WriteLine("Abhijir" + filename);
URLConnection connection = url.OpenConnection();
connection.Connect();
int LengthOfFile = connection.ContentLength;//Here connection.ContentLength return -1
Log.Verbose("length", ""+LengthOfFile);
InputStream input = new BufferedInputStream(url.OpenStream(), 8192);
OutputStream output = new FileOutputStream(filePath);
byte[] data = new byte[1024];
long total = 0;
while ((count = input.Read(data)) != -1)
{
total += count;
PublishProgress("" + (int)((total / 100) / LengthOfFile));
output.Write(data, 0, count);
}
output.Flush();
output.Close();
input.Close();
}
catch (Exception e)
{
System.Console.WriteLine("Abhijir"+e.Message);
}
return null;
}
}
如何克服该问题并在progressdialog中显示正确的值。内容长度为-1表示缺少标题或应忽略标题。这通常意味着服务器正在使用。根据设计,您无法知道要下载的文件的总长度,因此无法显示进度指示器
尝试在桌面浏览器中下载文件,您将看到在传输结束之前不会显示文件的总长度。该文件的解决方案是什么?没有解决方案。正如我所说,由于协议本身的设计方式,您无法获得内容长度。如果是您的服务器,则更改配置以停止使用分块传输。如果不是,那你就卡住了。是我的服务器。你能告诉我如何更改配置吗?我不知道。我不知道你在使用什么技术,也不知道你是如何设置的。你得自己搜索一下。
protected override void OnProgressUpdate(params string[] values)
{
base.OnProgressUpdate(values);
pd.SetProgressNumberFormat(values[0]);
Log.Verbose("values", "" + values[0]);//values[0] has negative values
pd.Progress = int.Parse(values[0]);
}