Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android doInBackground()-线程挂起-跳过帧中的大量工作_Android_Json_Multithreading_Android Asynctask - Fatal编程技术网

Android doInBackground()-线程挂起-跳过帧中的大量工作

Android doInBackground()-线程挂起-跳过帧中的大量工作,android,json,multithreading,android-asynctask,Android,Json,Multithreading,Android 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 concurren

我有一个简单的异步任务,在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;
}
JSONArray ja=mainJsonObject.getJSONArray(“font”)也是如此


有没有更有效的阅读方法?

这并不是问题的根源,除非你正在做一些愚蠢的事情,比如直接在主线程上调用doInBackground。异步任务在另一个线程上执行。你的问题主要是。嗯……我不知道你的意思。大体上我是这样做的:“我的任务;任务=新的myTask();task.execute();'我没有别的了。这不是调用asynctask的正确方法?这是正确的方法。我的意思是,这项任务不是你的问题,除非它只是使用了大量的内存。你的问题在别的地方。我也这么想。所以我创建了一个新项目,没有更多的东西。只有在main中调用了asyncTask,问题仍然存在。
    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 "";
    }
}