오늘 저는 Sina Weibo의 API에 대한 연구를 받았으며 이제 Weibo를 포함하여 Weibo를 게시하는 기능을 깨달았습니다. 보안의 경우 Sina Weibo의 API는 Weibo 계정 암호로 로그인하는 기능을 제공하지 않지만 OAUTH 인증을 사용합니다. 사용자는 브라우저를 통해 Sina 웹 사이트에 액세스하여 로그인합니다. 성공적으로 로그인 한 후 브라우저는 키를 반환하고 비밀을 프로그램에 반환합니다.
main.xml :
<? xml version = "1.0"encoding = "utf-8"?> <linearlayout xmlns : android = "http://schemas.android.com/apk/res/android"android : "vertical"andtical "Android : layout_width ="fill_parent "andlayout_height ="andlayout_height = " Android : layout_width = "fill_parent"Android : layout_height = "wrap_content"Android : id = "@+id/login"android : text = "login"/<edittext android : id = "@+id/status"android : layout_width = "fill_parent"and hine _height = "300SP"and hne : hne _ hne _ hne _height = "@+id/status. 메시지 " /> <버튼 안드로이드 : layout_width ="fill_parent "Android : layout_height ="wrap_content "android : id ="@+id /send "android :"텍스트 = "게시" /> < /linearlayout>
로그인 버튼, 입력 상자 및 게시 버튼은 브라우저에서 반환 된 데이터를 수신하려면 AndroidManifest.xml에 활동을 등록 할 때 의도 필터를 추가해야합니다.
<? xml version = "1.0"encoding = "utf-8"?> <manifest xmlns : android = "http://schemas.android.com/apk/res/android"package = "com.pocketdigi.weibo"android : versioncode = "1" "and versionname ="1.0 "> minsdk android : sdk android : sdk android. /> <Application Android : icon = "@drawable /icon"android : label = "@String /app_name"> <intent-filter> <action android : name = "android.intent.action.main" /> <카테고리 안드로이드 : name = "Android.category.launcher" /> <intent-filter> <action filter> Android : name = "android.intent.action.view" /> <카테고리 Android : name = "android.intent.category.default" /> <cat </actives> </application> <use-permission android : name = "android.permission.internet"> </use-permission> </manifest>
의도 필터는 두 단락으로 작성되어야합니다. 함께 작성되면 시작되지 않습니다.
간단히려면 Sina 샘플에서 Oauthconstant 클래스를 복사하기 만하면됩니다.
패키지 weibo4android.androidexamples 패키지; import weibo4android.weibo; import weibo4android.http.accesstoken; import weibo4android.http.requesttoken; 공개 클래스 Oauthconstant {private static weibo weibo = null; 개인 정적 OAuthConstant 인스턴스 = NULL; 비공개 요청 테이크 요청 송; 개인 Accessoken AccessToken; 개인 문자열 토큰; 개인 문자열 토큰 세트; private oauthconstant () {}; public static synchronized oauthconstant getinstance () {if (instance == null) instance = new OauthConstant (); 반환 인스턴스; } public weibo getweibo () {if (weibo == null) weibo = new weibo (); 반환 weibo; } public AccessToken getAccessToken () {return accessToken; } public void setAccessToken (AccessToken AccessToken) {this.accesstoken = accessToken; this.token = accessToken.getToken (); this.tokensecret = accessToken.getTokenSecret (); } public requestToken getRequestToken () {return requestToken; } public void setRequestToken (requestToken requestToken) {this.requestToken = requestToken; } public String getToken () {return token; } public void settoken (문자열 토큰) {this.token = 토큰; } public String getTokenSecret () {return tokensecret; } public void settokensecret (String tokensecret) {this.tokensecret = tokensecret; }}다음은 가장 중요한 주요 프로그램입니다.
패키지 com.pocketDigi.Weibo; import java.io.file; 수입 weibo4android.weibo; weibo4android.weiboException 가져 오기; import weibo4android.http.accesstoken; weibo4android.http.requesttoken import; android.app.Activity 가져 오기; android.content.intent import; android.content.sharedpreferences import; android.content.sharedpreferences.editor import; android.net.uri 가져 오기; android.os.bundle 가져 오기; Import Android.view.view; import Android.view.view.onclicklistener; import Android.widget.button; import Android.widget.edittext; import Android.widget.toast; 공개 클래스 메인은 활동이 처음으로 생성 될 때 호출되는 활동 { /**를 확장합니다. */ string key = "", Secret = ""; 버튼 로그인, 보내기; edittext 상태; @override public void onecreate (Bundle SavedInstancestate) {super.oncreate (savedinstancestate); SetContentView (R.Layout.Main); System.SetProperty ( "weibo4j.oauth.consumerkey", "3997936609"); System.SetProperty ( "weibo4j.oauth.ConsumersEcret", "8BC9E3BFD6AE8E3B2B8BDA9079918950"); // SINA 응용 프로그램에 적용된 응용 프로그램의 키 및 비밀 로그인을 설정 Open Platform = (Button) FindViewById (r.id.login); send = (버튼) findViewById (r.id.send); 상태 = (editText) findViewById (r.id.status); login.setonclicklistener (new onclicklistener () {@override public void onclick (view v) {// todo auto-auto-auto-renated method stub login (); // login}}); send.setonclicklistener (new onclicklistener () {@override public void onclick (view v) {// todo auto-auto-reconeated method stub login (); // login}); send.setonclicklistener (new onclicklistener () {@override public void onclick (view v) {// todo auto-auto-regenated method stub string text = string.valueof (status.gettext () 텍스트 파일 F = "/sdcard/ballpaper/129567208597069400.jpg"); (weiboexception e) {// todo 자동 생성 캐치 블록 e.printstacktrace (); } @override protected void onstart () {// todo 자동 생성 메소드 스터브 super.onstart (); // 시작시 확인하고 웹 페이지에서 로그인 할 것인지 확인하십시오. if (uri! = null) {// 브라우저 인 경우 {requestToken requestToken = OauthConstant.getInstance () .getRequestToken (); AccessToken AccessToken = requestToken.getAccessToken (uri .getQueryParameter ( "Oauth_Verifier")); OAuthConstant.getInstance (). setAccessToken (AccessToken); // 편집기 저장 sharedata = getsharedpreferences ( "wibo", 0) .edit (); sharedata.putString ( "키", accessToken.getToken ()); sharedata.putString ( "비밀", accessToken.getTokenSecret ()); sharedata.commit (); key = accessToken.getToken (); Secret = accessToken.getTokenSecret (); } catch (weiboException e) {e.printstacktrace (); }} else {// 사용자가 sharedpreferences 설정 = getsharedpreferences ( "wibo", 0); key = settings.getString ( "key", ""); Secret = settings.getString ( "Secret", ""); } if (key.equals ( "") || secret.equals ( "")) {Toast.Maketext (이것은 "아직 로그인되지 않은", 토스트 .length_long) .show (); 로그인(); // 브라우저 로그인으로 건너 뛰기}} public void login () {weibo weibo = oauthconstant.getInstance (). getWeibo (); requestToken requestToken; try {requestToken = weibo.getoauthrequesttoken ( "sina : // weibo"); // 유사한 응용 프로그램과의 충돌을 피하기 위해 URI를 직접 변경하십시오. uri uri2 = uri.parse (requestToken.getAuthenticationUrl ()+ "& from = xweibo"); OAuthConstant.getInstance (). setRequestToken (requestToken); startActivity (new 의도 (의도 .Action_View, URI2)); } catch (weiboException e) {e.printstacktrace (); }}}이미지를 보낼 때 commons-httpclient-3.0.1.jar를 가져와야합니다. 그렇지 않으면 오류 가보고됩니다. 물론 Weibo4android-1.2.0.jar는 필수 불가결합니다.