Skip to content
Browse files

enviando coordenadas geograficas no post

  • Loading branch information...
1 parent 5da6868 commit 68cc91c0585df8bdf3e675325c22544f1a8bbeb6 @eduresende committed Oct 9, 2011
Showing with 88 additions and 10 deletions.
  1. +71 −7 src/com/eduardo/PostarActivity.java
  2. +2 −1 src/com/eduardo/TimelineActivity.java
  3. +15 −2 src/com/eduardo/Twitter.java
View
78 src/com/eduardo/PostarActivity.java
@@ -2,6 +2,9 @@
import android.app.Activity;
import android.graphics.Color;
+import android.location.Location;
+import android.location.LocationListener;
+import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
@@ -13,14 +16,21 @@
import android.widget.EditText;
import android.widget.TextView;
-public class PostarActivity extends Activity implements OnClickListener, TextWatcher {
+public class PostarActivity extends Activity implements OnClickListener, TextWatcher, LocationListener {
private Button buttonPostar;
private EditText editStatus;
private TextView textResposta;
private TextView textContador;
+ //location
+ private static final long LOCATION_MIN_TIME = 3600000; // One hour
+ private static final float LOCATION_MIN_DISTANCE = 1000; // One kilometer
+ LocationManager locationManager;
+ Location location;
+ String provider;
+
@Override
public void onCreate(Bundle savedInstanceState){
@@ -38,13 +48,21 @@ public void onCreate(Bundle savedInstanceState){
textContador.setText(Integer.toString(140));
textContador.setTextColor(Color.GREEN);
editStatus.addTextChangedListener(this);
-
}
public void onClick(View v) {
- String status = editStatus.getText().toString();
- if (verificaLimite(status.length())){
- new Postador().execute(status);
+
+ String texto = editStatus.getText().toString();
+
+ double latlong[] = buscarPosicaoGeografica();
+ String latitude = String.valueOf(latlong[0]);
+ String longitude = String.valueOf(latlong[0]);
+
+ String params[] = {texto, latitude, longitude};
+
+ //String status = editStatus.getText().toString();
+ if (verificaLimite(texto.length())){
+ new Postador().execute(params);
}else{
mostrarResposta(Color.RED, getString(R.string.respostaMensagemTamanho));
}
@@ -53,9 +71,9 @@ public void onClick(View v) {
class Postador extends AsyncTask<String, Integer, String> {
@Override
- protected String doInBackground(String... status) {
+ protected String doInBackground(String... params) {
try {
- Twitter.postar(status[0]);
+ Twitter.postar(params);
return "ok";
} catch (Exception e) {
Log.e(Constantes.TAG, e.toString());
@@ -120,4 +138,50 @@ public boolean verificaLimite(int numCaracteres){
return true;
}
+
+ public void onLocationChanged(Location location) {
+ // TODO Auto-generated method stub
+ this.location = location;
+
+
+ }
+
+ public void onProviderDisabled(String provider) {
+ // TODO Auto-generated method stub
+ if (this.provider.equals(provider))
+ locationManager.removeUpdates(this);
+
+
+ }
+
+ public void onProviderEnabled(String provider) {
+ // TODO Auto-generated method stub
+ if (this.provider.equals(provider))
+ locationManager.requestLocationUpdates(this.provider, LOCATION_MIN_TIME,
+ LOCATION_MIN_DISTANCE, this);
+
+
+ }
+
+ public void onStatusChanged(String provider, int status, Bundle extras) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public double[] buscarPosicaoGeografica(){
+ provider = LocationManager.GPS_PROVIDER;
+ locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
+ double latlong[] = {0,0};
+
+ if (locationManager != null) {
+ location = locationManager.getLastKnownLocation(provider); //
+ locationManager.requestLocationUpdates(provider, LOCATION_MIN_TIME, LOCATION_MIN_DISTANCE, this);
+ }
+ if (location != null){
+ latlong[0] = location.getLatitude();
+ latlong[1] = location.getLongitude();
+ }
+ return latlong;
+ }
+
}
View
3 src/com/eduardo/TimelineActivity.java
@@ -75,7 +75,8 @@ public JSONArray buscaTimeline(){
for(Integer i=0; i< result.length(); i++){
try{
JSONObject obj = result.getJSONObject(i);
- String post = obj.getJSONObject("user").getString("name") + ": " + obj.getString("text");
+ String location = obj.getString("geo");
+ String post = obj.getJSONObject("user").getString("name") + ": " + obj.getString("text") + ". Location: " + location;
adapter.add(post);
}catch(JSONException e){
View
17 src/com/eduardo/Twitter.java
@@ -25,6 +25,8 @@
import org.json.JSONException;
import org.json.JSONObject;
+import android.util.Log;
+
public class Twitter {
static CommonsHttpOAuthConsumer consumer;
@@ -37,14 +39,22 @@ public static CommonsHttpOAuthConsumer getConsumer(){
}
- public static JSONObject postar(String texto){
+ public static JSONObject postar(String params[]){
JSONObject jso = null;
HttpParams parametros = new BasicHttpParams();
HttpProtocolParams.setUseExpectContinue(parametros, false);
try {
HttpPost post = new HttpPost(Constantes.POST_RESOURCE);
LinkedList<BasicNameValuePair> out = new LinkedList<BasicNameValuePair>();
- out.add(new BasicNameValuePair("status", texto));
+ out.add(new BasicNameValuePair("status", params[0]));
+ out.add(new BasicNameValuePair("lat", params[1]));
+ out.add(new BasicNameValuePair("long", params[2]));
+ out.add(new BasicNameValuePair("display_coordinates", "true"));
+
+ Log.v(Constantes.TAG, params[1]);
+ Log.v(Constantes.TAG, params[2]);
+
+
post.setEntity(new UrlEncodedFormEntity(out, HTTP.UTF_8));
post.setParams(parametros);
getConsumer().sign(post);
@@ -76,6 +86,9 @@ public static JSONArray ler(){
try {
getConsumer().sign(httpGet);
String response = client.execute(httpGet, new BasicResponseHandler());
+
+ Log.v(Constantes.TAG, response);
+
array = new JSONArray(response);
} catch (JSONException e) {
e.printStackTrace();

0 comments on commit 68cc91c

Please sign in to comment.
Something went wrong with that request. Please try again.