为什么okhttp asynctask没有执行

时间:2015-10-11 13:30:57

标签: android android-asynctask okhttp

前两个日志ddddddimageurl在logcat中可见,但其他日志未显示。我没有收到应用程序正在打开的错误。它似乎没有进入try {},因为log.d' Fffff'确实在logcat中显示。为什么?我正在尝试从网址加载图片并将其显示在应用中。

public class OkHttpHandler extends AsyncTask<Void, Void, byte[]> {

    private final String IMAGE_URL = "http://bit.ly/1DU2Zka";

    OkHttpClient httpClient = new OkHttpClient();

    @Override
    protected byte[] doInBackground(Void... params) {
        Log.d("e", "dddddddddd");
        Log.d("e", IMAGE_URL);

        Request.Builder builder = new Request.Builder();
        builder.url(IMAGE_URL);

        Request request = builder.build();

        try {

            Response response = httpClient.newCall(request).execute();
            Log.d("e", "dddddddddd");
            Log.d("e", "dddddddddd");
            Log.d("e", "dddddddddd");
            Log.d("e", "dddddddddd");
            Log.d("e", "dddddddddd");
            System.out.println(response);
            System.out.println(response);
            System.out.println(response);
            System.out.println(response);
            System.out.println(response);
            Log.d("e", "FFFFF");
            Log.d("e", "FFFFFFFF");
            Log.d("e", "dddddddddd");
            Log.d("e", "dddddddddd");
            Log.d("e", "dddddddddd");
            return response.body().bytes();
        } catch (Exception e) {
        }

        return null;
    }
}

我这么称呼它

public void downloadImage() {
    OkHttpHandler handler = new OkHttpHandler();

    byte[] image = new byte[0];

    try {

        image = handler.execute().get();

        if (image != null && image.length > 0){
            Log.d("e", "dddddddddd");
            Log.d("e", "dddddddddd");
            Log.d("e", "dddddddddd");
            Log.d("e", "dddddddddd");
            Log.d("e", "dddddddddd");
            Log.isLoggable("e",image.length);
            Log.d("e", "dddddddddd");
            Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
            imageView.setImageBitmap(bitmap);
            txtBytes.setText("Total bytes downloaded: " + image.length);
        }

    } catch (Exception e) {
        txtBytes.setText("Hmm sorry, something went wrong!");
    }

}

0 个答案:

没有答案