Domanda

I'm trying to make an Android application that connects with the API for Bodymedia data (it's an armband that senses your physical activity levels). I'm using the scribe oauth library. The problem I'm having is that the function "shouldOverrideUrlLoading" is never called.

Here's the code:

package com.android.fitapp;

import org.scribe.builder.ServiceBuilder;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;


public class VisWeight extends Activity {
final static String API_KEY = "[key]";
final static String API_SECRET = "[secret]";       

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.oauth);

    final OAuthService oauthService = new ServiceBuilder()
    .provider(new BodyMediaAPI(API_KEY))
    .apiKey(API_KEY)
    .apiSecret(API_SECRET)
    .build();

    final Token requestToken = oauthService.getRequestToken();
    final String authURL = oauthService.getAuthorizationUrl(requestToken);
    final TextView textView = (TextView)findViewById(R.id.textview);
    final WebView webview = (WebView) findViewById(R.id.webview);
    webview.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading (WebView view, String url) {
            if(url.startsWith("http")){
            webview.setVisibility(View.GONE);
            Uri uri = Uri.parse(url);
            String verifier = uri.getQueryParameter("oauth_verifier");
            Verifier v = new Verifier(verifier);

            Token accessToken = oauthService.getAccessToken(requestToken, v);

            OAuthRequest req = new OAuthRequest(Verb.GET, "http://api.bodymedia.com/v1.0/goal/current?api_key=" + API_KEY);
            oauthService.signRequest(accessToken, req);
            Response response = req.send();
            textView.setText(response.getBody());        

            return true;            
            }
        return super.shouldOverrideUrlLoading(view, url);

        }
    });

    webview.loadUrl(authURL);
    }
}

Any idea what's going wrong?

For reference, this code was adapted from (the presumably working) code provided on http://schwiz.net/blog/2011/using-scribe-with-android/, which demonstrated connecting to the twitter API via scribe.

È stato utile?

Soluzione

Figured it out. The problem was that I hadn't enabled Javascript. There were some minor things to fix once that was running. Feel free to message me if you'd like to see the complete corrected code.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top