package org.Base.Weibo;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
import com.example.wb_test.SinaWeiboAuthorize;
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;
/**
* 新浪微博助手
*
* @author jh_zy
*/
public class SinaWeiboHelper {
/* 全局变量 */
private Weibo sinaWeibo;
private String strAppKey = ""; // AppKey
private String strAuthorizationCallbackPage = ""; // 授权回调页
private String strAppSecret = ""; // AppSecret
private String PREFERENCES_NAME = "com_weibo_sdk_android"; // SharedPreferences的名称
private String strRequestToken; // 第一步获取的Token
private String strAccessToken; // 第二步获取的Token
private Oauth2AccessToken oauth2AccessToken;
private String strPostURL; // POST的网址
private String strPostContent; // POST的内容
private String strLat;
private String strLong;
private String strSinaCon;
private String imagePath;
private ProgressDialog dialog;
private Context thisContext;
/** 重写构造函数,使得该类可以控制Activity */
public SinaWeiboHelper(Context context) {
thisContext = context;
}
/** 新浪微博授权页面 */
public void SinaAuthorize() {
sinaWeibo = Weibo.getInstance(strAppKey, strAuthorizationCallbackPage,
null);
sinaWeibo.anthorize(thisContext,
new SinaWeiboHelper.AuthDialogListener());
}
/**
* 发送文字微博
*
* @param strContent
* 发送内容
* @param lat
* 纬度
* @param lon
* 经度
*/
public void SinaPutContent(String strContent, String lat, String lon) {
if (("".equals(strContent))) {
Toast.makeText(thisContext, "无内容发送!", Toast.LENGTH_LONG).show();
} else {
strPostURL = "https://api.weibo.com/2/statuses/update.json";
strPostContent = "access_token="
+ readAccessToken(thisContext).getToken() + "&status="
+ strContent + "&lat=" + lat + "&long=" + lon;
strSinaCon = strContent;
dialog = new ProgressDialog(thisContext);
dialog.setMessage("正在发送请稍后......");
dialog.show();
new Thread(runnable).start();
}
}
/**
* 发送图片微博
*
* @param strContent
* 发送内容
* @param img
* 图片绝对路径
* @param lat
* 纬度
* @param lon
* 经度
*/
public void SinaPutImage(String strContent, String img, String lat,
String lon) {
if (("".equals(strContent))) {
Toast.makeText(thisContext, "无内容发送!", Toast.LENGTH_LONG).show();
} else {
strPostURL = "https://upload.api.weibo.com/2/statuses/upload.json";
strSinaCon = strContent;
imagePath = img;
strLat = lat;
strLong = lon;
dialog = new ProgressDialog(thisContext);
dialog.setMessage("正在发送请稍后......");
dialog.show();
new Thread(runnableImage).start();
}
}
/** 清空授权 */
public void SinaClear() {
clear(thisContext);
}
/* 微博授权的监听 */
private class AuthDialogListener implements WeiboAuthListener {
public void onCancel() {
// TODO Auto-generated method stub
Toast.makeText(thisContext, "Auth cancel", Toast.LENGTH_LONG)
.show();
}
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
strRequestToken = values.getString("code"); // 第一步获取的Token
if (strRequestToken != null) {
strPostURL = "https://api.weibo.com/oauth2/access_token";
strPostContent = "client_id=" + strAppKey + "&client_secret="
+ strAppSecret + "&grant_type=authorization_code"
+ "&code=" + strRequestToken + "&redirect_uri="
+ strAuthorizationCallbackPage;
new Thread(runnable).start();
}
}
public void onError(WeiboDialogError e) {
// TODO Auto-generated method stub
Toast.makeText(thisContext, "Auth error : " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
public void onWeiboException(WeiboException e) {
// TODO Auto-generated method stub
Toast.makeText(thisContext, "Auth exception : " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
private Runnable runnable = new Runnable() {
public void run() {
String sTotalString = null;
try {
URL url = new URL(strPostURL);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream(), "utf-8");
out.write(strPostContent);
out.flush();
out.close();
String sCurrentLine;
sCurrentLine = "";
sTotalString = "";
InputStream l_urlStream;
l_urlStream = connection.getInputStream();
BufferedReader l_reader = new BufferedReader(
new InputStreamReader(l_urlStream));
while ((sCurrentLine = l_reader.readLine()) != null) {
sTotalString += sCurrentLine;
}
Message msg = new Message();
Bundle data = new Bundle();
data.putString("value", sTotalString);
msg.setData(data);
handler.sendMessage(msg);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if ((dialog != null) && (dialog.isShowing())) {
dialog.dismiss();
}
Bundle data = msg.getData();
String val = data.getString("value");
JSONObject o = null;
try {
o = new JSONObject(val);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
if (o.getString("id") != null) { // 说明发微博成功
Toast.makeText(thisContext, "微博发送成功!", Toast.LENGTH_LONG)
.show();
return;
}
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String expires_in = null;
try {
strAccessToken = o.getString("access_token"); // 第二步获取的Token
expires_in = o.getString("expires_in"); // AccessToken的生命周期
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
oauth2AccessToken = new Oauth2AccessToken(strAccessToken,
expires_in);
if (oauth2AccessToken.isSessionValid()) {
keepAccessToken(thisContext, oauth2AccessToken);
SinaWeiboAuthorize.tvSinaAuthorize.setText("已授权");
Toast.makeText(thisContext, "微博授权成功!", Toast.LENGTH_LONG)
.show();
}
}
};
/* 发送带图片的接口,这里必须要用到POST方法 */
private Runnable runnableImage = new Runnable() {
public void run() {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(strPostURL);
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipart
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
wb-test code_by_jh_zy.rar (58个子文件)
wb-test
.project 843B
project.properties 562B
src
com
example
wb_test
MainActivity.java 3KB
TencentWeiboAuthorize.java 3KB
SinaWeiboAuthorize.java 3KB
org
Base
Weibo
TencentAuthorize.java 9KB
TencentWeiboHelper.java 5KB
SinaWeiboHelper.java 11KB
AndroidManifest.xml 1KB
res
raw
mas.jpg 38KB
drawable-ldpi
drawable-hdpi
ic_launcher.png 9KB
ic_action_search.png 3KB
drawable-mdpi
ic_launcher.png 5KB
ic_action_search.png 3KB
layout
tencent_weibo_authorize.xml 2KB
sina_weibo_authorize.xml 2KB
main_activity.xml 889B
drawable-xhdpi
ic_launcher.png 14KB
ic_action_search.png 3KB
values
strings.xml 70B
styles.xml 89B
.settings
org.eclipse.jdt.core.prefs 177B
assets
gen
com
example
wb_test
BuildConfig.java 161B
R.java 2KB
libs
android-support-v4.jar 330KB
Android_SDK.jar 50KB
commons-httpclient-3.0.1.jar 273KB
httpmime-4.1.3.jar 26KB
weibo_sdk_0821.jar 109KB
Android_SDK_component.jar 103KB
.classpath 754B
bin
classes
com
example
wb_test
R$layout.class 497B
R$style.class 398B
BuildConfig.class 347B
R$drawable.class 450B
R$attr.class 343B
SinaWeiboAuthorize.class 3KB
TencentWeiboAuthorize.class 3KB
R$raw.class 387B
R$id.class 953B
R.class 632B
R$string.class 401B
MainActivity.class 4KB
org
Base
Weibo
SinaWeiboHelper$1.class 3KB
TencentAuthorize$5.class 2KB
SinaWeiboHelper$AuthDialogListener.class 3KB
TencentAuthorize$3.class 1KB
TencentAuthorize$1.class 787B
TencentAuthorize$6.class 894B
TencentAuthorize$4.class 1KB
TencentAuthorize.class 10KB
TencentAuthorize$2.class 780B
TencentWeiboHelper.class 6KB
SinaWeiboHelper$2.class 3KB
SinaWeiboHelper.class 8KB
SinaWeiboHelper$3.class 4KB
AndroidManifest.xml 1KB
jarlist.cache 119B
res
共 58 条
- 1
资源评论
- MatrixMinds2014-03-28发现不是最新的版本,不过这个可以参考下。
- dupengwei_32013-10-17我下载的是最新的sdk,发现这个demo不太适合我;不过还是谢谢分享;也能稍微参考;
justiceyves
- 粉丝: 11
- 资源: 43
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功