Info

«Override public void onCre-ate (Bundle savedl nstanceState) \

super . onCreate (savedTnstanceState) ;

setContentView (P.. layout .main) ;

if (!acessTokenlsValid())

showAlertDialog();

Intent intent = new Intent(ActivityMain.this, Act i vi tyShowSa y i ng.cla ss);

startActivity(intent); )

* £IJ®T SharedPreferences acessToken

private boolean acessTokenTsValid() ( SharedPreferences settings = getSharedPreferences( PreferencesU.til.preferencesDouban, 0) ;

String requestToken = sett ings . get.St ri ng ( PreferencesUt il. acessToken, "false");

if (requestToken.equals("false"))

return false;

else return true; }

private void showAlert.Dialog () \

final AlertDialog alert = new AlertDialog.Builder(this).create(); alert. setMessage &S*. AHWi&fT«**)««, : )");

alert. setBut ton ( , new Dialoglnterface.OnClickListener() I

public void onClick(Dialoglnterface dialog, int whichButton) { alert.dismiss();

Intent intent new Intent(ActivityMain.this, ActivityAuth.class); startActivity(intent);

protected void openWebBrowser(String url) {

.i(this.getClass().getName(), "about to launch browser, url: " + url);

Intent, i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse (url)) ; startActivity(i);

• acessTokcnlsValidO^iS^JWf acessToken fclSW'^-ft^, JB^iSftigW ifJ false, acessToken A^ m'^«

• showAlertDialog() <A jH S tj< & W ?X « Sk tf] % ^ fli , fy ffi' ft M Jg , S J* ©tJlj ActivityAuth rlJo

2. ActivityAuth ftj^ifl

ActivityAuth 'II rAlWM'^^WS^H-ffiMirW^^:

package com.douban.Android;

import Android.content.Intent;

import Android.content.SharedPreferences;

import Android.net.Uri;

import Android.os.Bundle;

import Android.util.Log;

import Android.webkit.WebView;

import com.douban.Android.uti1.DoubanUti1; import com.douban.Android.util.PreferencesUtil; import com.google.gdata.client.douban.DoubanService;

• ^author haiyangjy public class ActivityAuth extends Activity (

/** Called when the activity is first created. */ @0verr ide public void onCreate{Bundle savedlnstanceState) \ super.onCreate(savedlnstanceState); setContentView (P.. layout. ma_n) ; setTitle ("B.»f(«j") ; openWebBrowser(getRequestUr 1 () ) ; )

private String getRequestUrl() {

DoubanService myService = new DoubanService("subApplication", DoubanUtil.apiKey, DoubanUti1.secret, true);

String url = myService.aetAuthorizationUr 1(DoubanUtil.callback); Log.i("jinyan", "url " + url);

SharedPreferences settings - getSharedPreferences (

PreferencesUtil.preferencesDouban, 0) ;

settings.edit().putString(PreferencesUtil.oauthToken, myService.getRequestToken()).putString<

PreferencesUtil.oauthTokenSecret, myService.getRequestTokenSecret()).commit();

protected void openWebBrowser(String url) | Log

. i (this . get-Class () . get Name 0 » "about to launch browser, url: " + url);

Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url));

• /}-; gctRcquestUrl()g».iRia. ffiif 1 SJ f (Request Token) f1l*]>V: ftöiMc^WftfflJ (RequestToken Secret), S'J SharedPreferences EkÜL.

• aföiJM^J* (Request Token) ¿J§, ^^lÄ^ffl^S^SiS^W Request Token0 «J JE

lo iA^iifil&l+i openWebBrowser()® jft fity 3. ActivityAuthSuccess

"MS" ScfflÄ-i. Activity ActivityAuthSuccess , t Activity MiÄilSfttTTiÄ^ff»*:

• i3tiB«ttßWi»*^l»(Requcst Token)*«» (Access Token):

package com.douban.Android;

import java . io. IOExcept io.n; import java.util.ArrayList;

import Android.app.Activity;

import Android.content.Intent;

import Android.content.SharedPreferences;

import Android.os.Bundle;

import Android.util.Log;

import com.douban.Android.uti1.DoubanUtil;

import com.douban.Android.util.PreferencesUti1; import com. google .gdata .client .-douban. DoubanService;

import com.google.gdata .data . douban . UserEnt ry;

import com.goog Le.gdata.util.ServiceExcept ion;

