outofmemoryerror toString(“UTF-8”)ByteArrayOutputStream在android中

时间:2013-03-28 11:33:55

标签: android out-of-memory

这是日志:

03-28 19:12:29.083: E/AndroidRuntime(22004): FATAL EXCEPTION: AsyncTask #5
03-28 19:12:29.083: E/AndroidRuntime(22004): java.lang.RuntimeException: An error occured while executing doInBackground()
03-28 19:12:29.083: E/AndroidRuntime(22004):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
03-28 19:12:29.083: E/AndroidRuntime(22004):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-28 19:12:29.083: E/AndroidRuntime(22004):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-28 19:12:29.083: E/AndroidRuntime(22004):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-28 19:12:29.083: E/AndroidRuntime(22004):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-28 19:12:29.083: E/AndroidRuntime(22004):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-28 19:12:29.083: E/AndroidRuntime(22004):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-28 19:12:29.083: E/AndroidRuntime(22004):    at java.lang.Thread.run(Thread.java:856)
03-28 19:12:29.083: E/AndroidRuntime(22004): Caused by: java.lang.OutOfMemoryError
03-28 19:12:29.083: E/AndroidRuntime(22004):    at java.lang.String.<init>(String.java:364)
03-28 19:12:29.083: E/AndroidRuntime(22004):    at java.lang.String.<init>(String.java:223)
03-28 19:12:29.083: E/AndroidRuntime(22004):    at java.io.ByteArrayOutputStream.toString(ByteArrayOutputStream.java:175)
03-28 19:12:29.083: E/AndroidRuntime(22004):    at com.vtube.data.YoutubeAPI.doGetRequest(YoutubeAPI.java:71)
03-28 19:12:29.083: E/AndroidRuntime(22004):    at com.vtube.data.YoutubeAPI.getPopular(YoutubeAPI.java:86)
03-28 19:12:29.083: E/AndroidRuntime(22004):    at com.vtube.data.YoutubeAPI.getPopularEntry(YoutubeAPI.java:97)
03-28 19:12:29.083: E/AndroidRuntime(22004):    at com.vtube.app.BaseFourActivity$PageZeroRefreshTask.doInBackground(BaseFourActivity.java:358)
03-28 19:12:29.083: E/AndroidRuntime(22004):    at com.vtube.app.BaseFourActivity$PageZeroRefreshTask.doInBackground(BaseFourActivity.java:1)
03-28 19:12:29.083: E/AndroidRuntime(22004):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
03-28 19:12:29.083: E/AndroidRuntime(22004):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-28 19:12:29.083: E/AndroidRuntime(22004):    ... 4 more

我不明白为什么会出错,这个应用程序不会经常崩溃,只有几次。当我离开应用程序一段时间时会发生这种情况。 代码出错的地方:

public static String doGetRequest(String url) throws IOException{
        HttpClient lClient = new DefaultHttpClient();
        HttpGet lGetMethod = new HttpGet(url);

        HttpResponse lResp = null;
        try {
            lResp = lClient.execute(lGetMethod);
        } catch (ClientProtocolException e1) {
            Log.v(TAG, e1.toString());
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        ByteArrayOutputStream lBOS = new ByteArrayOutputStream();
        String lInfoStr = "";

        if (lResp != null){
            try {
                lResp.getEntity().writeTo(lBOS);
                lInfoStr = lBOS.toString("UTF-8");
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return lInfoStr;
    }

lInfoStr = lBOS.toString("UTF-8");这条特殊线

我该如何解决这个问题? toString()会花费大量内存吗?

0 个答案:

没有答案