Click here to Skip to main content
15,849,494 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am developing an application which connects to a web service via the internet and gets some information and I've used Async task for this in two activities. Now I want to use the same code in a service class which works with Timer task and on every tick, if the application is not running, it should return some data from web service. I can retrieve data when the app is open, but when I close it, I get "Connection to is refused"!

public void initializeTimerTask() {

    timerTask = new TimerTask() {
        public void run() {

            //use a handler to run a toast that shows the current timestamp
   Runnable() {
                public void run() {if(!isAppRunning(getApplicationContext(),getPackageName())) {

                            new ExecuteTask().execute();


class ExecuteTask extends AsyncTask<String, Void, String> {

    protected void onPreExecute() {


    protected void onCancelled() {

    protected String doInBackground(String... params) {

        String res1="";
        try {
                res1 = PostData();

        } catch (Exception ex) {
        return res1;

    protected void onPostExecute(String result) {

        if (!isCancelled()) {
            Log.d("responsed", result);

            try {

                int Ifrom = result.indexOf("[");
                int ITo = result.lastIndexOf("]");
                if (Ifrom > -1 && ITo > -1) {
                    result = result.substring(Ifrom, ITo + 1);
                    Log.d("test", result);

                android.util.JsonReader reader = new android.util.JsonReader(new StringReader(result));
                Type listType = new Type() {

                listType = new TypeToken<List<alarms>>() {
                List<alarms> lstAlarms = new ArrayList<alarms>();
                lstAlarms = new Gson().fromJson(result, listType);

                //if there is new alarm then show notifications

            } catch (Exception ex)




public String PostData() {
    String s = "";
    try {
        HttpClient httpClient = new DefaultHttpClient();
        String Url = "";
            Url = settingsWeb.getString("Address", null);
        List<namevaluepair> nameValuePairs = new ArrayList<namevaluepair>();

        nameValuePairs.add(new BasicNameValuePair("JsonQuery",Command);
        Url += "/SelectJsonAlarms";

        HttpPost httppost = new HttpPost(Url);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpClient.execute(httppost);

        s = readResponse(response);
    } catch (Exception e) {
        return "error";

    return s;


What I have tried:

I have uses-permission android:name="android.permission.INTERNET" in my Android manifest.
I get the error on this line ==> HttpResponse response = httpClient.execute(httppost);
Updated 15-Jan-19 0:32am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900