程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java應用新浪微博API經由過程賬號暗碼方法上岸微博的實例

Java應用新浪微博API經由過程賬號暗碼方法上岸微博的實例

編輯:關於JAVA

Java應用新浪微博API經由過程賬號暗碼方法上岸微博的實例。本站提示廣大學習愛好者:(Java應用新浪微博API經由過程賬號暗碼方法上岸微博的實例)文章只能為提供參考,不一定能成為您想要的結果。以下是Java應用新浪微博API經由過程賬號暗碼方法上岸微博的實例正文


明天下了個新浪微博的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是弗成少的

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved