How to ping a URL in an Android Service?











up vote
9
down vote

favorite
8












How can I use Android Service to do a ping callback? I need to open a webpage on a button click, but in the background, go ping another URL for stats collection.










share|improve this question
























  • Kotlin implementation supporting API 25+ can be found here: stackoverflow.com/questions/53402128/…
    – quietbits
    Nov 20 at 23:33















up vote
9
down vote

favorite
8












How can I use Android Service to do a ping callback? I need to open a webpage on a button click, but in the background, go ping another URL for stats collection.










share|improve this question
























  • Kotlin implementation supporting API 25+ can be found here: stackoverflow.com/questions/53402128/…
    – quietbits
    Nov 20 at 23:33













up vote
9
down vote

favorite
8









up vote
9
down vote

favorite
8






8





How can I use Android Service to do a ping callback? I need to open a webpage on a button click, but in the background, go ping another URL for stats collection.










share|improve this question















How can I use Android Service to do a ping callback? I need to open a webpage on a button click, but in the background, go ping another URL for stats collection.







android url service ping






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 22:04









halfer

14.3k758107




14.3k758107










asked May 7 '10 at 6:47









Chris

2,334113547




2,334113547












  • Kotlin implementation supporting API 25+ can be found here: stackoverflow.com/questions/53402128/…
    – quietbits
    Nov 20 at 23:33


















  • Kotlin implementation supporting API 25+ can be found here: stackoverflow.com/questions/53402128/…
    – quietbits
    Nov 20 at 23:33
















Kotlin implementation supporting API 25+ can be found here: stackoverflow.com/questions/53402128/…
– quietbits
Nov 20 at 23:33




Kotlin implementation supporting API 25+ can be found here: stackoverflow.com/questions/53402128/…
– quietbits
Nov 20 at 23:33












1 Answer
1






active

oldest

votes

















up vote
18
down vote



accepted










I think if you just want to ping an url, you can use this code :



try {
URL url = new URL("http://"+params[0]);

HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "Android Application:"+Z.APP_VERSION);
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1000 * 30); // mTimeout is in seconds
urlc.connect();

if (urlc.getResponseCode() == 200) {
Main.Log("getResponseCode == 200");
return new Boolean(true);
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


And, if you want to have the equivalent of a ping, you can put a System.currentTimeMillis() before and after the call, and calculate the difference.



hopes that helps



code snippet found in here






share|improve this answer























  • This has been working fine for me except for Android 4 devices where it always fails. Any ideas?
    – dee
    Feb 10 '12 at 16:29






  • 10




    You need to move the call off your main thread. Android 4 enforces no network calls on the main thread to prevent ANR issues.
    – Nick Campion
    Feb 13 '12 at 22:47






  • 2




    This will establish a TCP connection with a full 3-way handshake. So the time is nothing like a ping (ICMP-echo -> ICMP-reply)
    – Fredrik Erlandsson
    Mar 4 '15 at 19:51










  • @NickCampion: for me this code is not working on 4.2.2 device which includes samsung s3 and other , any solution
    – Punit Sharma
    Feb 10 '16 at 10:32











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2786720%2fhow-to-ping-a-url-in-an-android-service%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
18
down vote



accepted










I think if you just want to ping an url, you can use this code :



try {
URL url = new URL("http://"+params[0]);

HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "Android Application:"+Z.APP_VERSION);
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1000 * 30); // mTimeout is in seconds
urlc.connect();

if (urlc.getResponseCode() == 200) {
Main.Log("getResponseCode == 200");
return new Boolean(true);
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


And, if you want to have the equivalent of a ping, you can put a System.currentTimeMillis() before and after the call, and calculate the difference.



hopes that helps



code snippet found in here






share|improve this answer























  • This has been working fine for me except for Android 4 devices where it always fails. Any ideas?
    – dee
    Feb 10 '12 at 16:29






  • 10




    You need to move the call off your main thread. Android 4 enforces no network calls on the main thread to prevent ANR issues.
    – Nick Campion
    Feb 13 '12 at 22:47






  • 2




    This will establish a TCP connection with a full 3-way handshake. So the time is nothing like a ping (ICMP-echo -> ICMP-reply)
    – Fredrik Erlandsson
    Mar 4 '15 at 19:51










  • @NickCampion: for me this code is not working on 4.2.2 device which includes samsung s3 and other , any solution
    – Punit Sharma
    Feb 10 '16 at 10:32















up vote
18
down vote



accepted










I think if you just want to ping an url, you can use this code :



try {
URL url = new URL("http://"+params[0]);

HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "Android Application:"+Z.APP_VERSION);
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1000 * 30); // mTimeout is in seconds
urlc.connect();

