使用服务:新浪微博
功能分类:社交
支持平台: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>