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.GeoCommDeletePointsActivity; import cl.mmoscoso.geocomm.gui.GeoCommMapViewActivity; import cl.mmoscoso.geocomm.gui.MyItemizedOverlay; public class GeoCommGetPointsAsyncTask extends AsyncTask<Void, Integer, Boolean> { private ProgressDialog mProgressDialog; private Context context; private String hostname; private int id; //private String latitude,longitude; private final String TAGNAME = "GeoCommGetPointsAsyncTask"; private List<GeoCommPoint> points; //MyItemizedOverlay myItemizedOverlay = null; public GeoCommGetPointsAsyncTask(Context context, String hn, int id, List<GeoCommPoint> points) { this.context = context; this.hostname = hn; this.id = id; this.points = points; 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))); 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, point; try { //Log.i(TAGNAME, "tamao: "+ responseBody); jObj = new JSONObject(responseBody); //point = new JSONObject(jObj.getString("point1")); //Log.i(TAGNAME, "tamao22: "+ jObj.length()); //Log.i(TAGNAME, "VER: "+ point.names()); //Log.i(TAGNAME, "tamao: "+ jObj.names().getString(0)); for (int i = 0; i < jObj.length(); i++) { point = new JSONObject(jObj.getString(jObj.names().getString(i))); //Log.i(TAGNAME, "VER2: "+ jObj.getString(jObj.names().getString(i))); points.add(new GeoCommPoint(point.getInt("id"), point.getDouble("longitude"), point.getDouble("latitude"), point.getString("name"), point.getString("description"))); //Log.i(TAGNAME, points.get(i).getName()); //Log.i(TAGNAME, "lat: "+points.get(i).getLatitude()); //Log.i(TAGNAME, "long: "+points.get(i).getLongitude()); } } 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) { if (result) { mProgressDialog.dismiss(); //Toast.makeText(this.context,R.string.mssg_taskcompleted, Toast.LENGTH_SHORT).show(); try { ((GeoCommMapViewActivity) this.context).drawPoints(); } catch (Exception e) { // TODO: handle exception } try { ((GeoCommDeletePointsActivity) this.context).callAdapter(points); } catch (Exception e) { // TODO: handle exception } } } @Override protected void onCancelled() { Toast.makeText(this.context, R.string.mssg_taskcanceled, Toast.LENGTH_SHORT).show(); } }