--- android/PicturePoster/src/dk/thoerup/pictureposter/PostActivity.java 2010/02/20 14:01:43 597 +++ android/PicturePoster/src/dk/thoerup/pictureposter/PostActivity.java 2010/02/21 18:09:14 598 @@ -17,9 +17,16 @@ import org.apache.http.message.BasicNameValuePair; import android.app.Activity; +import android.app.Dialog; +import android.app.ProgressDialog; import android.content.Intent; import android.database.Cursor; +import android.location.Criteria; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; import android.net.Uri; +import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; @@ -29,7 +36,7 @@ import android.widget.TextView; import android.widget.Toast; -public class PostActivity extends Activity { +public class PostActivity extends Activity implements LocationListener { static final int IMAGE_PICK = 1000; ImageView picture; @@ -37,6 +44,9 @@ TextView title; TextView note; + Location currentLocation; + LocationManager locManager; + /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { @@ -60,6 +70,13 @@ submit(); } }); + + locManager = (LocationManager) getSystemService(LOCATION_SERVICE); + Criteria c = new Criteria(); + c.setAccuracy(Criteria.ACCURACY_FINE); + String provider = locManager.getBestProvider(c, true); + locManager.requestLocationUpdates(provider, 10, 0.0f, this); + } @@ -103,59 +120,117 @@ } void submit() { - HttpPost post = new HttpPost("http://app.t-hoerup.dk/PicturePosterService/PostServlet"); String titleStr = title.getText().toString(); String noteStr = note.getText().toString(); if (titleStr.trim().length() == 0) { - Toast.makeText(this, "Please enter a title", Toast.LENGTH_LONG).show(); + Toast.makeText(PostActivity.this, "Please enter a title", Toast.LENGTH_LONG).show(); return; } if (noteStr.trim().length() == 0) { - Toast.makeText(this, "Please enter a note", Toast.LENGTH_LONG).show(); + Toast.makeText(PostActivity.this, "Please enter a note", Toast.LENGTH_LONG).show(); return; } if (media_path == null || media_path.length() == 0) { - Toast.makeText(this, "Please select an picture", Toast.LENGTH_LONG).show(); - return; + Toast.makeText(PostActivity.this, "Please select an picture", Toast.LENGTH_LONG).show(); + return; } - + UploaderTask task = new UploaderTask(); + task.execute(); + } - MultipartEntity entity = new MultipartEntity(); - - entity.addPart("file", new FileBody( new File(media_path), "image/jpeg") ); - - try { - List data = new ArrayList(); - data.add( new BasicNameValuePair("title", titleStr) ); - data.add( new BasicNameValuePair("note", noteStr) ); - String encoded = URLEncodedUtils.format(data, "UTF-8"); - entity.addPart("text", new StringBody(encoded, URLEncodedUtils.CONTENT_TYPE, Charset.forName("UTF-8") )); - } catch (Exception e) { - Log.e("asdasd","asdasdasd",e); + + + + + @Override + public void onLocationChanged(Location location) { + currentLocation = location; + locManager.removeUpdates(this); + } + + @Override + public void onProviderDisabled(String provider) { + } + + @Override + public void onProviderEnabled(String provider) { + } + + @Override + public void onStatusChanged(String provider, int status, Bundle extras) { + } + + class UploaderTask extends AsyncTask { + String titleStr; + String noteStr; + + Dialog dlg; + + @Override + protected void onPreExecute() { + super.onPreExecute(); + titleStr = title.getText().toString(); + noteStr = note.getText().toString(); + + dlg = ProgressDialog.show(PostActivity.this, "PicturePoster", "Uploading data"); } - - - post.setEntity(entity); - boolean ok = false; - HttpClient client = new DefaultHttpClient(); - try { - HttpResponse resp = client.execute(post); - if (resp.getStatusLine().getStatusCode() == 200) { - ok = true; + + @Override + protected Boolean doInBackground(Void... params) { + HttpPost post = new HttpPost("http://192.168.10.10:8080/PicturePosterService/PostServlet"); + + + + MultipartEntity entity = new MultipartEntity(); + + entity.addPart("file", new FileBody( new File(media_path), "image/jpeg") ); + + try { + boolean hasLoc = currentLocation != null; + List data = new ArrayList(); + data.add( new BasicNameValuePair("title", titleStr) ); + data.add( new BasicNameValuePair("note", noteStr) ); + + data.add( new BasicNameValuePair("latitude", hasLoc ? Float.toString((float)currentLocation.getLatitude()) : "-") ); + data.add( new BasicNameValuePair("longitude", hasLoc ? Float.toString((float)currentLocation.getLongitude()) : "-" ) ); + + String encoded = URLEncodedUtils.format(data, "UTF-8"); + entity.addPart("text", new StringBody(encoded, URLEncodedUtils.CONTENT_TYPE, Charset.forName("UTF-8") )); + } catch (Exception e) { + Log.e("asdasd","asdasdasd",e); } - } catch (Exception e) { - Log.d("PostActivity", "upload failed", e); + post.setEntity(entity); + boolean ok = false; + HttpClient client = new DefaultHttpClient(); + try { + HttpResponse resp = client.execute(post); + Log.d("PostActivity", "" + resp.getStatusLine().getStatusCode() + ": " + resp.getStatusLine().getReasonPhrase() ); + if (resp.getStatusLine().getStatusCode() == 200) { + ok = true; + } + + } catch (Exception e) { + Log.d("PostActivity", "upload failed", e); + } + return ok; + } - if (ok == true) { - Toast.makeText(this, "Upload succeeded", Toast.LENGTH_LONG).show(); - } else { - Toast.makeText(this, "Upload failed", Toast.LENGTH_LONG).show(); + @Override + protected void onPostExecute(Boolean result) { + super.onPostExecute(result); + + dlg.dismiss(); + + if (result == true) { + Toast.makeText(PostActivity.this, "Upload succeeded", Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(PostActivity.this, "Upload failed", Toast.LENGTH_LONG).show(); + } } } - } \ No newline at end of file