android新浪分享執行個體

來源:互聯網
上載者:User

新浪分享比較簡單,新浪有提供完整的demo。


android實現新浪的分享功能,分3種分享情況:

純文字的,帶圖片的,圖片為本地圖片(傳入的是圖片在手機的地址),第2種帶圖片的是,網狀圖片,圖片地址為網路地址url

 


首先到新浪開放平台,註冊帳號,建立應用,注意的是請點擊應用,選擇進階資訊,在裡面設定上回調頁面

 

 


回調頁隨便填寫一個就行

 


下載sdk包,把包加入libs下面,引入

 


主類:


package com.sharedemo;


import java.io.IOException;
import java.text.SimpleDateFormat;


import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;


import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.Weibo;
import com.weibo.sdk.android.WeiboAuthListener;
import com.weibo.sdk.android.WeiboDialogError;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.StatusesAPI;
import com.weibo.sdk.android.api.UsersAPI;
import com.weibo.sdk.android.net.RequestListener;


public class MainActivity extends Activity {
private Weibo mWeibo;
private static final String CONSUMER_KEY = "1587758551";// App_key 建立應用獲得
private static final String REDIRECT_URL = "http://www.sina.com";      //回調地址,必須和註冊時候填寫的一致
public static Oauth2AccessToken accessToken;
private ProgressDialog dialog;
private Message message;
private UsersAPI usersAPI;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWeibo = Weibo.getInstance(CONSUMER_KEY, REDIRECT_URL);
accessToken = SaveDate.getDate(this);


}


public void sendContent1(View view) { // 發送純文字的方法
dialog = ProgressDialog.show(this, "處理中", "請稍等");
dialog.setCanceledOnTouchOutside(true);
sendContent();


}


public void getContent(View view) {     //擷取使用者資訊
if (accessToken.isSessionValid()) {
message = new Message();
dialog = ProgressDialog.show(this, "處理中", "請稍等");
usersAPI = new UsersAPI(accessToken);
usersAPI.show(Long.parseLong(SaveDate.get(this, "uid")),
new RequestListener() {


@Override
public void onIOException(IOException arg0) {
Log.i("hck", "onIOException " + arg0);
message.what = 0;
handler.sendMessage(message);
}


@Override
public void onError(WeiboException arg0) {
Log.i("hck", "onError " + arg0);
message.what = 0;
handler.sendMessage(message);
}


@Override
public void onComplete(String arg0) {


message.what = 1;
message.obj = arg0;
message.arg1 = 1;
handler.sendMessage(message);
}
});
} else {
mWeibo.authorize(MainActivity.this, new AuthDialogListener());
}
}


Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (dialog != null) {
dialog.dismiss();
}
if (msg.what == 1 && msg.arg1 != 1) {
Toast.makeText(MainActivity.this, "發送成功", Toast.LENGTH_LONG)
.show();
} else if (msg.arg1 == 1 && msg.what == 1) {
Toast.makeText(MainActivity.this, msg.obj.toString(),
Toast.LENGTH_LONG).show();
Log.i("hck", msg.obj.toString());
} else {
Toast.makeText(MainActivity.this, "發送失敗:" + msg.obj.toString(),
Toast.LENGTH_LONG).show();
}
};
};


public void sendContent2(View view) { // 帶圖片的方法,圖片為本機裡面的圖片
if (accessToken.isSessionValid()) {
dialog = ProgressDialog.show(this, "處理中", "請稍等");
String fiString = Environment.getExternalStorageDirectory()
.toString() + "/sina.png";
new StatusesAPI(accessToken).upload("圖片分享", fiString, "", "",
new listener());
} else {
mWeibo.authorize(MainActivity.this, new AuthDialogListener());
}
}


public void sendContent3(View view) { // 帶圖片的方法,圖片為網狀圖片
if (accessToken.isSessionValid()) {
dialog = ProgressDialog.show(this, "處理中", "請稍等");
new StatusesAPI(accessToken)
.uploadUrlText(
"圖片分享222",
"http://h.hiphotos.baidu.com/album/w%3D2048/sign=ba4fd5d5d0c8a786be2a4d0e5331c83d/d1160924ab18972b4b5775e5e7cd7b899e510aab.jpg",
"", "", new listener());
} else {
mWeibo.authorize(this, new AuthDialogListener());
}
}


