在doinbackground期间内存不足

时间:2016-11-15 08:16:17

标签: android android-asynctask out-of-memory

我有以下代码: -

public class ByState extends Fragment {
    @Nullable
    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.by_state, container, false);
        new GetAllStates().execute("dummy");
        return rootView;
    }
}
class GetAllStates extends AsyncTask<String,String,String> {
    @Override
    protected String doInBackground(String... key) {
        StringBuffer response = new StringBuffer();
        try {
            String url = "http://congress-search-148802.appspot.com/getData.php?value=legislator";
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            int responseCode = con.getResponseCode();
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }

            in.close();
        } catch (Exception e) {
            Log.w("Error", e.getMessage());
        }
        return response.toString();
    }
    @Override
    protected void onPostExecute(String groupResponse) {
        Log.d("My message",groupResponse);
        try {
            JSONArray groupList = new JSONArray(groupResponse);
            for(int i=0;i<groupList.length();i++){
                JSONObject groupInfo = groupList.getJSONObject(i);

                Log.d("BioguideID ",groupInfo.getString("bioguide_id"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

我做得不多,但我仍然收到以下错误: -

1-15 00:05:02.793 27120-27157/com.app.congress.congressapp E/art: Throwing OutOfMemoryError "Failed to allocate a 203542 byte allocation with 150292 free bytes and 146KB until OOM"
11-15 00:05:02.803 27120-27157/com.app.congress.congressapp E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: com.app.congress.congressapp, PID: 27120
    java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:304)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:818)
    Caused by: java.lang.OutOfMemoryError: Failed to allocate a 203542 byte allocation with 150292 free bytes and 146KB until OOM
        at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
        at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:125)
        at java.lang.StringBuilder.append(StringBuilder.java:271)
        at java.io.BufferedReader.readLine(BufferedReader.java:414)
        at com.app.congress.congressapp.GetAllStates.doInBackground(ByState.java:46)
        at com.app.congress.congressapp.GetAllStates.doInBackground(ByState.java:34)
        at android.os.AsyncTask$2.call(AsyncTask.java:292)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
        at java.lang.Thread.run(Thread.java:818) 

我曾尝试将堆增加到大堆,这是不推荐的,但我仍然遇到此错误。对我来说这是一个新手可以指出我出错的地方吗?

3 个答案:

答案 0 :(得分:4)

很简单,你试图缓冲太多的数据。

这是你的罪魁祸首:

while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}

由于您的数据源是JSON,因此您只需使用JSONReader逐个解析数据源。通过这种方式,您可以读取所有数据,而无需同时将所有存储在内存中。

有一个使用JSONReader in the official documentation的例子。

在你的情况下,你会像这样初始化它:

JSONReader reader = new JSONReader(new InputStreamReader(con.getInputStream()));

答案 1 :(得分:1)

大堆是一个糟糕的解决方案。而是使用Gson StreamingJackson。或者您也可以使用@knossos答案。

答案 2 :(得分:0)

解析Json也放入doInbackgroud()mehod