public class ActivityAuthSuccess extends AbstractActivity { DoubanService myService; SharedPreferenees se1.1ings ;

/** Called when the activity is first created. */ (¿Override public void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState); setContentView(R.layout.auth success); sdveAccessTcken(); saveUserlnfo();

Intent intent = new Intent(ActivityAuthSuccess.this, ActivityShowSaying.class);

startActivity(intent}; }

private String saveAccessToken() {

myService = new DoubanService("subApplication", DoubanUtil.apiKey, DoubanUti1.secret, true);

String url = myService.getAuthorizationUrl(DOubanUtil.callback);

settings = getSharedPreferences( PreferencesUtil.preferencesDouban, 0);

String requestToken - settings.getString(PreferencesUtil.oauthToken, "false");

String requestTokenSecr^t - set t. ings . getString ( Pref erencesUtil. oauthTokenSecret, "false");

Log.i<"jinyan", "requestToken " +■ requestToken);

myService.setRequestTcken(requestToken);

myService.setRequestTokenSecret(requestTokenSecret);

ArrayList<String> list myService.getAccessToken();

String acessToken = list.get(O);

String acessTokenSecret - list.get(l);

Log.i("jinyan", "acessToken=" + acessToken);

Log.i("jinyan", "acessTokenSecret=" + acessTokenSecret);

settings.edit().putString(PreferencesUti].acessToken, acessToken)

.putSt ring(PreferencesUtil.acessTokenSecret, acessTokenSecret)

private void saveUserInlo()( Log.i("jinyan","saveUserInfo"); try {

DoubanService Service - getAuthDoubanService(); UserEntry userEntry Service.getAuthorizedUser() ;

settings.edit().putString(PreferencesUti1.userName, userEntry.getUid()).commit(); Log.i("jinyan","saveUserInio"♦userEntry.getUid()); \ catch (IOException e) { // TODO Auto-generated catch block e.prIntStackTrace{); ) catch (ServiceException e) \ // TODO Auto-generated catch block e.printStackTrace() ; ) ) )

• ft. saveAccessToken() «»ftii, mift&l T tiW¡fll ^ZM&tfy&Vh ftW&Utift f SharedPrcferenccs S111 *

• &n)¥yivid mm^mm^m id>&mm ffljalBHWAPI »¡»mft'm^, ft-Jjlk saveUscrInfo()M, affJWJfl 71WM API

Java Ki^'Vl') im f nmw.&mnif} id {¡N&. XftMlUUft SharcdPreferences +1W 3WSffi Wf"J •

4. ActivityShowSaying

ActivityShowSaying 'iTM ri&'MM f'^m&'btftMWM:

package com.douban.Android;

import java.io.IOException; import java.uti1.ArrayList;

import Android.content.Context;

import Android.content.SharedPreferences;

import Android.os.Bundle;

import Android.uti].Log;

import Android.view.View;

import Android.view.ViewGroup;

import Android.view.View.OnClickListener;

import Android. widget.AbsLi stView;

import Android.widget.BaseAdapter;

import Android.widget.Button;

impgrt Android. w i.dget . Edi tText ;

import Android.widget.ImageView;

import Android.widget.LinearLayout;

import Android. widget.. L i stView;

import Android.widget.TextView;

import com.douban.Android.util.NetUtil; import com.douban.Android.util.PreferencesUti1; import com. google . gdata . cl ient. douban . DoubanServi.ee; import com.google.gdata.data.PlainTextConstruct; import com.google.gdata.data.TextContent; import com. goog 1 e. gdat.a . da t.a . douban . MiniblogEnt ry ; import coin. goog J e . gdata . da La . douban . Mi ni b Log Feed ; import com.goog1e.gdata . u 111.Set viceExcept ion;

public class ActivityShowSaying extends AbstractActivity I

ListView listView;

OnClickListener listener = null;

EditText editText - null;

ButLon button = null;

SayingLis'..Adapter sayingListAdapter = null;

private class Saying { public String id; public String nickname; public String content; public String time; )

/** Called when the activity is first created. */ ^Override public vo.'.d onCreate(Bundle savedlnstanceState) ( super.onCreate(savedlnstanceState);

// ^.^m setContentView(R.layout.show saying);

listView (ListView) IindViewByld(R.id.saying list); sayingListAdapter = new SayingListAdapter(this, ge t SayingArra r yFromIn t ernet()); 1istView.setAdapter(sayingListAdapter); editText (EditText) findViewById(R.id.content);

