Hi there,

I've re-written my app a bit and have completed the token exchange
stuff.  Now on app loadup, I check to see if there is a stored key, I
go right into the activity.  However, whats now happening is I'm
getting an NullPointerException on this line:

Log.i("Response", result.readBodyAsString().toString());

It seems I am not getting a response body.  This is the OAuthMessage I
am sending, with some stuff changed in the values TOKEN_HASH and
CONSUMER_KEY - but I can assure you these values are here:

01-14 00:37:15.341: INFO/Request(4818): OAuthMessage(GET,
http://brightkite.com/places/search.json,
[q=55.9588086605072%2C-3.1632739305496216, oauth_token=TOKEN_HASH,
oauth_consumer_key=CONSUMER_KEY, oauth_signature_method=HMAC-SHA1,
oauth_timestamp=1231893434, oauth_nonce=28181971054305,
oauth_version=1.0, oauth_signature=V6MxRdUHwUUZZBgbZjo7PW4ZQc8%3D])


package org.ifies.brightroid;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import net.oauth.OAuth;
import net.oauth.OAuthAccessor;
import net.oauth.OAuthConsumer;
import net.oauth.OAuthException;
import net.oauth.OAuthMessage;
import net.oauth.OAuthServiceProvider;
import net.oauth.client.OAuthClient;
import net.oauth.client.httpclient4.HttpClient4;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

public class BrightroidMap extends MapActivity {

        private final OAuthServiceProvider provider = new OAuthServiceProvider
(
                        
org.ifies.brightroid.oauth.Request.OAUTH_REQUEST_TOKEN_URL,
                        org.ifies.brightroid.oauth.Request.OAUTH_AUTHORIZE_URL,
                        
org.ifies.brightroid.oauth.Request.OAUTH_ACCESS_TOKEN_URL);
        private final OAuthConsumer consumer = new OAuthConsumer(null //
callback URL
            , org.ifies.brightroid.oauth.Request.CONSUMER_KEY //
consumer key
            , org.ifies.brightroid.oauth.Request.CONSUMER_SECRET //
consumer secret
            , provider);
        private final OAuthAccessor accessor = new OAuthAccessor(consumer);
        private final OAuthClient client = new OAuthClient(new HttpClient4
());

        @SuppressWarnings("unchecked")
        private class myLocationOverlay extends ItemizedOverlay {

                private List<OverlayItem> items=new ArrayList();
                private Drawable marker=null;

                public myLocationOverlay(Drawable defaultMarker) {
                        super(defaultMarker);
                        this.marker = defaultMarker;

                        GeoPoint point = new GeoPoint(
                                        (int) (myLocation.getLatitude() * 1E6),
                                        (int) (myLocation.getLongitude() * 
1E6));
                        items.add(new OverlayItem(point, "Me", "My Location"));
                        populate();
                }

                @Override
                protected OverlayItem createItem(int i) {
                        // TODO Auto-generated method stub
                        return (OverlayItem) items.get(i);
                }

                public void draw(Canvas canvas, MapView mapView, boolean 
shadow) {
                        super.draw(canvas, mapView, shadow);

                        boundCenterBottom(marker);
                }

                @Override
                protected boolean onTap(int i) {
                Toast.makeText(BrightroidMap.this, "Your Location",
Toast.LENGTH_SHORT).show();

                return(true);
                }

                @Override
                public int size() {
                        // TODO Auto-generated method stub
                        return(items.size());
                }

        }

        private class myLocationListener implements LocationListener {

                @Override
                public void onLocationChanged(android.location.Location 
location) {
                        // TODO Auto-generated method stub
                        if (location != null) {
                                myLocation = location;
                                Toast.makeText(getBaseContext(), "Location 
changed to " +
location.getLatitude() + " " + location.getLongitude(),
Toast.LENGTH_SHORT).show();
                                Log.i("Location", location.getLatitude() + ", " 
+
location.getLongitude());

                                GeoPoint point = new GeoPoint(
                                                (int) (location.getLatitude() * 
1E6),
                                                (int) (location.getLongitude() 
* 1E6));
                                controller_Mapview.animateTo(point);
                                controller_Mapview.setZoom(16);
                                Drawable 
marker=getResources().getDrawable(R.drawable.icon);
                                marker.setBounds(0, 0, 
marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
                                view_Mapview.getOverlays().add(new 
myLocationOverlay(marker));
                                view_Mapview.invalidate();
                                
locationManager.removeUpdates(myLocationListener.this);
                        } else {
                                Log.e("Location", "There was no position 
returned");
                        }
                        Log.i("Activity", "We tried to update loaction");
                }

                @Override
                public void onProviderDisabled(String provider) {
                        // TODO Auto-generated method stub

                }

                @Override
                public void onProviderEnabled(String provider) {
                        // TODO Auto-generated method stub
                        Log.i("Provider", provider.toString());
                }

                @Override
                public void onStatusChanged(String provider, int status, Bundle
extras) {
                        // TODO Auto-generated method stub

                }
        }

        public static final int MY_LOCATION = Menu.FIRST;
        public static final int SUBMIT_LOCATION = Menu.FIRST + 1;
        public static final int EXIT_ID = Menu.FIRST + 2;

        public Drawable marker;

        private final int GPS_TIMEOUT = 5000;

        private LocationManager locationManager;
        private LocationListener locationListener;
        protected Location myLocation = null;
        private String accessToken;

        private MapView view_Mapview;
        private MapController controller_Mapview;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.location);

