正文  第三方集成 > 微博 >

Android JSON对象获取登录新浪微博

Android实现JSON对象获取登录新浪微博[代码] [Java]代码01package com.sfc.ui;0203import java.util.ArrayList;04import java.util.List;0506import com.sfc.ui.adapter.LoginListAdapter;0708import weib......

Android实现JSON对象获取登录新浪微博
[代码] [Java]代码

01package com.sfc.ui;

02

03import java.util.ArrayList;

04import java.util.List;

05

06import com.sfc.ui.adapter.LoginListAdapter;

07

08import weibo4j.User; //这是新浪开发包中的实体类

09import weibo4j.Weibo;//这是新浪开发包中的类

10import weibo4j.WeiboException;//这是新浪开发包中的类

11

12import android.app.Activity;

13import android.app.AlertDialog;

14import android.app.ProgressDialog;

15import android.os.Bundle;

16import android.os.Handler;

17import android.os.Message;

18import android.util.Log;

19import android.view.View;

20import android.view.View.OnClickListener;

21import android.widget.Button;

22import android.widget.ListView;

23import android.widget.Toast;

24

25public class LoginActivity extends Activity implements Runnable {

26 private Button loginButton;

27 private ListView listView;

28 private ProgressDialog loginDialog;

29 private Thread loginThread;

30 private Handler handler;

31 @Override

32 protected void onCreate(Bundle savedInstanceState) {

33 super.onCreate(savedInstanceState);

34 setContentView(R.layout.login);

35 loginButton = (Button)findViewById(R.id.loginButton);

36 List list = new ArrayList();

37 list.add("随便看看");

38 list.add("推荐用户");

39 list.add("热门转发");

40 listView = (ListView)findViewById(R.id.listView);

41 loginThread = new Thread(this);

42

43 handler = new Handler(){

44 //1代表登陆成功 0代表登陆失败

45 public void handleMessage(Message msg) {

46 loginDialog.cancel();

47 switch (msg.what) {

48 case 1:

49 Toast.makeText(LoginActivity.this, "登陆成功 ",3000).show();

50 break;

51 case 0:

52 Toast.makeText(LoginActivity.this, "登陆失败",3000).show();

53 break;

54 }

55 };

56 };

57 listView.setAdapter(new LoginListAdapter(this,list));

58 loginButton.setOnClickListener(new OnClickListener(){

59 public void onClick(View v) {

60 loginDialog = new ProgressDialog(LoginActivity.this);

61 loginDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

62 loginDialog.setMessage("登陆服务器");

63 loginDialog.show();

64 loginThread.start();

65 }

66 });

67 }

68 public void run() {

69 Log.e("loginThread","start");

70 Weibo weibo = new Weibo("XXX@sina.com","XXX");//新浪微博用户名和密码

71 weibo.setHttpConnectionTimeout(5000);

72 Message msa = new Message();

73 try {

74 User user = weibo.verifyCredentials();//该方法会请求新浪微博服务器返回Json对象

75 msa.what=1;

76 } catch (WeiboException e) {

77 msa.what=0;

78 }

79 }

80}