بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
ایجاد پروگرس بار با retrofit
سلام خدمت همگی
من دارم با لایبرری retrofit یه فایل رو دانلود میکنم و مثل اموزش های استاد در سر فصل 15 میخوام پروگرس بار نشون داده بشه و میزان دانلود نمایش داده بشه من همون کار هارو دارم انجام میدم listener نوشتم اما پروگرس بار من پر نمیشه ، نه خطا میده نه هیچی حتی فایل هم دانلود میشه و از اخر یهو پروگرس بار پر میشه ، من حتی قبل مقدار دهی بهپروگرس بار یه log دارم چاپ میکنم اون log با هر بار صدا زدن listener چاپ میشه اما پروگرس بار مقدارش عوض نمیشه
ممنون میشم راهنمایی کنید
اینم کدم
public class MainActivity extends AppCompatActivity {
private TextView txt_percent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressBar prg_download = (ProgressBar) findViewById(R.id.prg_download);
txt_percent = (TextView) findViewById(R.id.txt_percent);
findViewById(R.id.btn_download).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onProgressDownloadListener onProgressDownloadListener = new onProgressDownloadListener() {
@Override
public void onProgress(final int percent, int downloadedSize, long fileSize) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i("TEST" , "per: " + percent)
prg_download.setProgress(pervent);
}
});
}
};
download(onProgressDownloadListener);
}
});
}//end onCreate
private String convertByteToMB(int lenght) {
float sizeInKB = lenght / 1024;
float sizeInMB = sizeInKB / 1024;
return String.format("%.1fMB", sizeInMB);
}
private void download(final onProgressDownloadListener downloadListener) {
IClient client = G.retrofit.create(IClient.class);
Call<ResponseBody> call = client.downloadFile();
Callback<ResponseBody> callback = new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
saveResponseToDevice(response.body(), downloadListener);
Toast.makeText(G.context, "Yes", Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.i("TEST", "Error:" + t.getMessage());
Toast.makeText(G.context, ":(", Toast.LENGTH_LONG).show();
}
};
call.enqueue(callback);
}
private boolean saveResponseToDevice(ResponseBody body, final onProgressDownloadListener downloadListener) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
byte[] buffer = new byte[1 * 1024];
final long fileSize = body.contentLength();
//Log.i("TEST", "File Size: " + fileSize);
int fileDownloadedSize = 0;
inputStream = body.byteStream();
File filePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "image.zip");
outputStream = new FileOutputStream(filePath);
int len = 0;
while ((len = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
fileDownloadedSize += len;
final float downloadPercent = 100.0f * (float) fileDownloadedSize / fileSize;
if (downloadListener != null) {
final int dinalFileDownloadedSize = fileDownloadedSize;
Log.i("TEST", "aaa");
downloadListener.onProgress((int) downloadPercent, dinalFileDownloadedSize, fileSize);
}
// Log.i("TEST", "File Download Size: " + fileDownloadedSize);
}
outputStream.flush();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}//end class
برای این سوال پاسخی وجود ندارد.
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .