如何在android上的linkedin上发布消息?

时间:2013-05-14 12:24:11

标签: android linkedin

我是android的新手。在我的应用程序中,有一个要求,当我点击分享按钮时,我需要分享在墙上链接的东西。我该怎么办?请任何人帮助我。

提前感谢。

2 个答案:

答案 0 :(得分:1)

我正在使用socialauth for android在linkedin和twitter上发布我的消息。

对于更基本的方法,您可以浏览this

以下是socialauth的一个示例:

首先你必须从here下载socialauth-android-sdk-2.0.zip

然后从dist文件夹中复制jar并将其粘贴到项目的libs文件夹中。

现在,

linkedin上注册您的申请并获取API密钥和密钥

将文件“oauth_consumer.properties”从zip的assets文件夹复制到项目的assets文件夹,并编辑该文件并在linkedin部分写下您的API密钥和密钥

#LinkedIn
api.linkedin.com.consumer_key = *************
api.linkedin.com.consumer_secret = **************

还可以从zip的示例文件夹中提供的任何示例中获取linkedin。

最后,相应地更改您的活动(下面给出的活动),

public class ShareActivity extends Activity implements OnClickListener {

    EditText shareText; 

    SocialAuthAdapter shareAdapter;

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

        button = (Button) findViewById(R.id.share);
        button.setOnClickListener(this);

        ImageView product_image = (ImageView) findViewById(R.id.share_image);

        shareText = (EditText) findViewById(R.id.share_text);

        shareAdapter = new SocialAuthAdapter(new ResponseListener());

        shareAdapter.addProvider(Provider.LINKEDIN, R.drawable.linkedin);


    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {


        case R.id.share:
            shareAdapter.authorize(this, Provider.LINKEDIN);
            break;
        }

    }

    private final class ResponseListener implements DialogListener {

        @Override
        public void onComplete(Bundle values) {


            shareAdapter.updateStatus(shareText.getText().toString());

            Toast.makeText(getApplicationContext(), "Message Posted",
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SocialAuthError e) {
            Log.v("Share", "OMG ERROR!!!!" + e.getMessage());
        }

        @Override
        public void onCancel() {
            // Toast.makeText(Share.this, "Cancelled",
            // Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onBack() {
            // Toast.makeText(Share.this, "Back", Toast.LENGTH_SHORT).show();
        }

    }

}

答案 1 :(得分:0)

使用它,它可以解决您的问题

share.setOnClickListener(new OnClickListener() {
@Override

 public void onClick(View v) {

        String share = et.getText().toString();
        if (null != share && !share.equalsIgnoreCase("")) {

            OAuthConsumer consumer = new CommonsHttpOAuthConsumer(Config.LINKEDIN_CONSUMER_KEY, Config.LINKEDIN_CONSUMER_SECRET);
            consumer.setTokenWithSecret(accessToken.getToken(), accessToken.getTokenSecret());

            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost post = new HttpPost("https://api.linkedin.com/v1/people/~/shares");
            try {
                consumer.sign(post);
            } catch (OAuthMessageSignerException e) {
                e.printStackTrace();
            } catch (OAuthExpectationFailedException e) {
                e.printStackTrace();
            } catch (OAuthCommunicationException e) {
                e.printStackTrace();
            } // here need the consumer for sign in for post the share
            post.setHeader("content-type", "text/XML");
            byte[] data = null;
         try {
        ileInputStream fis = new FileInputStream(imgUrl1);
        Bitmap bi = BitmapFactory.decodeStream(fis);
        ByteArrayOutputStream baos = new ByteArrayOutputStream()
               bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        data = baos.toByteArray();  
        }
          catch (FileNotFoundException e) 
           { 
         e.printStackTrace();
     Log.d("onCreate", "debug error  e = " + e.toString());
       }     

     String myEntity = "<share><comment>"+  text +"</comment>    <content><submitted-image-url>data</submitted-image-url></content><visibility><code>anyone</code></visibility></share>";

          try {
                post.setEntity(new StringEntity(myEntity));
                org.apache.http.HttpResponse response = httpclient.execute(post);
                Toast.makeText(LinkedInSampleActivity.this,
                        "Shared sucessfully", Toast.LENGTH_SHORT).show();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            Toast.makeText(LinkedInSampleActivity.this,
                    "Please enter the text to share",
                    Toast.LENGTH_SHORT).show();
        }
    }
});

}