读不正确的值?

时间:2013-05-28 10:17:54

标签: java android

我有以下类,扩展了Thread。 我的想法是提取线程内的日期,一切顺利,直到收到的数据大于几千字节的时候,然后我开始读取完整的错误数据。

public class ThreadBooksPositions extends Thread
{

    public ThreadBooksPositions()
    {
    }
    ..
    // default constructors

    public void run()
    {
        InputStream       iSS           = null;
        HttpURLConnection connection    = null;
        Integer sectionsDescriptorSize1 = 0;
        Integer sectionsDescriptorSize2 = 0;

        try
        {

            URL url = new URL( "192.168.1.4/bookstore.asp?getbooks" );
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod( "GET" );
            connection.connect();

            iSS = connection.getInputStream();

            BufferedInputStream bIS = new BufferedInputStream( iSS );

            if( bIS.available() > 4 )
            {
                Float   lat           = 0F;
                Float   lng           = 0F;

                ByteArrayOutputStream out = new ByteArrayOutputStream();

                byte[] bf;
                try
                {
                    bf = new byte[ bIS.available() ];

                    while ( bIS.read( bf ) != -1) 
                        out.write( bf ); //copy streams

                    out.flush();
                }
                catch ( IOException e )
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } //you can configure the buffer size

                byte[] bO = out.toByteArray();

                if( out != null )
                {
                    try
                    {
                        out.close();
                    }
                    catch ( IOException e )
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                ByteBuffer data = ByteBuffer.wrap( bO );

                sectionsDescriptorSize1 = data.getInt();
                sectionsDescriptorSize2 = data.getInt();

                ByteBuffer sectionData;

                try
                {
                    if( sectionsDescriptorSize1 > 0 )
                    {
                        byte[] bAS0 = new byte[ sectionsDescriptorSize1 ];
                        data.get( bAS0 );
                    }

                    if( sectionsDescriptorSize2 > 1 )
                    {
                        // trajectory
                        byte[] bAS1 = new byte[ sectionsDescriptorSize2 ];
                        data.get( bAS1, 0, sectionsDescriptorSize2 );

                        sectionData = ByteBuffer.wrap( bAS1 );

                        Boolean readingFailed = true;

                        if( sectionData != null )
                        {
                            while( sectionData.available() > 1 )
                            {
                                try
                                {
                                    readingFailed = false;
                                    lat           = sectionData.getFloat();      // 4
                                    lng           = sectionData.getFloat();      // 4

                                }
                                catch( Exception e )
                                {
                                    readingFailed = true;
                                }

                                try
                                {
                                    if( readingFailed == false )
                                    {
                                        addBookStorePosition( lat, lng );
                                    }
                                }
                                catch (Exception e) 
                                {
                                }
                            }
                        }

                }
                catch( Error e )
                {
                }
            }
        }
        catch( IOException e )
        {

        }
        finally
        {
            if( iSS != null )
            {
                try
                {
                    iSS.close();
                }
                catch( IOException e )
                {

                }           
            }

            if( connection != null )
            {
                connection.disconnect();
            }
        }
    }

}
  • 什么可能导致读取不正确的数据?

1 个答案:

答案 0 :(得分:1)

发现了这个问题。 似乎 .available()导致了这个问题,特别是在线程中。