如何使用Link(url)将Twitter api与我的Android应用程序集成

时间:2014-09-24 14:53:11

标签: android twitter android-twitter

在我的Android应用程序中,我已经使用链接(url)将媒体(照片或视频)共享到Facebook API,为此我使用了以下代码。我需要的是我必须共享所选媒体(照片/视频)使用相同的链接(网址)进入Twitter API。我在互联网上搜索但没有获得所需的解决方案,所以任何人都应该为这项任务提出更好的解决方案。

我如何与Facebook整合
mediaUrl包含链接

的值
 private UiLifecycleHelper uiHelper;
    uiHelper = new UiLifecycleHelper(this, null);
        uiHelper.onCreate(savedInstanceState); 


            try {
                    FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(this)
                            .setLink(mediaUrl)
                            .build();
                    uiHelper.trackPendingDialogCall(shareDialog.present());
                } catch (FacebookException fe){
          Toast.makeText(getApplicationContext(),"Facebook error! Most likely facebook app is not installed.", Toast.LENGTH_SHORT).show();

                }


     @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() {
                @Override
                public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
                    Log.e("Activity", String.format("Error: %s", error.toString()));
                }

                @Override
                public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
                    Log.i("Activity", "Success!");
                }
            });
        }

1 个答案:

答案 0 :(得分:1)

修改

我认为你必须使用sdk。我多次使用Social Auth Lib

Download Social Auth LibrarySee Sample Projects for Reference

代码示例:

// Global Variables

SocialAuthAdapter adapter;


 // Code

adapter = new SocialAuthAdapter(new ResponseListener());

adapter.addProvider(Provider.TWITTER, R.drawable.twitter);
adapter.addCallBack(Provider.TWITTER,
        "http://socialauth.in/socialauthdemo/socialAuthSuccessAction.do");
try {
    adapter.addConfig(Provider.TWITTER,
            StringUtils.TWITTER_PUBLIC_KEY,
            StringUtils.TWITTER_SECERET_KEY, null);
} catch (Exception e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
adapter.authorize(Social_Login.this, Provider.TWITTER);


/**
 * Listens Response from Library
 * 
 */

private final class ResponseListener implements DialogListener {
    @Override
    public void onComplete(Bundle values) {

        // Variable to receive message status
        log("Social Login", "Authentication Successful");

        // Get name of provider after authentication
        final String providerName = values
                .getString(SocialAuthAdapter.PROVIDER);

        log("Social Login", "Provider Name = " + providerName);
        toast(providerName + " connected");

        // Get the User Info
        adapter.getUserProfileAsync(new ProfileDataListener());

    }

    @Override
    public void onError(SocialAuthError error) {
        error.printStackTrace();

        log("Social Login", error.getMessage());
    }

    @Override
    public void onCancel() {
        log("Social Login", "Authentication Cancelled");
    }

    @Override
    public void onBack() {
        log("Social Login", "Dialog Closed by pressing Back Key");
    }
}

 // To receive the profile response after authentication

private final class ProfileDataListener implements
        SocialAuthListener<Profile> {

    @Override
    public void onError(SocialAuthError arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onExecute(String arg0, Profile arg1) {
        // TODO Auto-generated method stub
        log("Custom-UI", "Receiving Data");

        Profile profileMap = arg1;

        toast(profileMap.getDisplayName());


       // Now Post the msg
        final EditText edit = (EditText) findViewById(R.id.editTxt);
        Button update = (Button) findViewById(R.id.update);

        update.setOnClickListener(new OnClickListener() { 

            @Override
            public void onClick(View v) {
                    adapter.updateStatus(edit.getText().toString(), new MessageListener(), false);
            }
        });
    }
}

// To get status of message after authentication
private final class MessageListener implements SocialAuthListener<Integer> {
    @Override
    public void onExecute(String provider, Integer t) {
        Integer status = t;
        if (status.intValue() == 200 || status.intValue() == 201 || status.intValue() == 204)
            Toast.makeText(CustomUI.this, "Message posted on" + provider, Toast.LENGTH_LONG).show();
        else
            Toast.makeText(CustomUI.this, "Message not posted" + provider, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onError(SocialAuthError e) {

    }
}

如果您想通过Twitter官方App发布,这个答案很好......

This code will open the twitter app for tweet, If app is installed in phone...

  // Create intent using ACTION_VIEW and a normal Twitter url: 

  String tweetUrl = String.format("https://twitter.com/intent/tweet?text=%s&url=%s", UrlEncoder.encode("Tweet text"), UrlEncoder.encode("https://www.google.fi/"));

  Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(tweetUrl)); 

   // Narrow down to official Twitter app, if available: 

  List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent, 0); 

  for (ResolveInfo info : matches) { 
       if (info.activityInfo.packageName.toLowerCase().startsWith("com.twitter"))   {   
                intent.setPackage(info.activityInfo.packageName); 
        } 
    } 
   startActivity(intent);

See detailed answer here