用于演示Android中sociallib的不同功能的示例项目/教程

关于so​​ciallib:

http://www.androidlibs.com/sociallib.html

http://code.google.com/p/sociallib/wiki/AdvancedGuide

http://code.google.com/p/sociallib/wiki/SocialLibGuide

SocialLib旨在使社交应用程序的开发更加轻松.当前,Social Lib提供了对以下社交网络的访问:

>脸书
>推特
>谷歌
>嗡嗡声
>领英

现在我的场景:

我在Twitter,facebook中创建了应用程序,并生成了API和密钥.

我试图遵循SocialLibs Wiki页面中的指南.

但不幸的是,我被卡住了.我需要有人指出一个有效的连接源,并将其发布到facebook,twitter … etc

这是我尝试过的代码..如果出现一些错误,有人可以纠正我吗?
Java代码:

package com.schogini.socialib2x;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.scribe.oauth.Token;
import org.xml.sax.SAXException;

import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import com.expertiseandroid.lib.sociallib.connectors.SocialNetworkHelper;
import com.expertiseandroid.lib.sociallib.connectors.TwitterConnector;
import com.expertiseandroid.lib.sociallib.exceptions.NotAuthentifiedException;
import com.expertiseandroid.lib.sociallib.model.twitter.TwitterUser;

public class mainAct extends Activity {

    String CONS_KEY = "sdasdadsfdafafafwrfgsdfadfafasfasfsaf";
    String CONS_SEC = "sdasdadsfdafafafwrfgsdfadfafasfasfsaf";
    String CALLBACK = "http://schogini.in";

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

        TwitterConnector twitter = SocialNetworkHelper.createTwitterConnector(CONS_KEY, CONS_SEC, CALLBACK);
        try {
            twitter.requestAuthorization(this);
        } catch (OAuthMessageSignerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthNotAuthorizedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            twitter.authorize(this);
        } catch (OAuthMessageSignerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthNotAuthorizedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Token at = twitter.getAccessToken();
        String token = at.getToken(); //You can store these two strings
        String secret = at.getSecret();//in order to build the token back

        Token myAccessToken = new Token(token, secret);
        twitter.authentify(myAccessToken);

        TwitterUser me;
        int nbFollowers = 0;
        String nickname = null;
        try {
            me = twitter.getUser();
            nickname = me.getUsername(); //Some information is available through method calls
            nbFollowers = me.nbFollowers; //Some is available through object fields
        } catch (OAuthMessageSignerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NotAuthentifiedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } //Retrieves the current user


        TextView tv = (TextView) findViewById(R.id.text1);
        tv.setText("You are logged in as " + nickname + " and you have " + nbFollowers + " followers"); //Now we can print the information we retrieved onscreen

        try {
            twitter.tweet("Tweeting from code");
        } catch (OAuthMessageSignerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NotAuthentifiedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } //A simple tweet

        try {
            twitter.logout(this);
        } catch (OAuthMessageSignerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NotAuthentifiedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } //Providing this code is located in an Activity (or Context) class
    }

}

清单代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.schogini.socialib2x"
      android:versionCode="1"
      android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".mainAct" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <data android:scheme="testapp" android:host="twitter" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET" />    
</manifest>

这是LogCat输出:

