Java tutorial
/** 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.GeoCommLogInActivity; import cl.mmoscoso.geocomm.gui.GeoCommMapViewActivity; import cl.mmoscoso.geocomm.gui.MyItemizedOverlay; public class GeoCommCreateRouteAsyncTask extends AsyncTask<Void, Integer, Boolean> { private ProgressDialog mProgressDialog; private Context context; private String hostname; private String name, desc; //private String latitude,longitude; private final String TAGNAME = "GeoCommCreateRouteAsyncTask"; private int status, id_user; private boolean is_public; public GeoCommCreateRouteAsyncTask(Context context, String hn, String name, String desc, boolean is_public, int id) { this.context = context; this.hostname = hn; this.desc = desc; this.name = name; this.is_public = is_public; this.id_user = id; 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); try { // Add your data Log.i(TAGNAME, "PUBLIC: " + this.is_public); Log.i(TAGNAME, "NAME: " + this.name); Log.i(TAGNAME, "DESC: " + this.desc); Log.i(TAGNAME, "OWNER: " + this.id_user); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("name", this.name)); nameValuePairs.add(new BasicNameValuePair("description", this.desc)); nameValuePairs.add(new BasicNameValuePair("id", Integer.toString(this.id_user))); nameValuePairs.add(new BasicNameValuePair("public", Boolean.toString(this.is_public))); //nameValuePairs.add(new BasicNameValuePair("id", "1")); 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, "Status: " + this.is_public); 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 { ((GeoCommCreateRouteActivity) this.context).callMenu(); } catch (Exception e) { // TODO: handle exception } } else { Toast.makeText(this.context, R.string.error_failCreateRoute, Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this.context, R.string.error_cantCreateRoute, Toast.LENGTH_SHORT).show(); } } @Override protected void onCancelled() { Toast.makeText(this.context, R.string.mssg_taskcanceled, Toast.LENGTH_SHORT).show(); } }