if (urlc.getResponseCode() == 200) {
Main.Log("getResponseCode == 200");
return new Boolean(true);
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


And, if you want to have the equivalent of a ping, you can put a System.currentTimeMillis() before and after the call, and calculate the difference.



hopes that helps



code snippet found in here






share|improve this answer























  • This has been working fine for me except for Android 4 devices where it always fails. Any ideas?
    – dee
    Feb 10 '12 at 16:29






  • 10




    You need to move the call off your main thread. Android 4 enforces no network calls on the main thread to prevent ANR issues.
    – Nick Campion
    Feb 13 '12 at 22:47






  • 2




    This will establish a TCP connection with a full 3-way handshake. So the time is nothing like a ping (ICMP-echo -> ICMP-reply)
    – Fredrik Erlandsson
    Mar 4 '15 at 19:51










  • @NickCampion: for me this code is not working on 4.2.2 device which includes samsung s3 and other , any solution
    – Punit Sharma
    Feb 10 '16 at 10:32













up vote
18
down vote



accepted







up vote
18
down vote



accepted






I think if you just want to ping an url, you can use this code :



try {
URL url = new URL("http://"+params[0]);

HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "Android Application:"+Z.APP_VERSION);
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1000 * 30); // mTimeout is in seconds
urlc.connect();

if (urlc.getResponseCode() == 200) {
Main.Log("getResponseCode == 200");
return new Boolean(true);
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


And, if you want to have the equivalent of a ping, you can put a System.currentTimeMillis() before and after the call, and calculate the difference.



hopes that helps



code snippet found in here






share|improve this answer














I think if you just want to ping an url, you can use this code :



try {
URL url = new URL("http://"+params[0]);

HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "Android Application:"+Z.APP_VERSION);
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1000 * 30); // mTimeout is in seconds
urlc.connect();

if (urlc.getResponseCode() == 200) {
Main.Log("getResponseCode == 200");
return new Boolean(true);
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


And, if you want to have the equivalent of a ping, you can put a System.currentTimeMillis() before and after the call, and calculate the difference.



hopes that helps



code snippet found in here







share|improve this answer














share|improve this answer



share|improve this answer








edited May 23 '17 at 12:09









Community

11




11










answered May 7 '10 at 8:34









Sephy

37.2k27106122




37.2k27106122












  • This has been working fine for me except for Android 4 devices where it always fails. Any ideas?
    – dee
    Feb 10 '12 at 16:29






  • 10




    You need to move the call off your main thread. Android 4 enforces no network calls on the main thread to prevent ANR issues.
    – Nick Campion
    Feb 13 '12 at 22:47






  • 2




    This will establish a TCP connection with a full 3-way handshake. So the time is nothing like a ping (ICMP-echo -> ICMP-reply)
    – Fredrik Erlandsson
    Mar 4 '15 at 19:51










  • @NickCampion: for me this code is not working on 4.2.2 device which includes samsung s3 and other , any solution
    – Punit Sharma
    Feb 10 '16 at 10:32


















  • This has been working fine for me except for Android 4 devices where it always fails. Any ideas?
    – dee
    Feb 10 '12 at 16:29






  • 10




    You need to move the call off your main thread. Android 4 enforces no network calls on the main thread to prevent ANR issues.
    – Nick Campion
    Feb 13 '12 at 22:47






  • 2




    This will establish a TCP connection with a full 3-way handshake. So the time is nothing like a ping (ICMP-echo -> ICMP-reply)
    – Fredrik Erlandsson
    Mar 4 '15 at 19:51










  • @NickCampion: for me this code is not working on 4.2.2 device which includes samsung s3 and other , any solution
    – Punit Sharma
    Feb 10 '16 at 10:32
















This has been working fine for me except for Android 4 devices where it always fails. Any ideas?
– dee
Feb 10 '12 at 16:29




This has been working fine for me except for Android 4 devices where it always fails. Any ideas?
– dee
Feb 10 '12 at 16:29




10




10




You need to move the call off your main thread. Android 4 enforces no network calls on the main thread to prevent ANR issues.
– Nick Campion
Feb 13 '12 at 22:47




You need to move the call off your main thread. Android 4 enforces no network calls on the main thread to prevent ANR issues.
– Nick Campion
Feb 13 '12 at 22:47




2




2




This will establish a TCP connection with a full 3-way handshake. So the time is nothing like a ping (ICMP-echo -> ICMP-reply)
– Fredrik Erlandsson
Mar 4 '15 at 19:51




This will establish a TCP connection with a full 3-way handshake. So the time is nothing like a ping (ICMP-echo -> ICMP-reply)
– Fredrik Erlandsson
Mar 4 '15 at 19:51












@NickCampion: for me this code is not working on 4.2.2 device which includes samsung s3 and other , any solution
– Punit Sharma
Feb 10 '16 at 10:32




@NickCampion: for me this code is not working on 4.2.2 device which includes samsung s3 and other , any solution
– Punit Sharma
Feb 10 '16 at 10:32


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2786720%2fhow-to-ping-a-url-in-an-android-service%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin