亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

今天下了個(gè)新浪微博的API研究研究,目前實(shí)現(xiàn)了發(fā)布微博功能,包括帶圖片的微博。為了安全,新浪微博的API中并沒有提供用微博帳號密碼登錄的功能,而是采用OAuth授權(quán),用戶通過瀏覽器訪問新浪網(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>

一個(gè)登錄按鈕,一個(gè)輸入框,一個(gè)發(fā)布按鈕
因?yàn)橐邮諡g覽器返回的數(shù)據(jù),所以,AndroidManifest.xml注冊Activity的時(shí)候要加個(gè)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必須分成兩段寫,如果合在一起寫,就啟動(dòng)不了了。
為了簡便,直接把新浪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;
 }
 
}

接下來就是最關(guān)鍵的主程序:

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");
 //設(shè)置在新浪應(yīng)用開放平臺申請的應(yīng)用的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ā)文字+圖片,這里需要導(dǎo)入commons-httpclient-3.0.1.jar,自己網(wǎng)上下
   //在實(shí)際項(xiàng)目上,最好放Thread里,因?yàn)榘聪氯サ臅r(shí)候按鈕會(huì)卡
  } catch (WeiboException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  }});
 }
 
 @Override
 protected void onStart() {
 // TODO Auto-generated method stub
 super.onStart();
 //啟動(dòng)時(shí)執(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 {
  //如果是用戶自己啟動(dòng)
  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");
  //為了避免與同類應(yīng)用沖突,還是自己改下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ā)圖片需要導(dǎo)入commons-httpclient-3.0.1.jar,否則啟動(dòng)報(bào)錯(cuò),當(dāng)然weibo4android-1.2.0.jar是不可少的

相關(guān)文章

  • Maven 搭建開發(fā)環(huán)境

    Maven 搭建開發(fā)環(huán)境

    這篇文章主要介紹了Maven 如何搭建開發(fā)環(huán)境,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • spring 自動(dòng)注入AutowiredAnnotationBeanPostProcessor源碼解析

    spring 自動(dòng)注入AutowiredAnnotationBeanPostProcessor源碼解析

    這篇文章主要介紹了spring自動(dòng)注入AutowiredAnnotationBeanPostProcessor源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • JAVA類變量及類方法代碼實(shí)例詳解

    JAVA類變量及類方法代碼實(shí)例詳解

    這篇文章主要介紹了JAVA類變量及類方法代碼實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Java使用字節(jié)流復(fù)制文件的方法

    Java使用字節(jié)流復(fù)制文件的方法

    這篇文章主要為大家詳細(xì)介紹了Java使用字節(jié)流復(fù)制文件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Java中Spring Boot+Socket實(shí)現(xiàn)與html頁面的長連接實(shí)例詳解

    Java中Spring Boot+Socket實(shí)現(xiàn)與html頁面的長連接實(shí)例詳解

    這篇文章主要介紹了Java中Spring Boot+Socket實(shí)現(xiàn)與html頁面的長連接實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Java連接mysql數(shù)據(jù)庫代碼實(shí)例程序

    Java連接mysql數(shù)據(jù)庫代碼實(shí)例程序

    這篇文章主要介紹了java連接mysql數(shù)據(jù)庫代碼實(shí)例程序,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • BMIDE環(huán)境導(dǎo)入項(xiàng)目報(bào)編碼錯(cuò)誤解決方案

    BMIDE環(huán)境導(dǎo)入項(xiàng)目報(bào)編碼錯(cuò)誤解決方案

    這篇文章主要介紹了BMIDE環(huán)境導(dǎo)入項(xiàng)目報(bào)編碼錯(cuò)誤解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • IDEA插件之mybatisx?插件使用教程

    IDEA插件之mybatisx?插件使用教程

    這篇文章主要介紹了mybatisx?插件使用教程,包括插件安裝自動(dòng)生成代碼的相關(guān)知識,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • Mybatis的TypeHandler實(shí)現(xiàn)數(shù)據(jù)加解密詳解

    Mybatis的TypeHandler實(shí)現(xiàn)數(shù)據(jù)加解密詳解

    這篇文章主要介紹了Mybatis基于TypeHandler實(shí)現(xiàn)敏感數(shù)據(jù)加密詳解,Typehandler是mybatis提供的一個(gè)接口,通過實(shí)現(xiàn)這個(gè)接口,可以實(shí)現(xiàn)jdbc類型數(shù)據(jù)和java類型數(shù)據(jù)的轉(zhuǎn)換,需要的朋友可以參考下
    2024-01-01
  • 區(qū)分java中String+String和String+char

    區(qū)分java中String+String和String+char

    這篇文章主要向大家詳細(xì)區(qū)分了java中String+String和String+char,感興趣的小伙伴們可以參考一下
    2016-01-01

最新評論