listener = new OnClickListener() [ publ i c void onCl ic'k (View v) { setTitle("sending");

DoubanService myService = getAuthDoubanService(); try (

sayingListAdapter.addSaying(myService .createSaying(new PlainTextConstruct((editText .getText () ) . toStringO ) ) ); } catch (IOException e) ( setTitle("sending error"); e.printStackTrace (); ) catch (ServiceException e) { setTitle("sending error"); e.printStackTrace(); )

button (Button) findViewByld(R.id.send); button. set.OnCl i ckListene-r (listener) ; )

private ArtayList<Saying> getSayingArraryFromlnternet() | ArrayList<Saying> sayingList new ArrayList<Saying>(); SharedPro f erences settir.gs - getSharedPreferences ( PreferencesUti1 . pref erer.cesDouban, 0) ;

String iserld = settings.getString(Preferencesütil.userName, "ha i yangjy"); MiniblogFeed mf; try I

DoubanService myService = getAuthDoubanService (); mf = myService.getContactsMiniblogs(userld, 1, 20); for (MinibiogEntry me : mf.getEntries()> ( Saying saying new SayingO;

String nickname - me.get Authors().get(0).getName();

String content ( (TextContenc.) me . getContent () ) . getContent ()

.getPlainText();

saying.content content;

saying.nickname - nickname;

sayingList.add(saying);

} catch (IOException e) ( e.printStackTrace(); } catch (ServiceException e) { e.prinLStackTrace(); )

return sayingList; )

* @param contenL

