Android API 23/27 for-Loop行为

时间:2017-11-09 21:41:00

标签: java android

所以我在这里使用下载器进行for循环:

**更新了代码 - 在这里你得到PostExecute&状态栏更新的进度

ArrayMap<String, String> urls = new ArrayMap<String, String>();


            urls.put(urlone, "first.xml");
            urls.put(urltwo, "second.xml");
            urls.put(urlthree+"?anid=" + androidId,"score.xml");
            for (int i =0; i < urls.size(); i ++) {
              Log.e("Position i=",String.valueOf(i));
              Log.e("urls size",String.valueOf(urls.size()));
                String urlStr = urls.keyAt(i);
              Log.e("current URL keyAt",urls.keyAt(i));
                OutputStream file = openFileOutput(urls.valueAt(i),Context.MODE_PRIVATE);
              Log.e("file written valueAt",urls.valueAt(i));
                float fPercent = (((float)(i-1))/urls.size())*100.0f;
              Log.e("URL url site", String.valueOf(urls.size()));

                URL url = new URL(urlStr);
                URLConnection ucon = url.openConnection();
                int lengthOfFile = ucon.getContentLength();

                InputStream is = ucon.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                //Log.d("SPLASH LOGGER", "Got InputStream and BufferedInputStream");
                BufferedOutputStream bos = new BufferedOutputStream(file);
                //Log.d("SPLASH LOGGER", "Got FileOutputStream and BufferedOutputStream");


                byte data[] = new byte[1024];
                long total = 0;
                int count;
                //loop and read the current chunk
                while ((count = bis.read(data)) != -1) {
                    //keep track of size for progress.
                    total += count;
                    publishProgress(fPercent + (((float)total)/lengthOfFile)*(100.0f - fPercent));
                    bos.write(data, 0, count);

                }
                //Have to call flush or the file can get corrupted.
                bos.flush();
                bos.close();
            }



        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

        return null;

    }

    @Override
    protected void onProgressUpdate(Float... values) {
        TextView splashstatus = (TextView) findViewById(R.id.spstatus);
        if (values[0]>0)
        {
            splashstatus.setText(String.valueOf(values[0]));
        }

        //Log.i("String.valueOf values", String.valueOf(values[0]));
    }
    @Override
    protected void onPostExecute(Void result) {
        TextView splashstatus = (TextView) findViewById(R.id.spstatus);
        splashstatus.setText("DONE...");
        splashTread.start();
    }

    @Override
    protected void onPreExecute()
    {TextView splashstatus = (TextView) findViewById(R.id.spstatus);
        splashstatus.setText("UPDATING DATA");
    }


}

所以我的问题如下:当我在Android 7.0(Api 27)上启动此程序时,一切正常,但在Android 6.x(API 23)上下载,下载器仅下载urltwo和urlthree - first.xml不在手机上,而相同的代码在更高版本的android上完美运行。 我没有想法解决这个问题,是否有可能为api 23 ..我必须从i = 1开始?

LOG CAT:

11-10 16:54:49.583 5075-5121/? E/Position i=: 0
11-10 16:54:49.583 5075-5121/? E/urls size: 3
11-10 16:54:49.583 5075-5121/? E/current URL keyAt: ***URLTHREE***
11-10 16:54:49.583 5075-5121/? E/file written valueAt: score.xml
11-10 16:54:49.584 5075-5121/? E/URL url site: 3

API级别27上的LOG CAT(相同代码,一切正常)

E/Position i=: 0
E/urls size: 3
E/current URL keyAt: ** URL TWO 2 HERE **
E/file written valueAt: second.xml
E/URL url site: 3
E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
E/Position i=: 1
E/urls size: 3
E/current URL keyAt: **URL ONE 1 **
E/file written valueAt: first.xml
E/URL url site: 3
E/Position i=: 2
E/urls size: 3
E/current URL keyAt: **URL THREE 3 **
E/file written valueAt: score.xml
E/URL url site: 3

关注这个Logcat,我没有改变顺序,当我复制&amp;糊。它会让人感到困惑,但它确实有效。

0 个答案:

没有答案