Today I have a study on Sina Weibo's API, and now I have realized the function of publishing Weibo, including Weibo with pictures. For security, Sina Weibo's API does not provide the function of logging in with a Weibo account password, but uses OAuth authorization. Users access Sina websites through browsers to log in. After logging in successfully, the browser returns the key and 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="enter Weibo message" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/send" android:text="Publish" /></LinearLayout>
A login button, an input box, and a publish button Because it wants to receive data returned by the browser, you need to add an Intent-Filter when registering an activity on 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"> <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" /> <!-- Monitoring address like sina://weibo--> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET"></uses-permission></manifest>
Intent-filter must be written in two paragraphs. If written together, it will not be started.
For simplicity, just copy the OAuthConstant class in Sina Sample:
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; } }Next is 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 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"); //Set the key and secret login of the application applied on the Sina application open platform = (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(); //Login }}); send.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub login(); //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); //Send only text File f=new File("/sdcard/wallpaper/129567208597069400.jpg"); weibo.uploadStatus(text,f); //Send text + pictures, you need to import commons-httpclient-3.0.1.jar here, and download it yourself.//In actual projects, it is best to put it in Thread, because the button will be stuck when pressed} catch (WeiboException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); //Check when starting, check whether it comes from the web page to log in //If yes, get key and secret //Otherwise read SharedPreferences //If you can't get key and secret, jump to log in directly Uri uri = this.getIntent().getData(); if (uri != null) { //If it is a browser, 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 yet", Toast.LENGTH_LONG).show(); login(); //Skip to browser login} } public void login(){ Weibo weibo = OAuthConstant.getInstance().getWeibo(); RequestToken requestToken; try { requestToken =weibo.getOAuthRequestToken("sina://weibo"); //In order to avoid conflicts with similar applications, please change the URI by yourself Uri uri2 = Uri.parse(requestToken.getAuthenticationURL()+ "&from=xweibo"); OAuthConstant.getInstance().setRequestToken(requestToken); startActivity(new Intent(Intent.ACTION_VIEW, uri2)); } catch (WeiboException e) { e.printStackTrace(); } }}You need to import commons-httpclient-3.0.1.jar when sending images, otherwise an error will be reported. Of course, weibo4android-1.2.0.jar is indispensable.