遇到一个奇怪的问题,我每20秒就会触发一次意图服务: 通过截击击中api 当应用程序处于调试模式时,它可以正常工作并获得结果。否则它根本不起作用 请有人帮我找到解决方案。
public class DataSubmissionService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
gpsTracker_gps = new GPSTracker(getApplicationContext(), Constant.GPS);
if (Utils.isNetworkAvailable(getApplicationContext())){
init();
}else {
logUtils.writeToLogFile("Network not available");
}
}
private void fetchinfofromTracker(GPSTracker gpsTracker) {
latitude = String.valueOf(gpsTracker.latitude);
Log.d(TAG, "Latitude :" + latitude);
longitude = String.valueOf(gpsTracker.longitude);
Log.d(TAG, "Longitude :" + longitude);
elevation = String.valueOf(gpsTracker.altitude);
speed = String.valueOf(gpsTracker.speed);
accuracy = String.valueOf(gpsTracker.accuracy);
provider = gpsTracker.provider;
bearing = String.valueOf(gpsTracker.bearing);
model.setLatitude(latitude);
model.setLongitude(longitude);
model.setAccuracy(accuracy);
model.setProvider(provider);
model.setBearing(bearing);
model.setTimeStamp(getCurrentDateTime());
// callAlarmManager();
// logUtils.logText("GPS Values:" + "Latitude" +latitude + " Longitude" + longitude );
if (latitude.equals("0.0")) {
latitude = "0.0";
longitude = "0.0";
model.setLatitude(latitude);
model.setLongitude(longitude);
} else {
Intent intent = new Intent(BROADCAST_ACTION);
intent.putExtra("latitude", latitude);
intent.putExtra("longitude", longitude);
intent.putExtra("speed", speed);
intent.putExtra("accuracy", accuracy);
intent.putExtra("bearing", bearing);
intent.putExtra("provider", provider);
intent.putExtra("elevation", elevation);
sendBroadcast(intent);
// insertdata();
sendToServer(model);
}
}
private void sendVolleyRequest(JSONObject jsonObject) {
try {
logUtils.logText("Connecting to server");
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = Constant.BASE_URL;
JSONObject jsonBody = jsonObject;
final String requestBody = jsonBody.toString();
JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.POST, URL, null,
new Response.Listener<JSONArray>()
{
@Override
public void onResponse(JSONArray response) {
// display response
responseListener(response.toString(),resultCode);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", error.toString());
logUtils.writeToLogFile(error.toString());
}
}
){
@Override
public String getBodyContentType() {
return "application/json";
}
@Override
public byte[] getBody() {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Accept", "application/json");
headers.put("Content-Type", "application/json");
return headers;
}
};
getRequest.setRetryPolicy(new DefaultRetryPolicy(
TIME_OUT,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(getRequest);
} catch (Exception e) {
e.printStackTrace();
}
public void responseListener(String res, int resultCode) {
Log.e(TAG,res);
//Log.e("responseFromFeecal..", res);
//Log.e("feeResultCode", "" + resultCode);
//Log.e("CMR", "Server response: " + res);
int maxLogSize = 1000;
for (int i = 0; i <= res.length() / maxLogSize; i++) {
int start = i * maxLogSize;
int end = (i + 1) * maxLogSize;
end = end > res.length() ? res.length() : end;
Log.v("lt_response", res.substring(start, end));
}
logUtils.logText("Server Response: " + res);
if (res.contains("100") || (res.contains("Success"))) {
/* Toast.makeText(getApplicationContext(), "Success" ,
Toast.LENGTH_SHORT).show();*/
String guidval = "";
String status = "";
try {
JSONArray jsonArray = new JSONArray(res);
JSONObject jsonObject = jsonArray.getJSONObject(0);
JSONArray idStr = jsonObject.getJSONArray("ReceivedGuidKeys");
JSONObject key_value = idStr.getJSONObject(0);
String key_c = key_value.getString("key");
guidval = key_c;
String status_c = key_value.getString("status");
status = status_c;
System.out.println("Status: " + status_c + " GUIDKey: " + key_c);
} catch (JSONException e) {
e.printStackTrace();
logUtils.logText(e.getMessage());
}
if (status.equalsIgnoreCase(Constant.FALSE)) {
//SendtoServer(guidval, Constant.PENDINGITEM);
logUtils.logText("Server returns false for " + guidval);
System.out.print("Server returns false for " + guidval);
}
} else {
/*DBHelper db = new DBHelper(getApplicationContext());
db.logRecordCounts();*/
logUtils.logText("Server failed to respond");
}
}
}
在进行调试的同时,它始终会击中api并给予我完美的响应