                Bundle extras = getIntent().getExtras();
                if(extras != null) {
                        accessToken = (String) extras.getString("accessToken");
                }
                accessor.accessToken = accessToken;
                Log.i("Token", accessor.accessToken);

                view_Mapview = (MapView) findViewById(R.id.mapview);
                controller_Mapview = view_Mapview.getController();
                view_Mapview.displayZoomControls(true);
                locationManager = (LocationManager)getSystemService
(Context.LOCATION_SERVICE);
                locationManager.getBestProvider(new Criteria(), true);
        }

        @Override
                protected boolean isRouteDisplayed() {
                return false;
        }

        @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean result = super.onCreateOptionsMenu(menu);
        menu.add(0, MY_LOCATION, 0, R.string.my_location);
        menu.add(0, SUBMIT_LOCATION, 1, R.string.submit_location);
        menu.add(0, EXIT_ID, 2, R.string.menu_exit);
        return result;
    }

        @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
                case MY_LOCATION:
                        locationListener = new myLocationListener();
                        locationManager.requestLocationUpdates(
                                        LocationManager.GPS_PROVIDER,
                                        0,
                                        0,
                                        locationListener
                        );

                        new Handler().postDelayed(new Runnable(){
                    @Override
                    public void run() {
                        locationManager.removeUpdates(locationListener);
                        Toast.makeText(BrightroidMap.this, "GPS Unable to
determine location.  Falling back to last known location.",
Toast.LENGTH_SHORT).show();
                        myLocation = locationManager.getLastKnownLocation
("gps");
                        GeoPoint point = new GeoPoint(
                                                        (int) 
(myLocation.getLatitude() * 1E6),
                                                        (int) 
(myLocation.getLongitude() * 1E6));
                                        controller_Mapview.animateTo(point);
                                        controller_Mapview.setZoom(16);
                                        
marker=getResources().getDrawable(R.drawable.icon);
                                        marker.setBounds(0, 0, 
marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
                                        view_Mapview.getOverlays().add(new 
myLocationOverlay
(marker));
                                        view_Mapview.invalidate();
                    }
                }, GPS_TIMEOUT);

                        break;
                case SUBMIT_LOCATION:

                        try {
                                OAuthMessage result = accessor.newRequestMessage
(OAuthMessage.GET,
                                                        
"http://brightkite.com/places/search.json";,
                                                        OAuth.newList("q", 
myLocation.getLatitude() + "," +
myLocation.getLongitude()));
                                Log.i("Request", result.toString());
                                try {
                                Log.i("Response", 
result.readBodyAsString().toString());
                                } catch (IOException e) {
                                        Log.e("Error", 
e.getMessage().toString());
                                }
                                //JSONObject json_result = new JSONObject(new 
JSONTokener
(result.readBodyAsString()));

                                //Log.i("Location ID", json_result.toString());
                                } catch (OAuthException e) {
                                        // TODO Auto-generated catch block
                                        Log.e("Error", 
e.getMessage().toString());
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        Log.e("Error", 
e.getMessage().toString());
                                } catch (URISyntaxException e) {
                                        // TODO Auto-generated catch block
                                        Log.e("Error", 
e.getMessage().toString());
                                }

                        break;
                case EXIT_ID:
                        finish();
                        break;
        }
        return super.onOptionsItemSelected(item);
    }
}

