使用服务:新浪微博
功能分类:社交
支持平台:Android
运行环境:Eclipse
开发语言:Java
开发工具:Eclipse
源码大小:6.90MB
源码下载地址:http://down.51cto.com/data/1977280
源码简介
自己做的一个小项目,基本实现了新浪微博所提供接口的主要功能,授权登陆,发送,转发,评论,搜索等等。无网络也可以读取数据库数据离线浏览,整个项目中可能有一些BUG,希望童鞋们找到能联系我!共同学习!
源码运行截图
转发,评论
微博正文
授权登陆
搜索用户
发送微博
评论查看
源码片段
public class Homefragment extends Fragment implements
OnRefreshLoadingMoreListener {
private Context context;
private DragListView lv;
private Myviewadapter adapter;
private Oauth2AccessToken mAccessToken;
/** 微博信息列表 */
private StatusList statuses;
/** 用于获取微博信息流等操作的API */
private StatusesAPI mStatusesAPI;
List<mystatus> mystatuslist;
ContentValues values;
String TABLE_NAME = "weibo";
SQLiteDatabase database;
Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
statuses = (StatusList) msg.obj;
break;
case 1:
statuses = (StatusList) msg.obj;
break;
case 2:
StatusList statuse = (StatusList) msg.obj;
statuses.statusList.addAll(statuses.statusList.size(),
statuse.statusList);
break;
default:
break;
}
adapter = new Myviewadapter(context,statuses,null);
lv.setAdapter(adapter);
}
};
public Homefragment(Context context) {
super();
this.context = context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
getinformation(getActivity().getApplicationContext());
lv = (DragListView) view.findViewById(R.id.dragListView1);
// lv = (MyView) view.findViewById(R.id.myview1);
lv.getFooterViewsCount();
lv.setOnCreateContextMenuListener(this);
lv.setOnRefreshListener(this);
if (!GlobalstaiticData.connect) {
database = Databaseinit.initdatabase(context);
select();
adapter = new Myviewadapter(context,null,mystatuslist);
lv.setAdapter(adapter);
Log.i("123",mystatuslist.size()+"");
}
return view;
}
private void select() {
mystatuslist = new ArrayList<mystatus>();
Cursor cursor = database.query(TABLE_NAME, null, null, null, null,
null, null);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
String screen_name = cursor.getString(cursor
.getColumnIndex("screen_name"));
String source = cursor.getString(cursor.getColumnIndex("source"));
String text = cursor.getString(cursor.getColumnIndex("text"));
String retext = cursor.getString(cursor.getColumnIndex("retext"));
String idstr = cursor.getString(cursor.getColumnIndex("idstr"));
byte[] head = cursor.getBlob(cursor.getColumnIndex("head"));
Mystatus mystatus = new Mystatus(screen_name, source, text, retext,
idstr,head);
mystatuslist.add(mystatus);
}
}
private void getinformation(Context context) {
// 获取当前已保存过的 Token
mAccessToken = AccessTokenKeeper.readAccessToken(context);
// 对statusAPI实例化
mStatusesAPI = new StatusesAPI(mAccessToken);
if (mAccessToken != null && mAccessToken.isSessionValid()) {
mStatusesAPI.friendsTimeline(0L, 0L, 10, 1, false, 0, false,
mListener);
} else {
Toast.makeText(context, "Token不存在", Toast.LENGTH_LONG).show();
}
}
/**
* 微博 OpenAPI 回调接口。
*/
private RequestListener mListener = new RequestListener() {
@Override
public void onComplete(String response) {
Log.i("json", response);
if (!TextUtils.isEmpty(response)) {
if (response.startsWith("{\"statuses\"")) {
// 调用 StatusList#parse 解析字符串成微博列表对象
StatusList statuses = StatusList.parse(response);
Message msg = new Message();
msg.what = 0;
msg.obj = statuses;
handler.sendMessage(msg);
if (statuses != null && statuses.total_number > 0) {
Toast.makeText(getActivity().getApplicationContext(),
"获取微博信息流成功, 条数: " + statuses.statusList.size(),
Toast.LENGTH_LONG).show();
}
} else if (response.startsWith("{\"created_at\"")) {
// 调用 Status#parse 解析字符串成微博对象
Status status = Status.parse(response);
Toast.makeText(getActivity().getApplicationContext(),
"发送一送微博成功, id = " + status.id, Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(getActivity().getApplicationContext(),
response, Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onWeiboException(WeiboException e) {
// ErrorInfo info = ErrorInfo.parse(e.getMessage());
// Toast.makeText(getActivity().getApplicationContext(),
// info.toString(), Toast.LENGTH_LONG).show();
}
};
private void getinformationmore(Context context) {
// 获取当前已保存过的 Token
mAccessToken = AccessTokenKeeper.readAccessToken(context);
// 对statusAPI实例化
mStatusesAPI = new StatusesAPI(mAccessToken);
if (mAccessToken != null && mAccessToken.isSessionValid()) {
mStatusesAPI.friendsTimeline(0L, 0L, 5, 2, false, 0, false,
moreListener);
} else {
Toast.makeText(context, "Token不存在", Toast.LENGTH_LONG).show();
}
}
/**
* 微博 OpenAPI 回调接口。
*/
private RequestListener moreListener = new RequestListener() {
@Override
public void onComplete(String response) {
Log.i("json", response);
if (!TextUtils.isEmpty(response)) {
if (response.startsWith("{\"statuses\"")) {
// 调用 StatusList#parse 解析字符串成微博列表对象
StatusList statuses = StatusList.parse(response);
Message msg = new Message();
msg.what = 2;
msg.obj = statuses;
handler.sendMessage(msg);
lv.onLoadMoreComplete(false);
if (statuses != null && statuses.total_number > 0) {
Toast.makeText(getActivity().getApplicationContext(),
"获取微博信息流成功, 条数: " + statuses.statusList.size(),
Toast.LENGTH_LONG).show();
}
} else if (response.startsWith("{\"created_at\"")) {
// 调用 Status#parse 解析字符串成微博对象
Status status = Status.parse(response);
Toast.makeText(getActivity().getApplicationContext(),
"发送一送微博成功, id = " + status.id, Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(getActivity().getApplicationContext(),
response, Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onWeiboException(WeiboException e) {
// ErrorInfo info = ErrorInfo.parse(e.getMessage());
// Toast.makeText(getActivity().getApplicationContext(),
// info.toString(), Toast.LENGTH_LONG).show();
}
};
private void getinformationupdata(Context context) { // 获取当前已保存过的 Token
mAccessToken = AccessTokenKeeper.readAccessToken(context); //
// 对statusAPI实例化
mStatusesAPI = new StatusesAPI(mAccessToken);
if (mAccessToken != null && mAccessToken.isSessionValid()) {
mStatusesAPI.friendsTimeline(0L, 0L, 10, 1, false, 0, false,
myListener);
} else {
Toast.makeText(context, "Token不存在", Toast.LENGTH_LONG).show();
}
}
/**
*
* 微博 OpenAPI 回调接口。
*/
private RequestListener myListener = new RequestListener() {
@Override
public void onComplete(String response) {
if (!TextUtils.isEmpty(response)) {
if (response.startsWith("{\"statuses\"")) { // 调用
// StatusList#parse
// 解析字符串成微博列表对象
StatusList statuses = StatusList.parse(response);
Message msg = new Message();
msg.what = 1;
msg.obj = statuses;
handler.sendMessage(msg);
lv.onRefreshComplete();
if (statuses != null && statuses.total_number > 0) {
Toast.makeText(getActivity().getApplicationContext(),
"获取微博信息流成功, 条数: " + statuses.statusList.size(),
Toast.LENGTH_LONG).show();
}
} else if (response.startsWith("{\"created_at\"")) { // 调用
// Status#parse
// 解析字符串成微博对象
Status status = Status.parse(response);
Toast.makeText(getActivity().getApplicationContext(),
"发送一送微博成功, id = " + status.id, Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(getActivity().getApplicationContext(),
response, Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onWeiboException(WeiboException e) {
// ErrorInfo info = ErrorInfo.parse(e.getMessage());
// Toast.makeText(getActivity().getApplicationContext(),
// info.toString(), Toast.LENGTH_LONG).show();
}
};
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("更多操作");
// 添加菜单项
menu.add(0, Menu.FIRST, 0, "转发");
menu.add(0, Menu.FIRST + 1, 0, "评论");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case 1:
Intent intent1 = new Intent(getActivity(), RepostActivity.class);
intent1.putExtra("status", statuses.statusList.get(info.position));
startActivity(intent1);
break;
case 2:
Intent intent = new Intent(getActivity(),
CommentsomebadyActivity.class);
intent.putExtra("id", statuses.statusList.get(info.position).idstr);
Log.i("comment", "Weibo:"
+ statuses.statusList.get(info.position).idstr);
startActivity(intent);
break;
default:
break;
}
// Toast.makeText(getActivity().getApplicationContext(),
// "第几项:"+item.getItemId()+" 第几个: "+info.position,
// Toast.LENGTH_LONG).show();
return super.onContextItemSelected(item);
}
/***
* 下拉刷新
*/
@Override
public void onRefresh() {
getinformationupdata(context);
}
/***
* 点击加载更多
*/
@Override
public void onLoadMore() {
getinformationmore(context);
}
}
</mystatus></mystatus>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.
- 228.
- 229.
- 230.
- 231.
- 232.
- 233.
- 234.
- 235.
- 236.
- 237.
- 238.
- 239.
- 240.
- 241.
- 242.
- 243.
- 244.
- 245.
- 246.
- 247.
- 248.
- 249.
- 250.
- 251.
- 252.
- 253.
- 254.
- 255.
- 256.
- 257.
- 258.
- 259.
- 260.
- 261.
- 262.
- 263.
- 264.
- 265.
- 266.
- 267.
- 268.
- 269.
- 270.
- 271.
- 272.
- 273.
- 274.
- 275.
- 276.
- 277.
- 278.
- 279.
- 280.
- 281.
- 282.
- 283.
- 284.
- 285.
- 286.
- 287.
- 288.
- 289.
- 290.
- 291.
- 292.
- 293.
- 294.
- 295.
- 296.
- 297.
- 298.
- 299.
- 300.
- 301.
- 302.
- 303.
- 304.