doInBackground()中的大量工作 - 线程暂停 - 跳过的帧

时间:2017-01-18 21:15:33

标签: android json multithreading android-asynctask

我有一个简单的asynctask和doInBackground()。问题是,在json中至少需要做很多工作。 35000个节点

然后我有:

I/Choreographer: Skipped 41 frames!  The application may be doing too much work on its main thread.

W/art: Suspending all threads took: 33.923ms

I/art: Background sticky concurrent mark sweep GC freed 20(672B) AllocSpace objects, 1(9MB) LOS objects, 11% free, 47MB/53MB, paused 2.674ms total 102.623ms

W/art: Suspending all threads took: 20.574ms

I/art: Background partial concurrent mark sweep GC freed 65195(2MB) AllocSpace objects, 20(11MB) LOS objects, 28% free, 40MB/56MB, paused 20.163ms total 89.164ms

I/art: Background sticky concurrent mark sweep GC freed 166274(6MB) AllocSpace objects, 0(0B) LOS objects, 10% free, 50MB/56MB, paused 1.755ms total 118.003ms

I/art: Background partial concurrent mark sweep GC freed 68708(2MB) AllocSpace objects, 0(0B) LOS objects, 22% free, 54MB/70MB, paused 2.229ms total 134.658ms

I/art: Background sticky concurrent mark sweep GC freed 174470(6MB) AllocSpace objects, 0(0B) LOS objects, 7% free, 64MB/70MB, paused 2.596ms total 163.746ms

W/art: Suspending all threads took: 48.574ms

这里是我的asynctask doInBackground()。我使用的是真正的设备......不是模拟器。我不知道该怎么做,因为我需要用json做其他事情。我如何避免线程暂停和所有警告和跳帧?

@Override
protected ArrayList<MarkerOptions> doInBackground(Void... params) {

    try {
        url = new URL("http://xxxxxxxx.php");
        urlConnection = (HttpURLConnection) url.openConnection();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        jsonString=readStream(in);


    }catch(IOException e){
        e.printStackTrace();
    }
    finally {
        urlConnection.disconnect();
    }


    try {
        mainJsonObject = new JSONObject(jsonString);

        JSONArray ja = mainJsonObject.getJSONArray("font");
        Log.d("fontanelle",ja.length()+"");

    } catch (JSONException e) {
        e.printStackTrace();
    }

    return null;
}

我想我找到了问题。

方法read()花了很多时间来处理所有信息。这是方法:

    private String readStream(InputStream is) {
    try {
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
       Log.d("time", SystemClock.currentThreadTimeMillis()+"  startRead");
        int i = is.read();

        while (i != -1) {
            bo.write(i);
            i = is.read();
        }
     Log.d("time", SystemClock.currentThreadTimeMillis()+"  StopRead");

        return bo.toString();
    } catch (IOException e) {
        return "";
    }
}

JSONArray ja = mainJsonObject.getJSONArray(“font”);

有更有效的阅读方式吗?

0 个答案:

没有答案