最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2016年02月27日 15:00:28   作者:冰凍魚  
這篇文章主要介紹了Java使用新浪微博API通過賬號密碼方式登陸微博的實例,一般來說第三方App都是采用OAuth授權認證然后跳轉(zhuǎn)之類的方法,而本文所介紹的賬號方式則更具有自由度,需要的朋友可以參考下

今天下了個新浪微博的API研究研究,目前實現(xiàn)了發(fā)布微博功能,包括帶圖片的微博。為了安全,新浪微博的API中并沒有提供用微博帳號密碼登錄的功能,而是采用OAuth授權,用戶通過瀏覽器訪問新浪網(wǎng)站登錄,登錄成功后,瀏覽器再返回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="發(fā)布" />
</LinearLayout>

一個登錄按鈕,一個輸入框,一個發(fā)布按鈕
因為要接收瀏覽器返回的數(shù)據(jù),所以,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" />
  <!-- 監(jiān)控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);
   //只發(fā)文字
   File f=new File("/sdcard/wallpaper/129567208597069400.jpg");
   weibo.uploadStatus(text,f );
   //發(fā)文字+圖片,這里需要導入commons-httpclient-3.0.1.jar,自己網(wǎng)上下
   //在實際項目上,最好放Thread里,因為按下去的時候按鈕會卡
  } catch (WeiboException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  }});
 }
 
 @Override
 protected void onStart() {
 // TODO Auto-generated method stub
 super.onStart();
 //啟動時執(zhí)行檢測是否來自網(wǎng)頁登錄返回
 //如果是,獲取key和secret
 //否則讀取SharedPreferences
 //若得不到key和secret,直接跳轉(zhuǎn)登錄
 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();
  //跳轉(zhuǎn)到瀏覽器登錄
 
 }
 
 }
 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();
 }
 }
}

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

相關文章

  • JDK1.8、JDK1.7、JDK1.6區(qū)別看這里

    JDK1.8、JDK1.7、JDK1.6區(qū)別看這里

    這篇文章主要為大家詳細介紹了JDK1.8、JDK1.7、JDK1.6中的源碼,對比閱讀,發(fā)現(xiàn)修改問題以及改進點,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • SpringBoot 如何實現(xiàn)異步編程

    SpringBoot 如何實現(xiàn)異步編程

    在SpringBoot的日常開發(fā)中,一般都是同步調(diào)用的,但實際中有很多場景非常適合使用異步來處理,本文就詳細的介紹一下SpringBoot 如何實現(xiàn)異步編程 ,具有一定的參考價值,感興趣的可以了解一下
    2021-12-12
  • SpringBoot解析自定義yml文件的流程步驟

    SpringBoot解析自定義yml文件的流程步驟

    這篇文章主要介紹了SpringBoot解析自定義yml文件的流程步驟,文章通過代碼示例和圖文結(jié)合的方式給大家介紹的非常詳細, 對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-06-06
  • Java重寫(Override)與重載(Overload)區(qū)別原理解析

    Java重寫(Override)與重載(Overload)區(qū)別原理解析

    這篇文章主要介紹了Java重寫(Override)與重載(Overload)區(qū)別原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • Java中Cookie和Session的那些事兒

    Java中Cookie和Session的那些事兒

    Cookie和Session都是為了保持用戶的訪問狀態(tài),一方面為了方便業(yè)務實現(xiàn),另一方面為了簡化服務端的程序設計。這篇文章主要介紹了java中cookie和session的知識,需要的朋友可以參考下
    2016-09-09
  • Java多線程Condition接口原理介紹

    Java多線程Condition接口原理介紹

    這篇文章主要介紹了Java多線程Condition接口原理介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Java源碼解析之超級接口Map

    Java源碼解析之超級接口Map

    今天給各位小伙伴介紹一下超級接口Map,文中對接口Map講的非常詳細,對正在學習java的小伙伴們有很好的幫助喲,需要的朋友可以參考下
    2021-05-05
  • 使用Java的Graphics類進行繪圖的方法詳解

    使用Java的Graphics類進行繪圖的方法詳解

    這篇文章主要介紹了使用Java的Graphics類進行繪圖的方法,是Java的GUI編程的基礎,需要的朋友可以參考下
    2015-10-10
  • Java開發(fā)深入分析講解二叉樹的遞歸和非遞歸遍歷方法

    Java開發(fā)深入分析講解二叉樹的遞歸和非遞歸遍歷方法

    樹是一種重要的非線性數(shù)據(jù)結(jié)構(gòu),直觀地看,它是數(shù)據(jù)元素(在樹中稱為結(jié)點)按分支關系組織起來的結(jié)構(gòu),很象自然界中的樹那樣。樹結(jié)構(gòu)在客觀世界中廣泛存在,如人類社會的族譜和各種社會組織機構(gòu)都可用樹形象表示,本篇介紹二叉樹的遞歸與非遞歸遍歷的方法
    2022-05-05
  • 詳解Java數(shù)組的定義和聲明方法

    詳解Java數(shù)組的定義和聲明方法

    在Java開發(fā)中,數(shù)組是最常用的數(shù)據(jù)結(jié)構(gòu)之一,因此,深入了解Java數(shù)組的定義和聲明是非常必要的,本文將詳細介紹Java數(shù)組的定義和聲明方法,以及其在實際開發(fā)中的應用場景、優(yōu)缺點等方面,需要的朋友可以參考下
    2023-11-11

最新評論

贵州省| 深圳市| 当阳市| 大邑县| 南江县| 嘉祥县| 阿克陶县| 鹤峰县| 来凤县| 肥西县| 故城县| 牡丹江市| 鹤山市| 胶州市| 大宁县| 武邑县| 万宁市| 杭州市| 秦皇岛市| 新宁县| 繁昌县| 安多县| 石棉县| 宜都市| 囊谦县| 双流县| 库尔勒市| 安化县| 霍邱县| 高平市| 科技| 河池市| 民丰县| 八宿县| 康平县| 车致| 长武县| 贵溪市| 云浮市| 鞍山市| 灵璧县|