Android源碼下載:新浪微博
作者:佚名
自己做的一個小項目,基本實現了新浪微博所提供接口的主要功能,授權登陸,發送,轉發,評論,搜索等等。無網絡也可以讀取數據庫數據離線瀏覽,整個項目中可能有一些BUG,希望童鞋們找到能聯系我!共同學習!
使用服務:新浪微博
功能分類:社交
支持平臺: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>
責任編輯:閆佳明
來源:
網絡整理