android上的间歇性HttpClient GET问题

我有一个我一直在研究的Android应用程序从服务器下载图像,将其读入位图并在ImageView上显示 这在大多数情况下都很有效,但每隔一段时间,它就会经历一个过程(有一个ProgressDialog说“获取图像......”)并且一旦完成它就不会显示任何内容。在logcat中没有任何东西甚至似乎与此远程相关。 这是代码:
    Bitmap image = null;

    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(webService + "?cmd=get");

    try
    {
        HttpResponse resp = client.execute(get);

        Log.i("PhotoRouletteDebug", "Resp buffer size: " + (int)resp.getEntity().getContentLength());
        InputStream is = resp.getEntity().getContent();

        BufferedInputStream buf = new BufferedInputStream(is, (int)resp.getEntity().getContentLength());

        image = BitmapFactory.decodeStream(buf);

        // clean up
        buf.close();
        is.close();
即使没有显示任何内容,Resp内容长度始终报告正确的大小,但仍然没有任何结果显示。 此代码是从AsyncTask调用的,但一次只调用一个任务。 这让我疯了,我不知道为什么它一直这样做。 编辑:这是设置imageView的代码
// AsyncTask for Getting a new image from the queue
protected class GetImageTask extends AsyncTask<String, String, Bitmap>
{
    protected void onPreExecute()
    {
        // lets show a progress dialog so the user knows something is going on
        progressDialog = ProgressDialog.show(PhotoRoulette.this, "", "Fetching image...", true);
    }

    protected void onPostExecute (Bitmap image)
    {
        // we got a new photo so lets display it where it needs to be displayed
        try
        {
            photoView = (ImageView)findViewById(R.id.photoView);
            photoView.setImageBitmap(image);
        }
        catch (Exception e)
        {
            Log.e("Debug", "Something absolutely retarded happened", e);
        }

        // hide the progress dialog - we're all done
        progressDialog.dismiss();
    }

    protected Bitmap doInBackground(String... urls)
    {
        // Get a new Bitmap Queue Image
        Bitmap image = imageHandler.getQueueImage();
        return image;
    }
}
    
已邀请:
您没有向我们展示显示图像的代码,因此我们不确定该代码是否正确。也许问题出在那里? 但假设问题是图像被破坏,这就是我开始调试的方法:用PushbackInputStream包裹
buf
。读取buf中的字节并将其保存到文件中;然后将那些相同的字节推回到
PushbackInputStream
。然后将
PushbackInputStream
传递给
BitmapFactory.decodeStream
。如果图像显示成功,则删除文件(手动或以编程方式)。否则,您现在可以在闲暇时检查位图。     

要回复问题请先登录注册