社交API集成是开发Android应用程序时的重要方面。 大多数时候,我们必须连接到多个社交网络,并且有必要使用特定的SDK。 通过CloudRail社交API集成,可以仅使用一个API库并连接到多个社交网络。 这是非常有趣且有用的,而且此API是免费的。
首先,在几种情况下,检索社交用户个人资料很有帮助,这样用户不必再次插入其个人信息。 因此,本文将逐步介绍如何使用Social API集成以及如何使用几行代码来检索用户社交资料。 如果没有CloudRail Social API,我们应该使用不同的社交平台SDK(即Twitter,Facebook等),这将非常烦人。
在本文的结尾,您将知道如何创建一个社交Android应用程序,该应用程序可以连接到不同的社交网络并检索所有信息。 为此,我们将开发一个使用Social API集成的简单android应用,并将逐步指导您。
最终结果如下所示:
设置CloudRail Social API集成库
第一步是使用Android Studio创建一个android项目。 项目准备就绪后,我们将依赖项添加到gradle文件中:
dependencies {
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.cloudrail:cloudrail-si-android:2.7.2'
compile 'com.android.support:design:24.2.1'
}
此外,我们可以创建一个简单的布局,显示一个按钮列表,用于选择要连接的社交网络。 我将跳过此步骤,因为它非常简单。
我们将使用Android Service以及更详细的Android IntentService来实现登录阶段,这样我们就不会出现ANR问题。 服务在登录后使用本地广播接收器发回信息。
社交API整合
下面是一组用于不同社交网络的配置参数。 您会注意到,“艰巨”的工作是配置应用程序,以便它可以访问社交资料。 每个社交网络都有其配置步骤。 一旦配置了应用程序,社交API便非常易于使用。 这些步骤在下面介绍。
Facebook Social API集成
要在Android中使用Facebook集成,必须先授权我们的应用。 这意味着几个步骤:
下一步是添加Facebook登录功能:
最后,在此配置阶段的最后,我们具有身份验证密钥:
就这样。 我们准备使用Facebook登录并检索用户个人资料:
private Profile getFacebookProfile() {
Log.d("Prf", "Facebook Profile");
Profile profile = new Facebook(this, "xxxxxx", "yyy");
return profile;
}
Google加上Social API整合
为了使我们的应用程序能够获取Google plus用户个人资料,我们必须使用google控制台执行一些配置步骤:
- 配置项目
2.创建凭证
3.获取密钥
好的..就这样!!
private Profile getGoogleProfile() {
Log.d("Prf", "Google Profile");
Profile profile = new GooglePlus(this, "153867151354-xx","yy");
return profile;
}
Twitter社交API集成
以同样的方式,我们可以使用Twitter社交API,转到twitter开发人员控制台并创建一个twitter应用程序:
完成配置后,您将获得
在“ 密钥和访问令牌 ”中,您可以在应用程序内部使用令牌:
private Profile getTwitterProfile() {
Log.d("Prf", "Twitter Profile");
Profile profile = new Twitter(this, "xxx", "yyy");
return profile;
}
Github社交API集成
同样的事情也可以在Github中使用,并转到github控制台 :
最后:
private Profile getGithubProfile() {
Log.d("Prf", "Github Profile");
Profile profile = new GitHub(this, "xxx", "yyy");
return profile;
}
LinkedIn社会档案整合
最后覆盖的社交网络是Linkedin 。
要获得Linkedin个人资料,我们使用:
private Profile getLinkedinProfile() {
Log.d("Prf", "Linkedin Profile");
Profile profile = new LinkedIn(this, "xxxx", "yyy");
return profile;
}
最后,您会注意到Social API使用了称为Profile的通用接口,我们可以使用它以统一的方式访问多个社交平台 。 使用此界面,Android应用程序可以检索所有社交资料信息。 我们将在下面介绍。
构建Android应用
现在我们知道了如何访问社交网络个人资料,是时候创建应用了。 让我们创建一个处理UI的MainActivity.java并启动LoginService来执行登录并获取用户配置文件。 该活动如下所示:
public class MainActivity extends AppCompatActivity {
private ResponseReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// use your key
CloudRail.setAppKey("57f669f076b9451e6xxxxx");
Button btnFbk = (Button) findViewById(R.id.btnFbk);
Button btnGp = (Button) findViewById(R.id.btnGoo);
Button btnTwi = (Button) findViewById(R.id.btnTwitter);
Button btnLnk = (Button) findViewById(R.id.btnLink);
Button btnLGit = (Button) findViewById(R.id.btnGithub);
registerReciever(); // We register the receiver to get profile info
btnFbk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startLoginService(SocialProfile.FACEBOOK);
}
});
btnGp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startLoginService(SocialProfile.GOOGLE_PLUS);
}
});
// more social buttons
}
// Start the service, passing the social network selected
private void startLoginService(SocialProfile profile) {
Intent i = new Intent(this, LoginService.class);
i.putExtra(LoginService.PROFILE_INFO, profile );
startService(i);
}
...
}
同时,接收者是一个内部类,用于处理来自LoginService的传入Intent请求:
public class ResponseReceiver extends BroadcastReceiver {
public static final String ACTION = "com.survivingwithandroidp.PROFILE";
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Prf", "Receive info");
String fullName = intent.getStringExtra("fullName");
String email = intent.getStringExtra("email");
String url = intent.getStringExtra("url");
createDialog(fullName, email, url);
}
}
我们通过配置文件详细信息设置意图值。 您可以改用包含所有个人资料信息的个人资料类。 此类必须实现Android Parcelable接口,因此您可以将其从服务传递给活动。
当配置文件信息准备就绪时,应用程序将打开一个对话框,显示详细信息。
此示例显示了Github配置文件。
最后, LoginService.java源代码:
public class LoginService extends IntentService {
public static String PROFILE_INFO = "profile_info";
public LoginService() {
super("LoginService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("Prf", "Login service");
SocialProfile socialProfile = null;
Profile profile = null;
if (intent != null)
socialProfile = (SocialProfile) intent.getSerializableExtra(LoginService.PROFILE_INFO);
switch(socialProfile) {
case FACEBOOK:
profile = getFacebookProfile();
break;
case GOOGLE_PLUS:
profile = getGoogleProfile();
break;
case TWITTER:
profile = getTwitterProfile();
break;
case LINKEDIN:
profile = getLinkedinProfile();
break;
case GITHUB:
profile = getGithubProfile();
break;
}
String fullName = profile.getFullName();
String email = profile.getEmail();
String url = profile.getPictureURL();
sendBroatcast(fullName, email, url);
}
希望在本文结尾处,您能获得有关在Android中使用社交API集成的知识。 您可以使用它来访问社交用户个人资料并检索其信息。 例如,如果您想实现注册功能,它可能会很有用。
翻译自: https://www.javacodegeeks.com/2016/10/social-api-integration-android-access-social-profile-using-cloudrail.html