04-30 17:08:26.584: INFO/ActivityManager(60): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.schogini.socialib2x/.mainAct } from pid 374
04-30 17:08:26.754: DEBUG/AndroidRuntime(374): Shutting down VM
04-30 17:08:26.825: INFO/ActivityManager(60): Start proc com.schogini.socialib2x for activity com.schogini.socialib2x/.mainAct: pid=384 uid=10040 gids={3003, 1015}
04-30 17:08:26.855: INFO/AndroidRuntime(374): NOTE: attach of thread 'Binder Thread #3' failed
04-30 17:08:26.894: DEBUG/dalvikvm(374): GC_CONCURRENT freed 102K, 69% free 320K/1024K, external 0K/0K, paused 3ms+2ms
04-30 17:08:26.985: DEBUG/jdwp(374): Got wake-up signal, bailing out of select
04-30 17:08:26.985: DEBUG/dalvikvm(374): Debugger has detached; object registry had 1 entries
04-30 17:08:30.385: WARN/Resources(384): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f050000}
04-30 17:08:30.444: ERROR/dalvikvm(384): Could not find class 'oauth.signpost.commonshttp.CommonsHttpOAuthConsumer', referenced from method com.expertiseandroid.lib.sociallib.connectors.TwitterConnector.<init>
04-30 17:08:30.444: WARN/dalvikvm(384): VFY: unable to resolve new-instance 230 (Loauth/signpost/commonshttp/CommonsHttpOAuthConsumer;) in Lcom/expertiseandroid/lib/sociallib/connectors/TwitterConnector;
04-30 17:08:30.454: DEBUG/dalvikvm(384): VFY: replacing opcode 0x22 at 0x0011
04-30 17:08:30.464: DEBUG/dalvikvm(384): VFY: dead code 0x0013-0025 in Lcom/expertiseandroid/lib/sociallib/connectors/TwitterConnector;.<init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
04-30 17:08:30.484: INFO/dalvikvm(384): Could not find method oauth.signpost.commonshttp.CommonsHttpOAuthConsumer.sign, referenced from method com.expertiseandroid.lib.sociallib.connectors.TwitterConnector.signedGetRequest
04-30 17:08:30.505: WARN/dalvikvm(384): VFY: unable to resolve virtual method 1062: Loauth/signpost/commonshttp/CommonsHttpOAuthConsumer;.sign (Ljava/lang/Object;)Loauth/signpost/http/HttpRequest;
04-30 17:08:30.505: DEBUG/dalvikvm(384): VFY: replacing opcode 0x6e at 0x000c
04-30 17:08:30.524: DEBUG/dalvikvm(384): VFY: dead code 0x000f-0018 in Lcom/expertiseandroid/lib/sociallib/connectors/TwitterConnector;.signedGetRequest (Ljava/lang/String;)Lcom/expertiseandroid/lib/sociallib/messages/ReadableResponse;
04-30 17:08:30.544: INFO/dalvikvm(384): Could not find method oauth.signpost.commonshttp.CommonsHttpOAuthConsumer.sign, referenced from method com.expertiseandroid.lib.sociallib.connectors.TwitterConnector.signedPostRequest
04-30 17:08:30.544: WARN/dalvikvm(384): VFY: unable to resolve virtual method 1062: Loauth/signpost/commonshttp/CommonsHttpOAuthConsumer;.sign (Ljava/lang/Object;)Loauth/signpost/http/HttpRequest;
04-30 17:08:30.554: DEBUG/dalvikvm(384): VFY: replacing opcode 0x6e at 0x0020
04-30 17:08:30.564: DEBUG/dalvikvm(384): VFY: dead code 0x0023-002c in Lcom/expertiseandroid/lib/sociallib/connectors/TwitterConnector;.signedPostRequest (Ljava/lang/String;Ljava/util/List;)Lcom/expertiseandroid/lib/sociallib/messages/ReadableResponse;
04-30 17:08:30.585: INFO/dalvikvm(384): Could not find method oauth.signpost.commonshttp.CommonsHttpOAuthConsumer.setTokenWithSecret, referenced from method com.expertiseandroid.lib.sociallib.connectors.TwitterConnector.authentify
04-30 17:08:30.585: WARN/dalvikvm(384): VFY: unable to resolve virtual method 1061: Loauth/signpost/commonshttp/CommonsHttpOAuthConsumer;.setTokenWithSecret (Ljava/lang/String;Ljava/lang/String;)V
04-30 17:08:30.595: DEBUG/dalvikvm(384): VFY: replacing opcode 0x6e at 0x000d
04-30 17:08:30.605: DEBUG/dalvikvm(384): VFY: dead code 0x0010-0012 in Lcom/expertiseandroid/lib/sociallib/connectors/TwitterConnector;.authentify (Lorg/scribe/oauth/Token;)Z
04-30 17:08:30.624: INFO/dalvikvm(384): Could not find method oauth.signpost.commonshttp.CommonsHttpOAuthProvider.retrieveAccessToken, referenced from method com.expertiseandroid.lib.sociallib.connectors.TwitterConnector.authorize
04-30 17:08:30.624: WARN/dalvikvm(384): VFY: unable to resolve virtual method 1064: Loauth/signpost/commonshttp/CommonsHttpOAuthProvider;.retrieveAccessToken (Loauth/signpost/OAuthConsumer;Ljava/lang/String;)V
04-30 17:08:30.634: DEBUG/dalvikvm(384): VFY: replacing opcode 0x6e at 0x0012
04-30 17:08:30.644: DEBUG/dalvikvm(384): VFY: dead code 0x0015-002b in Lcom/expertiseandroid/lib/sociallib/connectors/TwitterConnector;.authorize (Landroid/app/Activity;)V
04-30 17:08:30.675: INFO/dalvikvm(384): Could not find method oauth.signpost.commonshttp.CommonsHttpOAuthConsumer.getToken, referenced from method com.expertiseandroid.lib.sociallib.connectors.TwitterConnector.getAccessToken
04-30 17:08:30.675: WARN/dalvikvm(384): VFY: unable to resolve virtual method 1059: Loauth/signpost/commonshttp/CommonsHttpOAuthConsumer;.getToken ()Ljava/lang/String;
04-30 17:08:30.685: DEBUG/dalvikvm(384): VFY: replacing opcode 0x6e at 0x0004
04-30 17:08:30.694: DEBUG/dalvikvm(384): VFY: dead code 0x0007-0015 in Lcom/expertiseandroid/lib/sociallib/connectors/TwitterConnector;.getAccessToken ()Lorg/scribe/oauth/Token;
04-30 17:08:30.724: INFO/dalvikvm(384): Could not find method oauth.signpost.commonshttp.CommonsHttpOAuthProvider.retrieveRequestToken, referenced from method com.expertiseandroid.lib.sociallib.connectors.TwitterConnector.requestAuthorization
04-30 17:08:30.724: WARN/dalvikvm(384): VFY: unable to resolve virtual method 1065: Loauth/signpost/commonshttp/CommonsHttpOAuthProvider;.retrieveRequestToken (Loauth/signpost/OAuthConsumer;Ljava/lang/String;)Ljava/lang/String;
04-30 17:08:30.735: DEBUG/dalvikvm(384): VFY: replacing opcode 0x6e at 0x0006
04-30 17:08:30.735: DEBUG/dalvikvm(384): VFY: dead code 0x0009-0018 in Lcom/expertiseandroid/lib/sociallib/connectors/TwitterConnector;.requestAuthorization (Landroid/content/Context;)V
04-30 17:08:30.765: WARN/dalvikvm(384): VFY: unable to find class referenced in signature (Loauth/signpost/commonshttp/CommonsHttpOAuthConsumer;)
04-30 17:08:30.784: DEBUG/AndroidRuntime(384): Shutting down VM
04-30 17:08:30.784: WARN/dalvikvm(384): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-30 17:08:30.825: ERROR/AndroidRuntime(384): FATAL EXCEPTION: main
04-30 17:08:30.825: ERROR/AndroidRuntime(384): java.lang.NoClassDefFoundError: oauth.signpost.commonshttp.CommonsHttpOAuthConsumer
04-30 17:08:30.825: ERROR/AndroidRuntime(384):     at com.expertiseandroid.lib.sociallib.connectors.TwitterConnector.<init>(TwitterConnector.java:121)
04-30 17:08:30.825: ERROR/AndroidRuntime(384):     at com.expertiseandroid.lib.sociallib.connectors.TwitterConnector.<init>(TwitterConnector.java:113)
04-30 17:08:30.825: ERROR/AndroidRuntime(384):     at com.expertiseandroid.lib.sociallib.connectors.SocialNetworkHelper.createTwitterConnector(SocialNetworkHelper.java:62)
04-30 17:08:30.825: ERROR/AndroidRuntime(384):     at com.schogini.socialib2x.mainAct.onCreate(mainAct.java:35)
04-30 17:08:30.825: ERROR/AndroidRuntime(384):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-30 17:08:30.825: ERROR/AndroidRuntime(384):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-30 17:08:30.825: ERROR/AndroidRuntime(384):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-30 17:08:30.825: ERROR/AndroidRuntime(384):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-30 17:08:30.825: ERROR/AndroidRuntime(384):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-30 17:08:30.825: ERROR/AndroidRuntime(384):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 17:08:30.825: ERROR/AndroidRuntime(384):     at android.os.Looper.loop(Looper.java:123)
04-30 17:08:30.825: ERROR/AndroidRuntime(384):     at android.app.ActivityThread.main(ActivityThread.java:3683)
04-30 17:08:30.825: ERROR/AndroidRuntime(384):     at java.lang.reflect.Method.invokeNative(Native Method)
04-30 17:08:30.825: ERROR/AndroidRuntime(384):     at java.lang.reflect.Method.invoke(Method.java:507)
04-30 17:08:30.825: ERROR/AndroidRuntime(384):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-30 17:08:30.825: ERROR/AndroidRuntime(384):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-30 17:08:30.825: ERROR/AndroidRuntime(384):     at dalvik.system.NativeStart.main(Native Method)
04-30 17:08:30.854: WARN/ActivityManager(60):   Force finishing activity com.schogini.socialib2x/.mainAct
04-30 17:08:31.425: WARN/ActivityManager(60): Activity pause timeout for HistoryRecord{4066a610 com.schogini.socialib2x/.mainAct}
04-30 17:08:34.014: INFO/Process(384): Sending signal. PID: 384 SIG: 9
04-30 17:08:34.094: WARN/InputManagerService(60): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@405aee30
04-30 17:08:34.495: INFO/ActivityManager(60): Process com.schogini.socialib2x (pid 384) has died.
04-30 17:08:37.704: DEBUG/dalvikvm(284): GC_EXPLICIT freed 8K, 54% free 2543K/5511K, external 716K/1038K, paused 91ms
04-30 17:08:42.408: WARN/ActivityManager(60): Activity destroy timeout for HistoryRecord{4066a610 com.schogini.socialib2x/.mainAct}
04-30 17:08:42.855: DEBUG/dalvikvm(297): GC_EXPLICIT freed 321K, 54% free 2539K/5511K, external 716K/1038K, paused 186ms
04-30 17:08:47.844: DEBUG/dalvikvm(311): GC_EXPLICIT freed 476K, 55% free 2592K/5703K, external 716K/1038K, paused 103ms

提前致谢…

解决方法:

似乎缺少oauth库.是否可能需要包含oauth-signpost

上一篇:javascript-获取Facebook页面帖子


下一篇:php-如何正确使用facebook作用域获取用户电子邮件?