无法分配JNI环境

时间:2017-04-07 02:33:21

标签: android android-volley

我每隔2秒更新一次我的API,
但我收到此错误,我的申请结束。

  

致命的例外:主要                                                                              处理:com.application.toweeloasep,PID:6681                                                                              java.lang.OutOfMemoryError:无法分配JNI环境                                                                                  at java.lang.Thread.nativeCreate(Native Method)                                                                                  在java.lang.Thread.start(Thread.java:730)                                                                                  在com.android.volley.RequestQueue.start(RequestQueue.java:145)                                                                                  在com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:66)                                                                                  在com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:78)                                                                                  在com.application.toweeloasep.fragments.Jobs $ 5 $ 1.run(Jobs.java:260)                                                                                  在android.os.Handler.handleCallback(Handler.java:751)                                                                                  在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                  在android.os.Looper.loop(Looper.java:154)                                                                                  在android.app.ActivityThread.main(ActivityThread.java:6077)                                                                                  at java.lang.reflect.Method.invoke(Native Method)                                                                                  在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                                                                                  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

大约一分钟后,它开始崩溃并收到该错误 我是否对Volley过度了?

private void setRepeatingAsyncTask() {
        final Handler handler = new Handler();
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                        try {
                            if (checkRequests) {
                                RequestQueue mRequestQueue = Volley.newRequestQueue(getActivity());
                                StringRequest mStringRequest = new StringRequest(Request.Method.POST, "http://api.000.com/booking/track", new Response.Listener<String>() {
                                    @Override
                                    public void onResponse(String response) {
                                        Log.e("RESPONSE:TRACK", response);
                                        try {
                                            JSONObject json = new JSONObject(response);
                                            String mStatus = json.getString("status");
                                            if (mStatus.equalsIgnoreCase("0")) {
                                                Log.e("STATUS", mStatus);
                                            } else if (mStatus.equalsIgnoreCase("1")) {
                                                JSONArray infos = json.getJSONArray("data");
                                                booking_id = infos.getJSONObject(0).getString("id");
                                                user_address_location = infos.getJSONObject(0).getString("user_address_location");
                                                mTxtBatteryInfo.setText(infos.getJSONObject(0).getJSONObject("battery").getString("model"));
                                                mTxtUserLocation.setText(user_address_location);
                                                checkRequests = false;
                                                mJobsHome.setVisibility(View.GONE);
                                                mJobRequest.setVisibility(View.VISIBLE);
                                                if (!onTick) {
                                                    mCountDownTimer.start();
                                                    onTick = true;
                                                }
                                            }
                                        } catch (Exception e) {
                                            Log.e("ERR", e.toString());
                                        }
                                    }
                                }, new Response.ErrorListener() {
                                    @Override
                                    public void onErrorResponse(VolleyError error) {
                                        Log.e("ERR", error.toString());
                                    }
                                });
                                mRequestQueue.add(mStringRequest);
                            } else {
                                RequestQueue requestPlaceInfo = Volley.newRequestQueue(getActivity());
                                StringRequest request2 = new StringRequest(Request.Method.POST, "http://api.000.com/booking/track", new Response.Listener<String>() {
                                    @Override
                                    public void onResponse(String response) {
                                        Log.e("RESPONSE", response);
                                    }
                                }, new Response.ErrorListener() {
                                    @Override
                                    public void onErrorResponse(VolleyError error) {
                                        Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_SHORT).show();
                                    }
                                }) {
                                    @Override
                                    protected Map<String, String> getParams() throws AuthFailureError {
                                        Map<String, String> params = new HashMap<>();
                                        params.put("booking_id", booking_id);
                                        params.put("rider_long", String.valueOf(lng));
                                        params.put("rider_lat", String.valueOf(lat));
                                        return params;
                                    }
                                };
                                requestPlaceInfo.add(request2);
                            }
                        } catch (Exception e) {
                            // error, do something
                        }
                    }
                });
            }
        };
        timer.schedule(task, 0, 2000);  // interval of one minute
    }

1 个答案:

答案 0 :(得分:1)

  

OutOfMemoryError ...在com.android.volley.toolbox.Volley.newRequestQueue

为什么需要每两秒创建一个新的RequestQueue或StringRequest?

尝试只制作其中一个。

private final Response.ErrorListener errorListener = new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e("ERR", String.valueOf(error));
    }
};

private void setRepeatingVolleyTask() {
    final RequestQueue mRequestQueue = Volley.newRequestQueue(getActivity());

    final StringRequest trackRequest = new StringRequest(Request.Method.POST, "http://api.toweelo.com/booking/track", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("RESPONSE:TRACK", response);
            try {
                JSONObject json = new JSONObject(response);
                String mStatus = json.getString("status");
                if (mStatus.equalsIgnoreCase("0")) {
                    Log.d("STATUS", mStatus);
                } else if (mStatus.equalsIgnoreCase("1")) {
                    JSONArray infos = json.getJSONArray("data");
                    booking_id = infos.getJSONObject(0).getString("id");
                    user_address_location = infos.getJSONObject(0).getString("user_address_location");
                    mTxtBatteryInfo.setText(infos.getJSONObject(0).getJSONObject("battery").getString("model"));
                    mTxtUserLocation.setText(user_address_location);
                    checkRequests = false;
                    mJobsHome.setVisibility(View.GONE);
                    mJobRequest.setVisibility(View.VISIBLE);
                    if (!onTick) {
                        mCountDownTimer.start();
                        onTick = true;
                    }
                }
            } catch (Exception e) {
                Log.e("ERR", e.toString());
            }
        }
    }, errorListener );

    final StringRequest trackRequest2 = new StringRequest(Request.Method.POST, "http://api.toweelo.com/booking/track", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("RESPONSE", response);
        }
    }, errorListener) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("booking_id", booking_id);
            params.put("rider_long", String.valueOf(lng));
            params.put("rider_lat", String.valueOf(lat));
            return params;
        }
    };

    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                public void run() {
                    // What are you trying to catch here??
                    try {
                        if (checkRequests) {
                            mRequestQueue.add(trackRequest);
                        } else {
                             mRequestQueue.add(trackRequest2);
                    } catch ( ... ) {
                        ....
                    }

注意:您可以使用JsonObjectRequest而不是从StringRequest

解析JSON