国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java使用新浪微博API通過賬號密碼方式登陸微博的實例

2019-11-26 14:32:18
字體:
來源:轉載
供稿:網友

今天下了個新浪微博的API研究研究,目前實現了發布微博功能,包括帶圖片的微博。為了安全,新浪微博的API中并沒有提供用微博帳號密碼登錄的功能,而是采用OAuth授權,用戶通過瀏覽器訪問新浪網站登錄,登錄成功后,瀏覽器再返回key和secret給程序。

main.xml:

<?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"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/login" android:text="登錄" /> <EditText android:id="@+id/status" android:layout_width="fill_parent" android:layout_height="300sp" android:hint="輸入微博消息" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/send" android:text="發布" /></LinearLayout>

一個登錄按鈕,一個輸入框,一個發布按鈕
因為要接收瀏覽器返回的數據,所以,AndroidManifest.xml注冊Activity的時候要加個Intent-Filter

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.pocketdigi.weibo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="7" />  <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Main" android:label="@string/app_name">  <intent-filter>  <action android:name="android.intent.action.MAIN" />  <category android:name="android.intent.category.LAUNCHER" />  </intent-filter>  <intent-filter>  <action android:name="android.intent.action.VIEW" />  <category android:name="android.intent.category.DEFAULT" />  <category android:name="android.intent.category.BROWSABLE" />  <data android:scheme="sina" android:host="weibo" />  <!-- 監控sina://weibo這樣的地址 -->  </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET"></uses-permission></manifest>

intent-filter必須分成兩段寫,如果合在一起寫,就啟動不了了。
為了簡便,直接把新浪Sample里的OAuthConstant類拷過來:

package weibo4android.androidexamples; import weibo4android.Weibo;import weibo4android.http.AccessToken;import weibo4android.http.RequestToken; public class OAuthConstant { private static Weibo weibo=null; private static OAuthConstant instance=null; private RequestToken requestToken; private AccessToken accessToken; private String token; private String tokenSecret; private OAuthConstant(){}; public static synchronized OAuthConstant getInstance(){ if(instance==null)  instance= new OAuthConstant(); return instance; } public Weibo getWeibo(){ if(weibo==null)  weibo= new Weibo(); return 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(String token) { this.token = token; } public String getTokenSecret() { return tokenSecret; } public void setTokenSecret(String tokenSecret) { this.tokenSecret = tokenSecret; } }

接下來就是最關鍵的主程序:

package com.pocketdigi.weibo; import java.io.File; import weibo4android.Weibo;import weibo4android.WeiboException;import weibo4android.http.AccessToken;import weibo4android.http.RequestToken;import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast; public class Main extends Activity { /** Called when the activity is first created. */ String key = "", secret = ""; Button login,send; EditText status; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); System.setProperty("weibo4j.oauth.consumerKey", "3997936609"); System.setProperty("weibo4j.oauth.consumerSecret",  "8bc9e3bfd6ae8e3b2b8bda9079918950"); //設置在新浪應用開放平臺申請的應用的key和secret login=(Button)findViewById(R.id.login); send=(Button)findViewById(R.id.send); status=(EditText)findViewById(R.id.status); login.setOnClickListener(new OnClickListener(){   @Override  public void onClick(View v) {  // TODO Auto-generated method stub  login();  //登錄  }}); send.setOnClickListener(new OnClickListener(){   @Override  public void onClick(View v) {  // TODO Auto-generated method stub  String text=String.valueOf(status.getText());  Weibo weibo = new Weibo();  weibo.setToken(key,secret);  try {   //weibo.updateStatus(text);   //只發文字   File f=new File("/sdcard/wallpaper/129567208597069400.jpg");   weibo.uploadStatus(text,f );   //發文字+圖片,這里需要導入commons-httpclient-3.0.1.jar,自己網上下   //在實際項目上,最好放Thread里,因為按下去的時候按鈕會卡  } catch (WeiboException e) {   // TODO Auto-generated catch block   e.printStackTrace();  }  }}); }  @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); //啟動時執行檢測是否來自網頁登錄返回 //如果是,獲取key和secret //否則讀取SharedPreferences //若得不到key和secret,直接跳轉登錄 Uri uri = this.getIntent().getData(); if (uri != null) {  //如果是瀏覽器返回  try {  RequestToken requestToken = OAuthConstant.getInstance()   .getRequestToken();  AccessToken accessToken = requestToken.getAccessToken(uri   .getQueryParameter("oauth_verifier"));  OAuthConstant.getInstance().setAccessToken(accessToken);  // 保存  Editor sharedata = getSharedPreferences("WeiBo", 0).edit();  sharedata.putString("key", accessToken.getToken());  sharedata.putString("secret", accessToken.getTokenSecret());  sharedata.commit();  key = accessToken.getToken();  secret = accessToken.getTokenSecret();  } catch (WeiboException e) {  e.printStackTrace();  } } else {  //如果是用戶自己啟動  SharedPreferences settings = getSharedPreferences("WeiBo", 0);  key = settings.getString("key", "");  secret = settings.getString("secret", ""); } if (key.equals("") || secret.equals("")) {  Toast.makeText(this, "尚未登錄", Toast.LENGTH_LONG).show();  login();  //跳轉到瀏覽器登錄  }  } 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 Intent(Intent.ACTION_VIEW, uri2)); } catch (WeiboException e) {  e.printStackTrace(); } }}

發圖片需要導入commons-httpclient-3.0.1.jar,否則啟動報錯,當然weibo4android-1.2.0.jar是不可少的

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 延长县| 汶上县| 宣武区| 青神县| 宝兴县| 平舆县| 石城县| 道孚县| 镇平县| 九龙县| 台江县| 织金县| 芷江| 屯留县| 周口市| 铜川市| 弥勒县| 宜城市| 兰溪市| 八宿县| 桂平市| 定州市| 治县。| 那坡县| 定安县| 紫云| 侯马市| 桐乡市| 如皋市| 勃利县| 浮山县| 安塞县| 漳州市| 永和县| 泸定县| 神木县| 准格尔旗| 固始县| 湄潭县| 福海县| 农安县|