private void sendContent() {
if (accessToken.isSessionValid()) {
new StatusesAPI(accessToken).update("新浪微博測試33377777", null, null,
new listener());
} else {
mWeibo.authorize(this, new AuthDialogListener());


}
}


class listener implements RequestListener {


@Override
public void onComplete(String arg0) {
dialog.dismiss();
message = new Message();
message.what = 1;
handler.sendMessage(message);


}


@Override
public void onError(WeiboException arg0) {


dialog.dismiss();
message = new Message();
message.what = 0;
message.obj = arg0;
handler.sendMessage(message);
Log.i("hck", "onError" + arg0 + "");


}


@Override
public void onIOException(IOException arg0) {


dialog.dismiss();
message = new Message();
message.what = 0;
message.obj = arg0;
handler.sendMessage(message);
Log.i("hck", arg0 + "");


}


}


class AuthDialogListener implements WeiboAuthListener {


@Override
public void onComplete(Bundle values) {
String token = values.getString("access_token");
String expires_in = values.getString("expires_in");
String userIdString = values.getString("uid");
accessToken = new Oauth2AccessToken(token, expires_in);
if (accessToken.isSessionValid()) {
SaveDate.save(MainActivity.this, "uid", userIdString);
sendContent();
SaveDate.saveDate(MainActivity.this, accessToken);
}


}


@Override
public void onError(WeiboDialogError e) {
Toast.makeText(getApplicationContext(),
"Auth error : " + e.getMessage(), Toast.LENGTH_LONG).show();
}


@Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "Auth cancel",
Toast.LENGTH_LONG).show();
}


@Override
public void onWeiboException(WeiboException e) {
Toast.makeText(getApplicationContext(),
"Auth exception : " + e.getMessage(), Toast.LENGTH_LONG)
.show();
}


}


}

 

儲存資料的類:


package com.sharedemo;


import com.weibo.sdk.android.Oauth2AccessToken;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;


public class SaveDate {
private static SharedPreferences preferences;


public SaveDate(Context context) {

}
public static void saveDate(Context context, Oauth2AccessToken token) {
preferences = context
.getSharedPreferences("save", Context.MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putString("token", token.getToken());
editor.putLong("expiresTime", token.getExpiresTime());
editor.commit();

}
public static Oauth2AccessToken getDate(Context context)
{
preferences = context
.getSharedPreferences("save", Context.MODE_PRIVATE);
Oauth2AccessToken token = new Oauth2AccessToken();
token.setToken(preferences.getString("token", ""));
token.setExpiresTime(preferences.getLong("expiresTime", 0));
return token;
}
public static void save(Context context,String key,String value)
{
preferences = context
.getSharedPreferences("save", Context.MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putString(key, value);
editor.commit();
}
public static String get(Context context,String key)
{
preferences = context
.getSharedPreferences("save", Context.MODE_PRIVATE);
return preferences.getString(key, null);
}
}

 

xml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity"

    android:gravity="center"

    android:orientation="vertical"

     >

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="擷取使用者資訊"

        android:onClick="getContent"

        />

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="發送一條純文字微博"

        android:onClick="sendContent1"

        />

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="發送一帶圖片的微博"

        android:onClick="sendContent2"

        />

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="發送一條帶圖片和url的微博"

        android:onClick="sendContent3"

        />

</LinearLayout>

 

 


demo:demo

 


//第3個方法的使用需要,你的應用提交審核通過後,在借口管理,申請許可權裡面,申請相應的許可權,才能使用,不然後報

06-24 15:35:02.837: I/hck(32697): onErrorcom.weibo.sdk.android.WeiboException: {"error":"Insufficient app permissions!","error_code":10014,"request":"/2/statuses/upload_url_text.json"}


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.