private void sendSaying(String content) ( if (content null && content.length() == 0) return;

DoubanService myService getAuthDoubanService(); try {

MiniblogEntry miniblogEntry - rr.yService .createSaying(new PlainTextConstruct(content));

e . printStackTrac.e () ; I catch (ServiceException e) {

public class SayingListAdapter extends BaseAdapter (

private Context mContext;

private ArrayList<Saying> sayingList;

public SayingListAdapter(Context context, ArrayList<Saying> sayingList) I mContext = context;

SayingList.Adapter . this . sayingList sayingList ; >

return sayingList.size();

@Override public Object get Item (int position) {

return position; \

(¿Override public long getItemld(int position) {

return position; }

@Override public View getview(int position, View convertView, ViewGroup parent) < SayingView sayingView;

Saying saying sayingList.get(position); if (convertView == nuil) \

sayingView = new SayingView(mContext, saying); } else |

sayingView - (SayingView) convertView; sayingView.setNickname(saying.nickname); sayingVi ew.setContent{saying.content);

return sayingView; )

* @param miniblogEntry */

public void addSaying (MiniblogEnt ry miniblogEnt ry) { Saying newSaying = new Saying();

newSaying. content < (Text Con tent) miniblogEnt ry. get.Content () ) .getContent().getPIainText();

newSaying.nickname = miniblogEntry.getAuthors().get(0).getName(); sayingList.add{0, newSaying);

notif yDataSet.Changed(> ; }

* (¿author Administrator

public class SayingView extends LinearLayout {

private TextView tNickname;

private TextView tContent;

private ImageView imageView;

private Saying saying;

private Context mContext;

public; SayingView (Context context. Saying saying) (

super(context) ;

mCor.text = context;

SayingView.this.saying saying;

imageView = getImageViewFromLocal();

addView(imageView);

tNickname = new TextView(context);

tNickname.setText(saying.nickname*": ");

addView(tNickname, new LinearLaycut.LayoutParams(

LayoutParams.WRAPCONTENT, 48) ) ;

tContent = new TextView(context); tContent.setText(saying.content) ; addView(tContent, new LinearLayout.LayoutParams(

* ctfiftBoett

public void setNickname(String nickname) (

tNickname.setText(nickname*": "); }

* mmzmmzftto®

public void setContent(String content) ( tContent.setText(content) ; )

private ImageView getlmageViewFromLocal() ( ImageView imageView = new ImageView(mContext); imageView.sot imageResource(R.drawable.default_head); imageView.setAdjustViewBounds(true);

imageView.setLayoutParams(new AbsListView.LayoutParams{ LayoutParams.WRAP CONTENT, LayoutParams.WRAP CONTENT));

return imageView; )

JllÜ/r Activity Xtjäftj layout

<LinearLayout xmlns:Aneroid-"http://schemas.Android.com/apk/res/Android" Aruir o id: orie.ntat.ion~" ver: ti-ca 3 " Android: layout width "fill parent" Android:layout_height "fill parent">

<ReI ativeLayout Android:layout_width="fill parent" Android:layout_height "wrap_content">

<Butt, >n Android:id="@+id/send" Android:layout alignParentRight="true"

Andro id:1ayout ma rgi n Le f t="1Odi p" And ro i d:1a yout _width="wrap con tent" Android: layout_height="wrap content " Android: text = />

<EditText Android:id-♦id/content" Android:maxLines-"1" Android:layout marginTop="2dip" Android:layout width="wrap_content" Android:ems "25" Android:layout height "wrap_eontent" Android:autoText="true" Android:capi tal i ze-"sentences" Android:scrol1Horizontaily="false" Android:layouttoLeftOf«"@id/send" Android: layout _alignTop="(?id/send" />

</Relatlvelayout>

<FrameLayout Android:layout width-"fi1l_parent" Android:layout height "Odip" Android:layout weight-"l"> <ListView Android:id="0+id/saying list" Android: layo.ut_width=" £ ill parent " Androi d:1a yout _ he ight = "fill parent" Andro id:drawSe1ec LorOnTop-"false" />

</FrameLayout>

</LinearLayoUt>

. ftffl i4-i2 'I' w «i x m w 'R m tii m in % jj £ # + w

• k%)' + W FrameLayout FffliMJX-J ActivityShowSaying F

• iV&MJ-ft F Saying^. Ä^Nfefi EU* TAH** ff(.«MI,

-arn'oL -t Saying liiW 4 ^ffl, fl-flJÄJIlflft id. J|Jf wife». rtiFic-ffiiimi. «jiWirmarm,1;,

. Saying View J^iJ^'J£ fllMJMo /HÄffJÜ tM f "Vt". U ^MM^mtä 3

. «t Text view, -^i]] }-ki.,s<\ui'<mm. y,^ wiK-Sjtfff^niWrtiF.

• getSayingArraryFromlntcrnetO^l^ft ft Mfl'J uJBMM API AUK£{BfiRMT'-iSWrtft•

• i'i'Muserld M Shared Preferences «ill

• Jt;«))IJ geiAuthDoubanServicc()i?fi8i?if'J -^SRW DoubanServiceo

• WtflifSHJ Douban Service fl'j getContactsMiniblogs()/j'&£j£iJj -fSfl'J 'I^Mo

• MJoifeif J^- MiniblogEntry JgSltfr 'flS'^J-^"itift/Jciftir J»f® ® fft SayingW Arrayl-ist

• SayingListAdapter BaseAdapter lilji«ff], 4 ^ h'UJ^S: . K1!1 I'fc4£ S^jJS!: get View (int position. View convert View, ViewGroup pareni) Hhi).. ii adapter $

ff'j View, Ut View tiivUi List View ^VlM'im % • t* View

• A4E«ffJ*if -FonCreateOZ/iio ifi.ifiaflllkTWft-'lHtfs

• ilfiH flndViewByld (R.id.saying list) ^ ListView, jffiH' new SayingListAdapter(this,get SayingArraryFromIntemet())®$iJ \ SayingListAdapter • iiflf-flUNItt ListView UtnflsiS^lh^ f ,

• iffiH flndViewByld (R.id.send) button, ^Uiifrifc ^ OnClickListener, ff iViLiimm^ft-is. »i&sHfts. Rw&nk&wm

DoubanScrvicc 1% createSaying()tifkfclkQttfy. 5. AbstractActivity

AbstractActivity i! I^ll^il-J Activity. JC'I1 Activity A uthSuccess fll ActivityShowSaying AbstractActivity 0 AbstractActivity i.iiU^fJft f ^FfqgetAuthDoubanService() , i^l^pi'H

nm^ikn m-^iviumimJavaMUM^NAPI Kwmmw

JFtt.lH*tfjAPU JW&iJMTOi K/fr^:

package com.douban.Android;

import Android.app.Act i v i t y;

import: Android, content. SnaredPreferences;

import com.douban.Androii.util.DoubanUtil; import com.douban.Android.util.PreferencesUtii; import com. google . gdata . r.\ i ent. douban . DoufoanService;

public abstract class AbstractActivity extends Activity { /*'

0 0

Post a comment