After a user successfully logs in with an authorized user, we can get Weibo. The Weibo homepage is what you see when you log on to Sina Weibo, that is, you pay attention to your Weibo content. We can use the getHomeTimeline () method of Weibo in the SDK. This method obtains Weibo Json data through access to the API interface. Before using this method, we must set the accessToken value for Weibo.
Place a ListView in the layout. The layout code is as follows:
<?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>
We need to display a progress bar when getting data. When we get the data, close the progress bar! To avoid ANR, we also start a new thread to obtain data and use Handler to update the UI. Code:
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. handl Er; 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;/*** Weibo homepage * @ author Cloay * 05:18:13 */public class HomeActivity extends Activity implements SensorEventListener {priva Te LinearLayout progress; // progress bar private ImageView empty; // private ImageView tryAgain when data is obtained; // refresh and retry private ListView listView; // private List for storing Weibo data <Status> listStatus = new ArrayList <Status> (); // listprivate HomeListAdapter adapter for storing Weibo data; private Weibo mWeibo = OAuthConstant. getInstance (). getWeibo (); // used to shake and refresh Weibo SensorManager sensorManager = null; Vibrator vibrator = null; @ Overrideprotected void onCr Eate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); MainService. allActivity. add (this); setContentView (R. layout. home); // initialize init (); // load the microblog data getStatus ();}/*** load Weibo data in the background */private void getStatus () {if (CheckNetUtil. checkNet (HomeActivity. this) {// check whether the Network is available HashMap <String, Weibo> parms = new HashMap <String, Weibo> (); parms. put ("weibo", mWeibo); Task task = new Task (Task. GET_STATUS, parms); Mai NService. allTask. add (task);} else {progress. setVisibility (View. GONE); tryAgain. setVisibility (View. VISIBLE); AlertDlgUtil. alertDialogNetErr (HomeActivity. this) ;}} private void init () {mWeibo. setOAuthAccessToken (OAuthConstant. getInstance (). getAccessToken (); // you must set 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 () {@ Overridepublic void onItemClick (AdapterView <?> Parent, View view, int position, long id) {if (adapter. listSize = position + 1 & position <65) {// dynamically load the data adapter with multiple Weibo posts. 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); // view the Weibo intent details. putExtras (bundle); startActivity (intent) ;}}) ;}Private Handler handler = new Handler () {@ SuppressWarnings ("unchecked") @ Overridepublic void handleMessage (Message msg) {super. handleMessage (msg); if (msg. what = Constant. GET_DATA_NORMAL) {// if the retrieved data is normal listStatus = (List <Status>) msg. obj; adapter = new HomeListAdapter (HomeActivity. this, listStatus); listView. setAdapter (adapter); progress. setVisibility (View. GONE); // close the progress bar empty. setVisibility (View. GO NE); // hide if (tryAgain. isShown () {tryAgain. setVisibility (View. GONE);} listView. setVisibility (View. VISIBLE);} else if (msg. what = Constant. GET_DATA_ERROR) {// exception in retrieving data SS. setVisibility (View. GONE); tryAgain. setVisibility (View. VISIBLE); empty. setVisibility (View. VISIBLE); ToastUtil. showToast (HomeActivity. this, "an exception occurred while loading data. Please try again! ") ;}}; Public void refresh (Message msg) {handler. sendMessage (msg); System. out. println (" refresh -----------> !!! ");}/*** Refresh method to reload Weibo data */public void refresh () {if (progress. isShown () {ToastUtil. showToast (this, "loading data. Please wait... ");} else {getStatus (); progress. setVisibility (View. VISIBLE) ;}@ Overrideprotected void onResume () {// register sensorManagersuper. onResume (); sensorManager. registerListener (this, sensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER), SensorManager. SENSOR_DELAY_NORMAL) ;}@ Overrideprotected void onPa Use () {super. onPause (); sensorManager. unregisterListener (this); // program end logout} @ Overridepublic void onSensorChanged (SensorEvent event) {int sensorType = event. sensor. getType (); // values [0]: X axis, values [1]: Y axis, values [2]: Z axis 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 () {T OastUtil. showToast (this, "refreshing. Please wait... ") ;}else {refresh (); // re-enable the task // shake the phone, and then follow the vibration prompt ~~ Vibrator. vibrate (300); progress. setVisibility (View. VISIBLE) ;}}@ Overridepublic void onAccuracyChanged (Sensor sensor, int accuracy ){}}
GetHomeTimeline () returns a List <Satus> type of data. We need to customize an Adapter to set Weibo data for listView! Refer to the next article: Sina Weibo development-setting Weibo data for listView.
The running effect is as follows:
There are so many articles. If you have any questions, please leave a message for discussion and study!
Note: Please indicate the source for reprinting!