为什么从警报管理器调用的服务调用 HttpUrlConnection Post 调用时给出 java.io.FileNotFoundException?
Why does HttpUrlConnection Post call gives java.io.FileNotFoundException when calling it from a service invoked by alarm manager?
详情:
在我的主要 Activity 中,我使用以下代码创建了一个警报:
Intent myIntent1 = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent1,PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), constants.INTERVAL , pendingIntent);
然后在 AlarmReceiver 中,我使用以下代码调用服务:
@Override
public void onReceive(final Context context, Intent intent) {
Intent in = new Intent(context, LocationPoll.class);
context.startService(in);
}
下面是 LocationPoll.java,它扩展服务并实现位置侦听器,其中 onLocationChanged following.I 使用 GoogleApiClient 接收 updates.I 只需传递 json数据并期望 json 响应。
@Override
public void onLocationChanged(Location location) {
String url = constants.BASE_URL+constants.TRACK_URL;
JSONObject jsonBody = new JSONObject();
try {
jsonBody.put("latitude", location.getLatitude());
jsonBody.put("longitude",location.getLongitude());
jsonBody.put("driver_id", constants.DRIVERID);
}
catch(Exception e ){
}
JSONRequest jr = new JSONRequest(jsonBody,url,this);
jr.execute();
}
JSONRequest Asynct 任务执行以下操作以使用 HttpUrlConnection 进行 post 调用:
URL urlConnection = new URL(url);
System.setProperty("http.keepAlive", "false");
connection = (HttpURLConnection) urlConnection
.openConnection();
connection.setDoOutput(true);
connection.setChunkedStreamingMode(0);
connection.setDoInput(true);
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.setRequestProperty("Accept-Encoding","gzip, deflate");
connection.setRequestProperty("Accept-Language", "en-US,en;q=0.8,hi;q=0.6");
connection.setRequestProperty("Connection", "close");
connection.setRequestMethod("POST");
OutputStream os = connection.getOutputStream();
os.write(data.toString().getBytes("UTF-8"));
os.close();
connection.connect();
_map= connection.getHeaderFields();
responsecode = connection.getResponseCode();
当应用程序处于前台或后台状态时,以上内容工作正常。但是当应用程序处于终止状态时,位置更新失败并出现错误:java.io.FileNotFoundException。请协助。
Logcat:
10-14 18:50:03.806 17841-18041/? W/System.err﹕ java.io.FileNotFoundException: http://example.com
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:197)
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at java.net.URLConnection$DefaultContentHandler.getContent(URLConnection.java:1017)
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at java.net.URLConnection.getContent(URLConnection.java:199)
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at com.bikeninja.driver_app.asynctasks.JSONRequest.doInBackground(JSONRequest.java:70)
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at com.bikeninja.driver_app.asynctasks.JSONRequest.doInBackground(JSONRequest.java:23)
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at android.os.AsyncTask.call(AsyncTask.java:288)
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-14 18:50:03.809 17841-18041/? W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
仅供参考。在我的情况下,当应用程序处于终止状态时,我传递的 JSON 数据的字段之一为空。原因是我曾经从应用程序中的常量文件中读取,当应用程序被杀死时该文件不可用。因此,用于从 server.The 获取 404 的解决方案是从 SharedPreferences 读取它。
详情:
在我的主要 Activity 中,我使用以下代码创建了一个警报:
Intent myIntent1 = new Intent(MainActivity.this, AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent1,PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), constants.INTERVAL , pendingIntent);
然后在 AlarmReceiver 中,我使用以下代码调用服务:
@Override public void onReceive(final Context context, Intent intent) { Intent in = new Intent(context, LocationPoll.class); context.startService(in); }
下面是 LocationPoll.java,它扩展服务并实现位置侦听器,其中 onLocationChanged following.I 使用 GoogleApiClient 接收 updates.I 只需传递 json数据并期望 json 响应。
@Override public void onLocationChanged(Location location) { String url = constants.BASE_URL+constants.TRACK_URL; JSONObject jsonBody = new JSONObject(); try { jsonBody.put("latitude", location.getLatitude()); jsonBody.put("longitude",location.getLongitude()); jsonBody.put("driver_id", constants.DRIVERID); } catch(Exception e ){ } JSONRequest jr = new JSONRequest(jsonBody,url,this); jr.execute(); }
JSONRequest Asynct 任务执行以下操作以使用 HttpUrlConnection 进行 post 调用:
URL urlConnection = new URL(url); System.setProperty("http.keepAlive", "false"); connection = (HttpURLConnection) urlConnection .openConnection(); connection.setDoOutput(true); connection.setChunkedStreamingMode(0); connection.setDoInput(true); connection.setRequestProperty("Accept", "*/*"); connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); connection.setRequestProperty("Accept-Encoding","gzip, deflate"); connection.setRequestProperty("Accept-Language", "en-US,en;q=0.8,hi;q=0.6"); connection.setRequestProperty("Connection", "close"); connection.setRequestMethod("POST"); OutputStream os = connection.getOutputStream(); os.write(data.toString().getBytes("UTF-8")); os.close(); connection.connect(); _map= connection.getHeaderFields(); responsecode = connection.getResponseCode();
当应用程序处于前台或后台状态时,以上内容工作正常。但是当应用程序处于终止状态时,位置更新失败并出现错误:java.io.FileNotFoundException。请协助。
Logcat:
10-14 18:50:03.806 17841-18041/? W/System.err﹕ java.io.FileNotFoundException: http://example.com
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:197)
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at java.net.URLConnection$DefaultContentHandler.getContent(URLConnection.java:1017)
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at java.net.URLConnection.getContent(URLConnection.java:199)
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at com.bikeninja.driver_app.asynctasks.JSONRequest.doInBackground(JSONRequest.java:70)
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at com.bikeninja.driver_app.asynctasks.JSONRequest.doInBackground(JSONRequest.java:23)
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at android.os.AsyncTask.call(AsyncTask.java:288)
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-14 18:50:03.807 17841-18041/? W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-14 18:50:03.809 17841-18041/? W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
仅供参考。在我的情况下,当应用程序处于终止状态时,我传递的 JSON 数据的字段之一为空。原因是我曾经从应用程序中的常量文件中读取,当应用程序被杀死时该文件不可用。因此,用于从 server.The 获取 404 的解决方案是从 SharedPreferences 读取它。