Today, a microblog research on Sina Weibo has been implemented to launch the microblogging function, including micro blogs with pictures. In order to be safe, Sina Weibo's API does not provide the function that uses the microblogging account password to log in, but uses the OAuth authorization, the user accesses Sina website login through the browser, the login succeeds, the browser returns the key and the secret to the program.
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= "login"/>
<edittext android:id= "@+id/status" Android:layout_ Width= "Fill_parent"
android:layout_height= "300sp" android:hint= "input microblogging message"/> <button
Width= "Fill_parent" android:layout_height= "wrap_content" android:id= "@+id/send" android:text= "
publish"/ >
</LinearLayout>
A login button, an input box, a publishing button
Because you want to receive the data returned by the browser, add a intent-filter when registering the activity 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 "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 Andr Oid:name= "Android.intent.category.DEFAULT"/> <category android:name= "Android.intent.category.BROWSABLE"/ > <data android:scheme= "Sina" android:host= "Weibo"/> <!--monitor Sina://weibo address--> </intent-filter > </activity> </application> <uses-permission androId:name= "Android.permission.INTERNET" ></uses-permission> </manifest>
The
Intent-filter must be written in two paragraphs, and if it is written together, it will not start.
For simplicity, direct the Oauthconstant class in Sina sample to come:
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;
}
}
The
is followed by the most critical main program:
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 ' a ', ' is a ', '
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 ");
The key and Secret login= (Button) Findviewbyid (r.id.login) are set up for application in Sina application open platform;
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 met
Hod stub login ();
Login}}); Send.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated Meth
OD stub String text=string.valueof (Status.gettext ());
Weibo Weibo = new Weibo ();
Weibo.settoken (Key,secret);
try {//weibo.updatestatus (text);
Only text file F=new file ("/sdcard/wallpaper/129567208597069400.jpg");
Weibo.uploadstatus (TEXT,F); Text + pictures, where you need to import Commons-httpclient-3.0.1.jar, your own online///In the actual project, it is best to put in thread, because when pressed button will be card catch (Weiboexception e) {/
TODO auto-generated Catch block E.printstacktrace ();
}
}});
@Override protected void OnStart () {//TODO auto-generated Method Stub Super.onstart (); Execute at startupDetect if a login from the Web page is returned//if yes, get key and secret//otherwise read sharedpreferences//If no key and secret are available, direct jump login uri uri = this.getintent (). GetData ();
if (URI!= null) {//If the browser returns try {requesttoken Requesttoken = Oauthconstant.getinstance (). Getrequesttoken ();
Accesstoken Accesstoken = Requesttoken.getaccesstoken (URI. Getqueryparameter ("Oauth_verifier"));
Oauthconstant.getinstance (). Setaccesstoken (Accesstoken);
Save 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 {//If the user starts sharedpreferences settings = getsharedpreferences ("WeiBo", 0);
Key = Settings.getstring ("Key", "");
Secret = Settings.getstring ("Secret", ""); } if (Key.equals ("") | | Secret.equals ("")) {Toast.maketext (this, "not logged in", ToasT.length_long). Show ();
Login ();
Jump to Browser login}} public void Login () {Weibo Weibo = Oauthconstant.getinstance (). Getweibo ();
Requesttoken Requesttoken;
try {requesttoken =weibo.getoauthrequesttoken ("Sina://weibo");
In order to avoid conflict with similar applications, or to change their own 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 ();
}
}
}
Send a picture need to import Commons-httpclient-3.0.1.jar, otherwise start an error, of course, Weibo4android-1.2.0.jar is not less