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

Java tutorial

Introduction

Here is the source code for cl.mmoscoso.geocomm.sync.GeoCommGetRoutesOwnerAsyncTask.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;

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 cl.mmoscoso.geocomm.GeoCommMainActivity;
import cl.mmoscoso.geocomm.R;
import cl.mmoscoso.geocomm.entity.GeoCommRoute;
import cl.mmoscoso.geocomm.gui.GeoCommListRoutesAdapter;
import cl.mmoscoso.geocomm.gui.GeoCommMapViewActivity;
import cl.mmoscoso.geocomm.gui.GeoCommOpcionUserActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

/**
 * 
 * @author Fabian Ramirez
 *
 */
public class GeoCommGetRoutesOwnerAsyncTask extends AsyncTask<String, Void, Boolean> {

    private static String TAGNAME = "GeoCommGetRoutesAsyncTask";

    private ProgressDialog mProgressDialog;
    private Context context;
    private List<GeoCommRoute> list_routes;
    private ListView listview;
    private String hostname;
    private int id_user;

    /**
     * 
     * @param cont
     * @param url
     * @param username
     * @param password
     */
    public GeoCommGetRoutesOwnerAsyncTask(Context cont, String host, List<GeoCommRoute> routes, ListView listview,
            int id_user) {
        this.context = cont;
        this.list_routes = routes;
        this.listview = listview;
        this.hostname = host;
        this.id_user = id_user;
        this.mProgressDialog = new ProgressDialog(context);
        this.mProgressDialog.setMessage(context.getResources().getString(R.string.mssg_loading_routes));
        this.mProgressDialog.setIndeterminate(false);
        this.mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        this.mProgressDialog.setCancelable(true);

    }

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

    @Override
    protected Boolean doInBackground(String... params) {
        // TODO Auto-generated method stub

        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);

        Log.i(TAGNAME, "Try to connect to " + this.hostname);
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("id", Integer.toString(this.id_user)));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            Log.i(TAGNAME, "Post Request");
            HttpResponse response = httpclient.execute(httppost);
            int responseCode = response.getStatusLine().getStatusCode();

            switch (responseCode) {
            default:
                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, ruta;
                    try {
                        Log.i(TAGNAME, "Reading JSONResponse");
                        jObj = new JSONObject(responseBody);
                        SharedPreferences preferences = this.context.getSharedPreferences("userInformation",
                                context.MODE_PRIVATE);
                        Boolean is_allroutes = preferences.getBoolean("is_allroutes", false);
                        for (int i = 0; i < jObj.length(); i++) {
                            ruta = new JSONObject(jObj.getString(jObj.names().getString(i)));
                            if (is_allroutes) {
                                if (ruta.getBoolean("public") == false) {
                                    list_routes.add(new GeoCommRoute(ruta.getString("name"),
                                            ruta.getString("owner"), ruta.getInt("id"), ruta.getInt("totalPoints"),
                                            ruta.getString("description"), ruta.getBoolean("public")));
                                }
                            } else
                                list_routes.add(new GeoCommRoute(ruta.getString("name"), ruta.getString("owner"),
                                        ruta.getInt("id"), ruta.getInt("totalPoints"),
                                        ruta.getString("description"), ruta.getBoolean("public")));
                        }
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                break;
            }

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            Log.e(TAGNAME, e.toString());
            return false;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.e(TAGNAME, e.toString());
            return false;
        }
        return true;
    }

    /**
     * 
     */
    @Override
    protected void onPostExecute(Boolean result) {
        mProgressDialog.dismiss();
        if (result) {
            //Toast.makeText(this.context,R.string.mssg_taskcompleted, Toast.LENGTH_SHORT).show();
            GeoCommListRoutesAdapter adapter = new GeoCommListRoutesAdapter(context, list_routes);
            this.listview.setAdapter(adapter);
        } else {
            Toast.makeText(this.context, R.string.error_cantgetroutes, Toast.LENGTH_SHORT).show();
        }
        SharedPreferences preferences = this.context.getSharedPreferences("userInformation", context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putBoolean("is_allroutes", false);
        edit.commit();

    }

}