Thursday, November 14, 2013

Fitbit Integration in Android | Login with Fitbit

Hello friends!

on many demand today i am sharing fitbit account integration in android application.

REF-

http://dev.fitbit.com/

https://github.com/manishsri01/FitbitIntegration

NOTE: please create your application on fitbit and replace with yours credential. Don't use mine in your application.



For this i created a MainActivity.java and put a button for login with fitbit account and open a web-view in next page. see below code hope it will help you-

MainActivity.java-


package com.manish.fitbitintegration;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

Button btnLogin;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnLogin = (Button) findViewById(R.id.button1);

btnLogin.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
FitBitActivity.class);
startActivity(intent);
}
});
}

}

And on FitBitActivity.java class-


private void login() {

try {

HttpResponse response = null;
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 20000);
HttpConnectionParams.setSoTimeout(httpParameters, 20000);
HttpClient client = new DefaultHttpClient(httpParameters);

HttpGet request = new HttpGet(
"http://api.fitbit.com/oauth/request_token?oauth_consumer_key=7af733f021f649bcac32f6f7a4fe2e9a&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1358921319&oauth_nonce=456236281&oauth_callback=http://androidhub4you.com/&oauth_version=1.0&oauth_token=5cefb18d2a80073520211f03f8d75321&oauth_signature=QdVUzMvT6tveGyoPu%2BEevzvo07s%3D");
response = client.execute(request);

BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));

String webServiceInfo = "";
while ((webServiceInfo = rd.readLine()) != null) {
Log.e("****Step 1***", "Webservice: " + webServiceInfo);
String result[]=webServiceInfo.split("=");
String result2 = result[1];
String result3[]=result2.split("&");
authToken = result3[0];
Log.e("Auth token:", "Webservice: " + authToken);

}

} catch (Exception e) {http://google.com/
// TODO: handle exception
e.printStackTrace();
}

}


DOWNLOAD FULL CODE HERE

Thanks!