On Jan 11, 10:54 pm, Tane Piper <[email protected]> wrote:
> Hi there,
>
> Thanks - your examples are a little clearer, but I'm still having
> issues at the same point.
>
> Here is my new code:
> package org.ifies.brightroid;
>
> import android.app.Activity;
> import android.app.AlertDialog;
> import android.os.Bundle;
> import android.view.View;
> import android.view.Window;
> import android.widget.Button;
> import android.util.Log;
> import android.content.DialogInterface;
> import android.content.Intent;
>
> import org.ifies.brightroid.oauth.*;
>
> public class AppSettings extends Activity {
>         private static final String OAUTH_REQUEST = "http://brightkite.com/
> oauth/request_token";
>         private static final String OAUTH_AUTHORIZE = "http://brightkite.com/
> oauth/authorize";
>         private static final String OAUTH_ACCESS = "http://brightkite.com/
> oauth/access_token";
>
>         private static String CONSUMER_KEY = "";
>         private static String CONSUMER_SECRET = "";
>
>         private static Button button_auth;
>         private static Button button_back_from_settings;
>
>         public oAuthClient oauth_client;
>
>         @Override
>         public void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>         setNoTitle();
>         setContentView(R.layout.app_settings);
>
>         button_auth = (Button) findViewById(R.id.button_auth);
>         button_auth.setOnClickListener(new View.OnClickListener(){
>                 @Override
>                 public void onClick(View v) {
>                         try {
>                                 doLoginAuth();
>                         } catch (Exception e) {
>                                 Log.e("Auth Error", 
> e.getMessage().toString());
>                         }
>                 }
>         });
>         button_back_from_settings = (Button) findViewById
> (R.id.button_back_from_settings);
>         button_back_from_settings.setOnClickListener(new
> View.OnClickListener(){
>                         @Override
>                         public void onClick(View v) {
>                                 finish();
>                         }
>         });
>         }
>
>         public void setNoTitle() {
>         requestWindowFeature(Window.FEATURE_NO_TITLE);
>     }
>
>     public void doLoginAuth() throws Exception {
>
>         try{
>                 ClientSettings oauth_client_settings = new ClientSettings();
>                 oauth_client_settings.setOAuthAccessTokenUrl(OAUTH_ACCESS);
>                 oauth_client_settings.setOAuthAuthorizeUrl(OAUTH_AUTHORIZE);
>                 oauth_client_settings.setOAuthRequestTokenUrl(OAUTH_REQUEST);
>                 oauth_client_settings.setConsumerToken(new Token(CONSUMER_KEY,
> CONSUMER_SECRET));
>
>                 oauth_client = new oAuthClient();
>                 oauth_client.setClientSettings(oauth_client_settings);
>                 String auth_url = oauth_client.getUserAuthorizationUrl();
>
>                 Intent auth_intent = new Intent(AppSettings.this,
> oAuthRequest.class);
>                         auth_intent.putExtra("BRIGHTKITE_AUTHORIZE_URL", 
> auth_url);
>                         auth_intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
>                         AppSettings.this.startActivity(auth_intent);
>                 } catch (Exception e) {
>                         Log.e("oAuth Error", e.getMessage().toString());
>                 }
>     }
>
> }
>
> Here I am using some of your code to make a simple library for oAuth.
> My second class is this:
>
> package org.ifies.brightroid;
>
> import java.util.Collection;
>
> import net.oauth.OAuthMessage;
> import android.app.Activity;
> import android.os.Bundle;
> import android.view.View;
> import android.webkit.WebView;
> import android.webkit.WebSettings;
> import android.webkit.WebViewClient;
> import android.widget.Button;
> import android.util.Log;
> import android.content.Intent;
>
> import org.ifies.brightroid.AppSettings;
> import org.ifies.brightroid.oauth.*;
>
> public class oAuthRequest extends Activity {
>
>         private String url;
>         private String request_token;
>
>         private Button button_close_auth;
>         public WebView webView;
>
>         @Override
>         public void onCreate(Bundle savedInstanceState) {
>                 Log.i("Activity", "Yea, This has been invoked!");
>         super.onCreate(savedInstanceState);
>
>         setContentView(R.layout.oauth_request);
>
>         button_close_auth = (Button) findViewById
> (R.id.button_close_auth);
>         button_close_auth.setOnClickListener(new View.OnClickListener()
> {
>                         @Override
>                         public void onClick(View v) {
>
>                         oAuthClient oauth_client = new 
> AppSettings().oauth_client;
>
>                         try {
>                                 oauth_client.fetchAccessToken();
>                                 Token access_token = 
> oauth_client.getClientSettings
> ().getUserSpecificAccessToken();
>                                 Log.i("Public Token", 
> access_token.getPublicToken());
>                                 finish();
>                         } catch (Exception e) {
>                                 Log.e("Access Token Error", 
> e.getMessage().toString());
>                                 finish();
>                         }
>
>                         }
>         });
>
>         Bundle extras = getIntent().getExtras();
>                 if(extras != null) {
>                         url = (String) 
> extras.getString("BRIGHTKITE_AUTHORIZE_URL");
>                 }
>
>         webView = (WebView) findViewById(R.id.wv1);
>         webView.loadUrl(url);
>         WebViewClient webViewClient = new WebViewClient();
>         webViewClient.shouldOverrideUrlLoading(webView, url);
>
>         webView.getSettings().setJavaScriptEnabled(true);
>         webView.getSettings().setLoadsImagesAutomatically(true);
>         webView.getSettings().setSavePassword(false);
>         webView.getSettings().setSaveFormData(false);
>         webView.getSettings().setSupportZoom(true);
>         webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
>         webView.getSettings().setUserAgent(1);
>         webView.getSettings().setSupportMultipleWindows(false);
>         webView.setWebViewClient(webViewClient);
>         Log.i("oAuth Request URL", url);
>         }
>
> }
>
> As you can see, the user gets to auth the app in the web view, then I
> want to get the access token so I can save this, however it always
> seems to crash the app, even in a try/catch block.  Any help would be
> much appreciated!
>
> On Jan 11, 9:22 pm, "Sean Sullivan" <[email protected]> wrote:
>
> > I've written two Android applications that use OAuth.  The first app
> > connects to Fire Eagle via OAuth:
> >    http://code.google.com/p/jfireeagle/wiki/Android
>
> > The second app connects to PortableContacts providers:
>
> >    http://code.google.com/p/jpoco/wiki/Android
>
> > The source code is in SVN on code.google.com
>
> > Sean
>
> > On Sun, Jan 11, 2009 at 10:10 PM, Tane Piper 
> > <[email protected]>wrote:
>
> > > Hi folks,
>
> > > I'm writing an Android app for Brightkite using the oAuth API and I've
> > > hit a bit a a stopping point.  I've had a look at the example for the
> > > Desktop application and I can't work out what it's doing with
> > > overwriting the accessor.  I've pasted the first part of the
> > > authorisation below, which invokes a webview to allow the app to be
> > > authed with the request token:
>
> > > package org.ifies.brightroid;
>
> > > import java.net.URL;
>
> > > import net.oauth.OAuthAccessor;
> > > import net.oauth.OAuthConsumer;
> > > import net.oauth.OAuthServiceProvider;
> > > import net.oauth.client.OAuthClient;
> > > import net.oauth.client.httpclient4.HttpClient4;
> > > import android.app.Activity;
> > > import android.app.AlertDialog;
> > > import android.os.Bundle;
> > > import android.view.View;
> > > import android.view.Window;
> > > import android.widget.Button;
> > > import android.util.Log;
> > > import android.content.DialogInterface;
> > > import android.content.Intent;
>
> > > public class AppSettings extends Activity {
> > >        private static final String OAUTH_REQUEST = "http://brightkite.com/
> > > oauth/request_token <http://brightkite.com/oauth/request_token>";
> > >        private static final String OAUTH_AUTHORIZE = "
> > >http://brightkite.com/
> > > oauth/authorize <http://brightkite.com/oauth/authorize>";
> > >        private static final String OAUTH_ACCESS = "http://brightkite.com/
> > > oauth/access_token <http://brightkite.com/oauth/access_token>";
> > >        private static final String BRIGHTKITE_AUTHORIZE_URL = "http://
> > > brightkite.com/oauth/authorize?oauth_token=";
> > >        private static String CONSUMER_KEY = "";
> > >        private static String CONSUMER_SECRET = "";
>
> > >        public OAuthClient httpClient;
> > >        public OAuthAccessor accessor;
> > >        public OAuthServiceProvider provider;
> > >        public OAuthConsumer consumer;
>
> > >        private BrightRoid br_app;
>
> > >        private static Button button_auth;
> > >        private static Button button_back_from_settings;
>
> > >       �...@override
> > >        public void onCreate(Bundle savedInstanceState) {
> > >        super.onCreate(savedInstanceState);
> > >        setNoTitle();
> > >        setContentView(R.layout.app_settings);
>
> > >        br_app = new BrightRoid();
>
> > >        button_auth = (Button) findViewById(R.id.button_auth);
> > >        button_auth.setOnClickListener(new View.OnClickListener(){
> > >               �...@override
> > >                public void onClick(View v) {
> > >                        try {
> > >                                Log.i("Application", "We get to try very
> > > hard!");
> > >                                doLoginAuth();
> > >                        } catch (Exception e) {
> > >                                Log.e("Auth Error",
> > > e.getMessage().toString());
> > >                        }
> > >                }
> > >        });
> > >        button_back_from_settings = (Button) findViewById
> > > (R.id.button_back_from_settings);
> > >        button_back_from_settings.setOnClickListener(new
> > > View.OnClickListener(){
> > >                       �...@override
> > >                        public void onClick(View v) {
> > >                                finish();
> > >                        }
> > >        });
> > >        }
>
> > >        public void setNoTitle() {
> > >        requestWindowFeature(Window.FEATURE_NO_TITLE);
> > >    }
>
> > >    public void doLoginAuth() throws Exception {
> > >      
>
> ...
>
> read more »
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"OAuth" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/oauth?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to