Square`s OkHttp. Download progress

Is there any way to get downloading file progress when using square`s OkHttp?

I did not find any solution in Recipes.

  • wait for 3 seconds or user click
  • android java lang runtimeexception fail to connect to camera service
  • How to get a android ListView item selector to use state_checked
  • How to create a shelf like view in Android?
  • Eclipse - parseSdkContent failed error - not recognizing XML Files
  • Android ListView Headers
  • They have class Call which can get content asynchronically, but there is no method to get current progress.

    Related posts:

    How to get current memory usage in android?
    RelativeLayout add rule “RelativeLayout.LEFT_OF” not working
    Benefits of using flush() close() in Android streams?
    How to detect orientation change in layout in Android?
    OpenStreetMap in Android
    sendBroadcast in a BroadcastReceiver
  • How do I add a horizontal RecyclerView inside a list item
  • Android product flavors are not considered when using CPU ABI split in build.gradle
  • java.io.IOException: SERVICE_NOT_AVAILABLE in GCM Client
  • Android - Using Dao Pattern with contentProvider
  • Translate animation works perfectly when defining with XML and only once perfectly by code - Android
  • Get list of places surrounding user's current location
  • 2 Solutions collect form web for “Square`s OkHttp. Download progress”

    Oops answer was obvious. Sorry for dumb question. Just need to read InputStream as usual.

    private class AsyncDownloader extends AsyncTask<Void, Long, Boolean> {
        private final String URL = "file_url";
        protected Boolean doInBackground(Void... params) {
            OkHttpClient httpClient = new OkHttpClient();
            Call call = httpClient.newCall(new Request.Builder().url(URL).get().build());
            try {
                Response response = call.execute();
                if (response.code() == 200) {
                    InputStream inputStream = null;
                    try {
                        inputStream = response.body().byteStream();
                        byte[] buff = new byte[1024 * 4];
                        long downloaded = 0;
                        long target = response.body().contentLength();
                        publishProgress(0L, target);
                        while (true) {
                            int readed = inputStream.read(buff);
                            if(readed == -1){
                            //write buff
                            downloaded += readed;
                            publishProgress(downloaded, target);
                            if (isCancelled()) {
                                return false;
                        return downloaded == target;
                    } catch (IOException ignore) {
                        return false;
                    } finally {
                        if (inputStream != null) {
                } else {
                    return false;
            } catch (IOException e) {
                return false;
        protected void onProgressUpdate(Long... values) {
            textViewProgress.setText(String.format("%d / %d", values[0], values[1]));
        protected void onPostExecute(Boolean result) {
            textViewStatus.setText(result ? "Downloaded" : "Failed");

    every time in the while loop ,you publish the progress.So it’s like being blocked

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.