編輯:關於Android編程
新浪分享比較簡單,新浪有提供完整的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"}
本文實例講述了Android互聯網訪問圖片並在客戶端顯示的方法。分享給大家供大家參考,具體如下:1、布局界面<RelativeLayout xmlns:androi
利用TabHost創建標簽式的版面設置,進行不同標簽的切換,顯示不同的背景圖片。效果如下所示: 1、添加6張圖片資源(直接拖入drawable文件夾),分別為gra
1 查看手機CPU信息 cmd——adb shell——cd /proc------cat cpuinfo 2 獲取cpu的是arm指令集,armv7指令集、還是ne
在上一篇文章中,我們學習了LitePal的基本用法,體驗了使用框架來進行創建表操作的便利。然而大家都知道,創建表只是數據庫操作中最基本的一步而已,我們在一開