我正在尝试使用凌空库将加速度计x y z值发送到服务器, 但是执行了一段时间后,它显示了以下问题
2019-03-23 11:34:42.610 9772-9977 / com.example.acer.smarhelmetmain D / Volley:?> [4173] BasicNetwork.logSlowRequests:请求的HTTP响应= <[]> {{3} } 0x67692a02 NORMAL [lifetime = 9613],[size = 0],[rc = 200],[retryCount = 0]
并使应用崩溃,并显示“请重试”错误
我使用了CountDownTimer并设置了每5秒执行一次的请求,如下所示
package com.example.acer.smarhelmetmain.service;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.acer.smarhelmetmain.activity.AlertActivity;
import com.example.acer.smarhelmetmain.util.DBHandle;
import com.example.acer.smarhelmetmain.util.SPHandle;
import java.util.HashMap;
import java.util.Map;
public class SensorService extends Service implements SensorEventListener {
String url;
String imei;
private SensorManager sensorManager;
CountDownTimer countDownTimer;
public SensorService() {
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
url = "http://" + new SPHandle(this).getIP() + "/helmet/sensor_details.php";
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
imei = telephonyManager.getDeviceId();
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
countDownTimer = new CountDownTimer(Long.MAX_VALUE,5000) {
@Override
public void onTick(long millisUntilFinished) {
checkForStatus();
}
@Override
public void onFinish() {
start();
}
}.start();
}
private void checkForStatus() {
String c_url="http://"+new SPHandle(getApplicationContext()).getIP()+"/helmet/getHelmetStatus.php?imei="+new SPHandle(getApplicationContext()).getIMEI();
RequestQueue requestQueue=Volley.newRequestQueue(this);
StringRequest request=new StringRequest(Request.Method.GET, c_url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
crashDetection(response);
if(response.equals("1"))
{
countDownTimer.cancel();
}
else
{
countDownTimer.start();
}
Log.d("CRASHDETECTION", "onResponse: "+response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(request);
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
getAccelerometer(event);
}
}
private void getAccelerometer(SensorEvent event) {
float[] values = event.values;
// Movement
final float x = values[0];
final float y = values[1];
final float z = values[2];
CountDownTimer countDownTimer=new CountDownTimer(Long.MAX_VALUE,5000) {
@Override
public void onTick(long millisUntilFinished) {
insertToDB(x,y,z);
}
@Override
public void onFinish() {
start();
}
}.start();
}
private void crashDetection(String status) {
if(status.equals("1"))
{
startActivity(new Intent(getApplicationContext(),AlertActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
}
private void insertToDB(float x, float y, float z) {
final HashMap<String,String> param = new HashMap<>();
param.put("x",String.valueOf(x));
param.put("y",String.valueOf(y));
param.put("z",String.valueOf(z));
param.put("imei",imei);
RequestQueue requestQueue=Volley.newRequestQueue(this);
StringRequest req=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return param;
}
};
requestQueue.add(req);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
我希望每5秒钟将数据发送到服务器,而不会出现任何问题。
这是我在运行代码时遇到的错误,应用程序崩溃了
2019-03-23 15:39:06.408 7589-7595/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:06.408 7589-7595/com.example.acer.smarhelmetmain A/libc: Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 7595 (Jit thread pool), pid 7589 (.smarhelmetmain)
2019-03-23 15:39:07.959 7589-14240/com.example.acer.smarhelmetmain D/Volley: [11014] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://192.168.1.39:8088/helmet/getHelmetStatus.php?imei=864558040157556 0x67692a02 NORMAL 1> [lifetime=3007], [size=1], [rc=200], [retryCount=0]
2019-03-23 15:39:07.959 7589-14261/com.example.acer.smarhelmetmain D/Volley: [11035] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://192.168.1.39:8088/helmet/getHelmetStatus.php?imei=864558040157556 0x67692a02 NORMAL 1> [lifetime=3001], [size=1], [rc=200], [retryCount=0]
2019-03-23 15:39:07.959 7589-14264/com.example.acer.smarhelmetmain D/Volley: [11038] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://192.168.1.39:8088/helmet/getHelmetStatus.php?imei=864558040157556 0x67692a02 NORMAL 1> [lifetime=3005], [size=1], [rc=200], [retryCount=0]
2019-03-23 15:39:07.959 7589-14269/com.example.acer.smarhelmetmain D/Volley: [11043] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://192.168.1.39:8088/helmet/getHelmetStatus.php?imei=864558040157556 0x67692a02 NORMAL 1> [lifetime=3001], [size=1], [rc=200], [retryCount=0]
2019-03-23 15:39:07.960 7589-14254/com.example.acer.smarhelmetmain D/Volley: [11028] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://192.168.1.39:8088/helmet/getHelmetStatus.php?imei=864558040157556 0x67692a02 NORMAL 1> [lifetime=3012], [size=1], [rc=200], [retryCount=0]
2019-03-23 15:39:07.960 7589-14249/com.example.acer.smarhelmetmain D/Volley: [11023] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://192.168.1.39:8088/helmet/getHelmetStatus.php?imei=864558040157556 0x67692a02 NORMAL 1> [lifetime=3010], [size=1], [rc=200], [retryCount=0]
2019-03-23 15:39:07.961 7589-14192/com.example.acer.smarhelmetmain D/Volley: [10968] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://192.168.1.39:8088/helmet/getHelmetStatus.php?imei=864558040157556 0x67692a02 NORMAL 1> [lifetime=3015], [size=1], [rc=200], [retryCount=0]
2019-03-23 15:39:07.961 7589-14229/com.example.acer.smarhelmetmain D/Volley: [11004] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://192.168.1.39:8088/helmet/getHelmetStatus.php?imei=864558040157556 0x67692a02 NORMAL 1> [lifetime=3019], [size=1], [rc=200], [retryCount=0]
2019-03-23 15:39:07.963 7589-15207/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.963 7589-14292/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.963 7589-14518/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.963 7589-15235/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.966 7589-15362/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.967 7589-15344/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.969 7589-15157/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.970 7589-15096/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.970 7589-15384/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.972 7589-15100/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.973 7589-14312/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.975 7589-15252/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.976 7589-15214/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.976 7589-15354/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.977 7589-15179/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.977 7589-15342/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.977 7589-14307/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.977 7589-15324/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.979 7589-15334/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.979 7589-15219/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.979 7589-14324/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.981 7589-14282/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.981 7589-14322/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.981 7589-15365/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.981 7589-15173/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.981 7589-14277/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.982 7589-14546/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.982 7589-15105/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.982 7589-14532/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.983 7589-15209/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.984 7589-15389/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.988 7589-14284/com.example.acer.smarhelmetmain E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
2019-03-23 15:39:07.991 7589-14150/com.example.acer.smarhelmetmain D/Volley: [10930] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://192.168.1.39:8088/helmet/getHelmetStatus.php?imei=864558040157556 0x67692a02 NORMAL 1> [lifetime=3198], [size=1], [rc=200], [retryCount=0]
2019-03-23 15:39:08.134 7589-14300/com.example.acer.smarhelmetmain D/Volley: [11074] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://192.168.1.39:8088/helmet/getHelmetStatus.php?imei=864558040157556 0x67692a02 NORMAL 1> [lifetime=3168], [size=1], [rc=200], [retryCount=1]