HTTP GET请求丢失数据 [英] HTTP get request is missing data

查看:1211
本文介绍了HTTP GET请求丢失数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在Android的HTTP一个奇怪的问题。
我试图从一个远程服务器上的图片,并在设备上显示。
如果图象是小的JPEG,这是没有问题的。但如果图片的尺寸变得更大,它不会工作(仅适用于部分画面显示)。

下面是我的完整演示code:

 公共类HTTP_testActivity延伸活动{私人ImageView的ivPicture;
私人按钮btGetImage;
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    ivPicture =(ImageView的)findViewById(R.id.ivpiture1);
    btGetImage =(按钮)findViewById(R.id.btGetPicture1);
    btGetImage.setOnClickListener(新Button.OnClickListener()
    {
        公共无效的onClick(查看arg0中)
        {
            URI URI;
            尝试{
                URI =新的URI();
                URLConnection的连接= URI.toURL()根据的openConnection()。
                connection.setUseCaches(真);
                connection.connect();                二BufferedInputStream为=新的BufferedInputStream(connection.getInputStream());
                Log.d(TEST,输入长度+ bis.available());
                尝试{
                    视频下载(2000);
                }赶上(InterruptedException的E){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }
                Log.d(TEST,等待后输入长度+ bis.available());
                字节[]数据=新的字节[640 * 480 * 5];
                bis.read(数据);
                BMP位图= BitmapFactory.de codeByteArray的(数据,0,jdata.length);
                如果(BMP!= NULL)
                {
                    ivPicture.setImageBitmap(BMP);
                }
                bis.close();
        }赶上(的URISyntaxException E){
            // TODO自动生成catch块
            Log.d(TEST,e.getMessage());
            e.printStackTrace();
        }赶上(MalformedURLException的E){
            // TODO自动生成catch块
            Log.d(TEST,e.getMessage());
            e.printStackTrace();
        }赶上(IOException异常五){
            // TODO自动生成catch块
            Log.d(TEST,e.getMessage());
            e.printStackTrace();
        }
        }
    });
}

有人可以看到我在做什么错?
什么到目前为止,我想通了就是:bis.available()返回从来没有超过65KB以上。虽然InputStream的本身所具有的合适的长度(在调试器看到)。


解决方案

  

用()


将返回CON被从InputStream无阻塞读的字节数。因此,将不受阻塞地返回,可以从网络上读取数据。

尝试:

 的InputStream双=新的InputStream(connection.getInputStream());
            Log.d(TEST,输入长度+ bis.available());
            尝试{
                视频下载(2000);
            }赶上(InterruptedException的E){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            INT读= 0;
            字节[]缓冲区=新的字节[SIZE_OF_IMAGE]
            字节[] = inBuff新的字节[32678]
            而((读取= bis.read(inBuff,0,32768))大于0){
                   //缓冲区拷贝哪些已经阅读
                   //从输入流
            }            //关闭是
            BMP位图= BitmapFactory.de codeByteArray的(缓冲,0,buffer.length);

显然不是还有更有效的方式做到这一点,但可能是一个切入点。让我知道如果你需要更多的帮助。

编辑:当然,你必须避免做在UI线程阻塞调用,如人建议

Hi i have a strange problem with HTTP in Android. I'm trying to get a picture from a remote server and display it on the device. If the picture is a small JPEG, this is not a problem. but if the picture get bigger in size it will not work (only parts of the picture are shown).

Here is my complete demo code:

public class HTTP_testActivity extends Activity {

private ImageView ivPicture;
private Button btGetImage;  


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ivPicture = (ImageView) findViewById(R.id.ivpiture1);
    btGetImage = (Button) findViewById(R.id.btGetPicture1);
    btGetImage.setOnClickListener(new Button.OnClickListener() 
    {
        public void onClick(View arg0) 
        {
            URI uri;
            try {


                uri = new URI("");
                URLConnection connection =  uri.toURL().openConnection();
                connection.setUseCaches(true);
                connection.connect();

                BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
                Log.d("TEST","Length of Input " +bis.available());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Log.d("TEST","Length of Input after wait " +bis.available());
                byte[] data = new byte[640*480*5];
                bis.read(data);
                Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, jdata.length);
                if (bmp != null)
                {
                    ivPicture.setImageBitmap(bmp);
                }
                bis.close();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            Log.d("TEST", e.getMessage());
            e.printStackTrace();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            Log.d("TEST", e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.d("TEST", e.getMessage());
            e.printStackTrace();
        }
        }
    });
}

Can someone see what I'm doing wrong? What I have figured out so far is: bis.available() returns never more than 65kb. Although the InputStream itself has the right length (seen in the debugger).

解决方案

available()

will return the number of bytes that con be read from the inputstream without blocking. So it will be return data that could be read from the network without blocking.

try with:

            InputStream bis = new InputStream(connection.getInputStream());
            Log.d("TEST","Length of Input " +bis.available());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            int read = 0;
            byte[] buffer = new byte[SIZE_OF_IMAGE];
            byte[] inBuff = new byte[32678]
            while ((read = bis.read(inBuff, 0, 32768)) > 0) {
                   // copy in buffer what have been read
                   // from the input stream
            }

            // close the is
            Bitmap bmp = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);

obviously is not there are more efficient way to do this but could be an entry point. Let me know if you need more help.

Edit: of course you have to avoid doing blocking call in the UI thread, as people have suggested.

这篇关于HTTP GET请求丢失数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