'Android not make GET request after app closing from Service
First of all sorry if such sort of question is already asked before.
I want to make a app which make a GET request
or Simply Open A URL
to tell server that app is closed now, no output is needed just load URL.
What I already tried:
I used
Volley
but unfortunately its not a solution in this case.I also tried to make
AsyncTask
but seriously friend its weird its also not working
Only what's work is a simple line of code:
URL url = new URL("URL HERE");
InputStream stream = url.openStream();
but cause NetworkException
. I just want to Execute a URL from Service after app closing.
Sample Codes Will Appreciate
THanks In Advance
Update Section
public class Service extends android.app.Service {
public int isExecuted = 0;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("rrLOG", "Service Started");
checkClosing();
return Service.START_NOT_STICKY;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Log.e("rrLOG", "Service Stopped");
updateServer(UPDATE_OFFLINE);
stopSelf();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("rrLOG", "Service Destroyed");
updateServer(UPDATE_OFFLINE);
stopSelf();
}
public void checkClosing(){
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (Helper.isAppRunning(Service.this, getPackageName())) {
Log.i("rrLOG", "App Is Running");
} else {
Log.i("rrLOG", "App Is Not Running");
updateServer(UPDATE_OFFLINE);
stopSelf();
}
}
}, 0, 500);//put here time 1000 milliseconds=1 second
}
static class RequestTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
responseString = out.toString();
out.close();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//Do anything with response..
}
}
public void updateServer(final String urlJsonArry) {
Log.i("rrLOG", urlJsonArry);
new RequestTask().execute(urlJsonArry);
}
}
UPDATE2 Section
public class Service extends android.app.Service {
public int isExecuted = 0;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("rrLOG", "Service Started");
checkClosing();
return Service.START_NOT_STICKY;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
//super.onTaskRemoved(rootIntent);
Log.e("rrLOG", "Service Stopped");
updateServer(UPDATE_OFFLINE);
}
@Override
public void onDestroy() {
//super.onDestroy();
Log.e("rrLOG", "Service Destroyed");
updateServer(UPDATE_OFFLINE);
}
public void checkClosing(){
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (Helper.isAppRunning(Service.this, getPackageName())) {
Log.i("rrLOG", "App Is Running");
} else {
Log.i("rrLOG", "App Is Not Running");
updateServer(UPDATE_OFFLINE);
}
}
}, 0, 500);//put here time 1000 milliseconds=1 second
}
public class NetworkTask extends AsyncTask<Void,Void,String> {
@Override
protected String doInBackground(Void... voids) {
Log.i("rrLOG", String.valueOf(isExecuted));
URL url = null; //get URL from your uri object
try {
url = new URL(UPDATE_OFFLINE);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
InputStream stream = url.openStream();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("rrLOG", String.valueOf(url));
return String.valueOf(url);
}
@Override
protected void onPostExecute(String result) {
Log.i("rrLOG", "onPostExecute");
super.onPostExecute(result);
stopSelf();
}
}
public void updateServer(String urlJsonArry) {
Log.i("rrLOG", urlJsonArry);
if(isExecuted == 0) {
new NetworkTask().execute();
isExecuted = 1;
}
}
}
Solution 1:[1]
In Android, every network request is to be made on worker thread. That thread is different to UI thread. You have to make network request in background via AsyncTask
. Example below:
public class NetworkTask extends AsyncTask<Void,Void,Void> {
public Void doInBackground() {
// Here you can make network request
}
}
Now you can make this request inside a Service, so even if your activity is killed the network request will go through.
Kindly ensure you have internet permissions in Manifest.
Edit:
I checked the attached code in question, you are trying to stop the service at the same time when you have triggered a network request. If you do so, your network request won't be even completed before it is killed by the system.
You can kill service once your network request is completed, i.e. inside onPostExecute
of AsyncTask
.
Solution 2:[2]
public class NetworkTask extends AsyncTask<Void,Void,String> {
@Override
protected String doInBackground(Void... voids) {
SessionManager manager=new SessionManager ();
Log.i("rrLOG", String.valueOf(isExecuted));
URL url = null; //get URL from your uri object
try {
url = new URL(paste your url);
} catch (MalformedURLException e) {
e.printStackTrace ();
}
try {
InputStream stream = url.openStream();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("rrLOG", String.valueOf(url));
return String.valueOf(url);
}
@Override
protected void onPostExecute(String result) {
Log.i("rrLOG", "onPostExecute");
super.onPostExecute(result);
stopSelf();
}
}
public void updateServer(String url) {
Log.i("rrLOG", urlJsonArry);
if(isExecuted == 0) {
new NetworkTask().execute();
isExecuted = 1;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Patrick |
Solution 2 | Arthi Vengadesan |