cl.mmoscoso.geocomm.sync.GeoCommDeletePointAsyncTask.java Source code

Java tutorial

Introduction

Here is the source code for cl.mmoscoso.geocomm.sync.GeoCommDeletePointAsyncTask.java

Source

/**
Copyright 2014 Fabian Ramirez Barrios
This file is part of GeoComm.
    
GeoComm is free software: you can redistribute it and/or modify 
it under the terms of the GNU General Public License as published by 
the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.
    
GeoComm is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License 
along with GeoComm.  If not, see <http://www.gnu.org/licenses/>.
    
**/
package cl.mmoscoso.geocomm.sync;

/**
 * @author Fabian Ramirez
 */
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import cl.mmoscoso.geocomm.GeoCommMainActivity;
import cl.mmoscoso.geocomm.R;
import cl.mmoscoso.geocomm.entity.GeoCommPoint;
import cl.mmoscoso.geocomm.gui.GeoCommCreateRouteActivity;
import cl.mmoscoso.geocomm.gui.GeoCommDeletePointsActivity;
import cl.mmoscoso.geocomm.gui.GeoCommLogInActivity;
import cl.mmoscoso.geocomm.gui.GeoCommMapViewActivity;
import cl.mmoscoso.geocomm.gui.MyItemizedOverlay;

public class GeoCommDeletePointAsyncTask extends AsyncTask<Void, Integer, Boolean> {

    private ProgressDialog mProgressDialog;
    private Context context;
    private String hostname;
    //private String latitude,longitude;
    private final String TAGNAME = "GeoCommCreatePointAsyncTask";
    MyItemizedOverlay myItemizedOverlay = null;
    private int status, id_route, id_point;

    public GeoCommDeletePointAsyncTask(Context context, String hn, int id_point) {
        this.context = context;
        this.hostname = hn;
        this.id_point = id_point;
        this.mProgressDialog = new ProgressDialog(context);
        this.mProgressDialog.setMessage(this.context.getResources().getString(R.string.Loading));
        this.mProgressDialog.setIndeterminate(false);
        this.mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        this.mProgressDialog.setCancelable(true);
    }

    @Override
    protected Boolean doInBackground(Void... params) {

        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpParams httpParams = httpclient.getParams();
        httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
        httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);

        HttpPost httppost = new HttpPost(this.hostname);

        //http://172.16.50.35/~ramirez/testAddPoint.php
        //http://172.16.57.132/~laost/Symfony/web/app_dev.php/addPoint/

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("id", Integer.toString(this.id_point)));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            int responseCode = response.getStatusLine().getStatusCode();
            Log.i(TAGNAME, "ERROR: " + responseCode);
            switch (responseCode) {
            default:
                Log.i(TAGNAME, "ERROR");
                //Toast.makeText(this.context,R.string.error_not200code, Toast.LENGTH_SHORT).show();
                break;
            case 200:
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    String responseBody = EntityUtils.toString(entity);
                    //Log.i(TAGNAME, responseBody);
                    JSONObject jObj;
                    try {
                        Log.i(TAGNAME, "tamao: " + responseBody);
                        jObj = new JSONObject(responseBody);
                        this.status = jObj.getInt("status");
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                break;
            }

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }

        return true;
    }

    @Override
    protected void onPreExecute() {
        mProgressDialog.show();
    }

    @Override
    protected void onPostExecute(Boolean result) {
        mProgressDialog.dismiss();
        if (result) {
            if (this.status == 1) {
                //Toast.makeText(this.context,R.string.mssg_taskcompleted, Toast.LENGTH_SHORT).show();
                try {
                    ((GeoCommDeletePointsActivity) this.context).callMenu();
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
            if (this.status == 0) {
                Toast.makeText(this.context, R.string.error_failDeletePoint, Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this.context, R.string.error_cantDeletePoint, Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onCancelled() {
        Toast.makeText(this.context, R.string.mssg_taskcanceled, Toast.LENGTH_SHORT).show();
    }

}