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.R; import cl.mmoscoso.geocomm.entity.GeoCommPoint; import cl.mmoscoso.geocomm.gui.GeoCommCreateRouteActivity; import cl.mmoscoso.geocomm.gui.GeoCommMapViewActivity; import cl.mmoscoso.geocomm.gui.GeoCommRegisterActivity; import cl.mmoscoso.geocomm.gui.MyItemizedOverlay; public class GeoCommRegisterAsyncTask extends AsyncTask<Void, Integer, Boolean> { private ProgressDialog mProgressDialog; private Context context; private String hostname; private int status; private String name, pass, e_mail; //private String latitude,longitude; private final String TAGNAME = "GeoCommRegisterAsyncTask"; MyItemizedOverlay myItemizedOverlay = null; public GeoCommRegisterAsyncTask(Context context, String hn, String name, String pass, String e_mail) { this.context = context; this.hostname = hn; this.pass = pass; this.name = name; this.e_mail = e_mail; 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 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("username", this.name)); nameValuePairs.add(new BasicNameValuePair("password", this.pass)); nameValuePairs.add(new BasicNameValuePair("email", this.e_mail)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); int responseCode = response.getStatusLine().getStatusCode(); switch (responseCode) { 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); Log.i(TAGNAME, "tamao22: " + jObj.getInt("status")); 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 { ((GeoCommRegisterActivity) this.context).callLogin(); } catch (Exception e) { // TODO: handle exception } } else { Toast.makeText(this.context, R.string.error_fairegister, Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this.context, R.string.error_cantregister, Toast.LENGTH_SHORT).show(); } } @Override protected void onCancelled() { Toast.makeText(this.context, R.string.mssg_taskcanceled, Toast.LENGTH_SHORT).show(); } }