foursquare integration in android

荒凉一梦 提交于 2019-11-29 05:13:17

Check this code for Authentication Part and Callback Part using Foursquare API:

public class ActivityMain extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn = (Button)findViewById(R.id.button);
    btn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(ActivityMain.this, ActivityWebView.class);
            startActivity(intent);
        }
    });
}
}

ActivityWebView.Java : After Button clicked :

public class ActivityWebView extends Activity{
    private static final String TAG = "ActivityWebView";
    public static final String CLIENT_ID = "01QWBUR2UUFSMMLZXOM5YY0UWCM4BC0F2IOZDJ0S5XGOINQM";
    public static final String OAUTH_CALLBACK_SCHEME = "x-oauthflow-foursquare";
    public static final String OAUTH_CALLBACK_HOST = "callback";
    public static final String CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://" + OAUTH_CALLBACK_HOST;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);       

    String url =
        "https://foursquare.com/oauth2/authenticate" + 
            "?client_id=" + CLIENT_ID + 
            "&response_type=token" + 
            "&redirect_uri=" + CALLBACK_URL;

            WebView webview = (WebView)findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebViewClient(new WebViewClient() {
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            String fragment = "#access_token=";
            int start = url.indexOf(fragment);
            if (start > -1) {
                // You can use the accessToken for api calls now.
                String accessToken = url.substring(start + fragment.length(), url.length());

                Log.v(TAG, "OAuth complete, token: [" + accessToken + "].");

                Toast.makeText(ActivityWebView.this, "Token: " + accessToken, Toast.LENGTH_SHORT).show();
                editor.putString("Access_Token", accessToken);
                editor.commit();
                startActivity(new Intent(ActivityWebView.this,Nearest_Places_View.class));  // After Successfull Login the Web view will Redirected to Nearest_Places_View Activity
            }
        }
    });
    webview.loadUrl(url);
}
}

activity_webview.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" >
 <WebView  
    android:id="@+id/webview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"  />
 </LinearLayout>

Activity after Callback :

public class Nearest_Places_View extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.anylayout);

        // Do your All stuff for Foursquare for example getting User Checkins,Nearest Place
    }
 }

You manifest file should look like this :

<?xml version="1.0" encoding="utf-8"?>
 <manifest 
  xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.foursquare.android.oauth"
  android:versionCode="1"
  android:versionName="1.0">
  <uses-sdk android:minSdkVersion="4" />
   <application     android:icon="@drawable/icon"    android:label="@string/app_name">
        <activity   android:name=".ActivityMain"        android:label="@string/app_name">
           <intent-filter>
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
       </activity>

  <activity
  android:name=".Nearest_Places_View"
  />

<activity android:name=".ActivityWebView" android:launchMode="singleTask">>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="x-oauthflow-foursquare" android:host="callback" />
    </intent-filter>
</activity>

Call back should be same for all the three 1. Activity 2.manifest 3.Settings Page. then only it will work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!