用户使用已授权用户登录成功之后,我们就可以获取微博了。微博主页既是用户登录新浪微博时看到的内容,即自己关注用户的微博内容。我们可使用SDK中Weibo的getHomeTimeline()方法,该方法通过对API接口的访问获取微博Json数据。使用该方法前我们一定要为Weibo设置accessToken值。
布局中放置一个ListView,我们要做的主要是为listview设置微博数据,布局代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/white"> <include android:id="@+id/content_progress" layout="@layout/progress_bar" /> <RelativeLayout android:gravity="right" android:paddingRight="30dip" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:layout_width="80dip" android:id="@+id/try_again" android:layout_height="80dip" android:layout_marginTop="10dip" android:background="@drawable/try_again" android:visibility="gone" android:layout_alignParentRight="true"> </ImageView> </RelativeLayout> <ImageView android:id="@+id/empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/empty" android:layout_gravity="center"/> <ListView android:id="@+id/contentList" android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="gone" android:divider="@drawable/divider_horizontal_timeline" android:listSelector="#00000000" android:cacheColorHint="#00000000"> </ListView> </LinearLayout>
package com.cloay.weibo.ui; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import weibo4android.Status; import weibo4android.Weibo; import com.cloay.weibo.R; import com.cloay.weibo.constants.Constant; import com.cloay.weibo.constants.OAuthConstant; import com.cloay.weibo.service.MainService; import com.cloay.weibo.service.Task; import com.cloay.weibo.utils.AlertDlgUtil; import com.cloay.weibo.utils.CheckNetUtil; import com.cloay.weibo.utils.ToastUtil; import com.cloay.weibo.adapter.HomeListAdapter; import android.app.Activity; import android.app.Service; import android.content.Intent; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.Vibrator; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; /** * 微博主页 * @author Cloay * 2012-2-24 * 下午05:18:13 */ public class HomeActivity extends Activity implements SensorEventListener{ private LinearLayout progress; //进度条 private ImageView empty; //数据为获取时 private ImageView tryAgain; //刷新重试 private ListView listView; //存储微博数据 private List<Status> listStatus = new ArrayList<Status>(); //存放微博数据的list private HomeListAdapter adapter; private Weibo mWeibo = OAuthConstant.getInstance().getWeibo(); //用于摇一摇刷新微博 SensorManager sensorManager = null; Vibrator vibrator = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MainService.allActivity.add(this); setContentView(R.layout.home); //初识化 init(); //加载微博数据 getStatus(); } /** * 后台加载微博数据 */ private void getStatus() { if(CheckNetUtil.checkNet(HomeActivity.this)){ //先检查网络是否可用 HashMap<String, Weibo> parms = new HashMap<String, Weibo>(); parms.put("weibo", mWeibo); Task task = new Task(Task.GET_STATUS, parms); MainService.allTask.add(task); }else{ progress.setVisibility(View.GONE); tryAgain.setVisibility(View.VISIBLE); AlertDlgUtil.alertDialogNetErr(HomeActivity.this); } } private void init() { mWeibo.setOAuthAccessToken(OAuthConstant.getInstance().getAccessToken()); //一定要设置 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE); progress = (LinearLayout) findViewById(R.id.content_progress); empty = (ImageView) findViewById(R.id.empty); tryAgain = (ImageView) findViewById(R.id.try_again); listView = (ListView) findViewById(R.id.contentList); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if(adapter.listSize == position + 1 && position < 65){ //动态加载跟多微博数据 adapter.listSize += 15; adapter.notifyDataSetChanged(); }else{ Bundle bundle = new Bundle(); Status status = listStatus.get(position); bundle.putSerializable("status", status); Intent intent = new Intent(HomeActivity.this, StatusDetail.class); //查看详细微博 intent.putExtras(bundle); startActivity(intent); } } }); } private Handler handler = new Handler(){ @SuppressWarnings("unchecked") @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what == Constant.GET_DATA_NORMAL){ //if获取数据正常 listStatus = (List<Status>) msg.obj; adapter = new HomeListAdapter(HomeActivity.this, listStatus); listView.setAdapter(adapter); progress.setVisibility(View.GONE); //关闭进度条 empty.setVisibility(View.GONE); //获取数据后隐藏 if(tryAgain.isShown()){ tryAgain.setVisibility(View.GONE); } listView.setVisibility(View.VISIBLE); }else if(msg.what == Constant.GET_DATA_ERROR){ //获取数据异常 progress.setVisibility(View.GONE); tryAgain.setVisibility(View.VISIBLE); empty.setVisibility(View.VISIBLE); ToastUtil.showToast(HomeActivity.this, "加载数据异常,请重试!"); } } }; public void refresh(Message msg){ handler.sendMessage(msg); System.out.println("refresh ------------->!!!"); } /** * 刷新方法重新加载微博数据 */ public void refresh(){ if(progress.isShown()){ ToastUtil.showToast(this, "正在加载数据,请稍后..."); }else{ getStatus(); progress.setVisibility(View.VISIBLE); } } @Override protected void onResume() { //注册sensorManager super.onResume(); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onPause() { super.onPause(); sensorManager.unregisterListener(this); //程序结束注销 } @Override public void onSensorChanged(SensorEvent event) { int sensorType = event.sensor.getType(); //values[0]:X轴,values[1]:Y轴,values[2]:Z轴 float[] values = event.values; if(sensorType == Sensor.TYPE_ACCELEROMETER){ if((Math.abs(values[0]) > 18 || Math.abs(values[1]) > 18 || Math.abs(values[2]) > 18)){ if(progress.isShown()){ ToastUtil.showToast(this, "正在刷新,请稍后..."); } else{ refresh(); //重新开启任务 //摇动手机后,再伴随震动提示~~ vibrator.vibrate(300); progress.setVisibility(View.VISIBLE); } } } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }getHomeTimeline()返回的是一个List<Satus>类型的数据,我们要自定义一个Adapter为listView设置微博数据!请参考下一篇: 新浪微博开发之为listView设置微博数据。
运行效果如下:
就写那么多,有问题请留言大家一起交流学习!
说明:转载请注明出处!