Skip to content

Commit

Permalink
使用afinal自带的方法实现Cookies持久化
Browse files Browse the repository at this point in the history
  • Loading branch information
fantouch committed May 21, 2013
1 parent 4596062 commit 0a8d77f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 37 deletions.
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
2 changes: 1 addition & 1 deletion src/com/useease/hack10086/activity/MenuActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected void onCreate(Bundle savedInstanceState) {

public void onClick(View v) {
switch (v.getId()) {

case R.id.btnSimulateLogin:
startActivity(new Intent(this, SimulateLoginActivity.class));
break;
Expand Down
5 changes: 2 additions & 3 deletions src/com/useease/hack10086/activity/QueryBalanceActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class QueryBalanceActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.query_balance_activity);

}

public void onClick_QueryBalance(View v) {
Expand Down Expand Up @@ -53,7 +52,7 @@ public void onSuccess(String t) {
responseTxt.setText(Html.fromHtml(t));
}

MyApp.getInstance().hideProgressDialog();
MyApp.getInstance().killDialog();
}

@Override
Expand All @@ -68,7 +67,7 @@ public void onFailure(Throwable t, String strMsg) {
errMsg.append("\n\nstrMsg:\n\n");
errMsg.append(strMsg);

MyApp.getInstance().hideProgressDialog();
MyApp.getInstance().killDialog();

Toast.makeText(QueryBalanceActivity.this, errMsg, Toast.LENGTH_LONG)
.show();
Expand Down
80 changes: 47 additions & 33 deletions src/com/useease/hack10086/application/MyApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.google.gson.reflect.TypeToken;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.PreferencesCookieStore;

import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;
Expand All @@ -38,7 +39,7 @@ public class MyApp extends Application {

private final String PRE_COOKIES = "cookies";
private static MyApp mApp;
private FinalHttp fh;
private MyFinalHttp fh;
private BasicCookieStore cookieStore;

@Override
Expand All @@ -60,52 +61,65 @@ private ProgressDialog getProgressDialog(Context ctx, String msg) {

public void showProgressDialog(Context ctx, String msg) {
if (pd != null && pd.isShowing()) {
hideProgressDialog();
killDialog();
}
getProgressDialog(ctx, msg).show();
}

public void hideProgressDialog() {
public void killDialog() {
if (pd != null && pd.isShowing()) {
pd.dismiss();
pd = null;
}
}

public void saveCookies() {
// if (cookieStore != null) {
// List<Cookie> cookies = cookieStore.getCookies();
//
// Type type = new TypeToken<List<BasicClientCookie>>() {
// }.getType();
//
// String cookiesStr = new Gson().toJson(cookies, type);
// Log.i(TAG, "CookiesToBeSaved = " + cookiesStr);
//
// SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
// preferences.edit().putString(PRE_COOKIES, cookiesStr).commit();
//
// Log.i(TAG, "saveCookies()");
// }
if (cookieStore != null) {
List<Cookie> cookies = cookieStore.getCookies();

Type type = new TypeToken<List<BasicClientCookie>>() {
}.getType();

String cookiesStr = new Gson().toJson(cookies, type);
Log.i(TAG, "CookiesToBeSaved = " + cookiesStr);

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
preferences.edit().putString(PRE_COOKIES, cookiesStr).commit();

Log.i(TAG, "saveCookies()");
PreferencesCookieStore preCookieStore = new PreferencesCookieStore(this);
for (Cookie cookie : cookieStore.getCookies()) {
preCookieStore.addCookie(cookie);
}
}
}

private CookieStore getRestoredCookies() {
// if (cookieStore == null) {
// cookieStore = new BasicCookieStore();
// String cookiesStr = PreferenceManager.getDefaultSharedPreferences(this).getString(
// PRE_COOKIES, "");
// Log.i(TAG, "CookiesToBeRestore = " + cookiesStr);
//
// if (cookiesStr != null && !cookiesStr.equals("")) {
// Type type = new TypeToken<List<BasicClientCookie>>() {
// }.getType();
// List<Cookie> cookies = new Gson().fromJson(cookiesStr, type);
// cookieStore.addCookies(cookies.toArray(new Cookie[cookies.size()]));
//
// Log.i(TAG, "RestoredCookiesSucc");
// } else {
// Log.i(TAG, "RestoredCookiesFail,will use new CookieStore");
// }
// }
// return cookieStore;
if (cookieStore == null) {
cookieStore = new BasicCookieStore();
String cookiesStr = PreferenceManager.getDefaultSharedPreferences(this).getString(
PRE_COOKIES, "");
Log.i(TAG, "CookiesToBeRestore = " + cookiesStr);

if (cookiesStr != null && !cookiesStr.equals("")) {
Type type = new TypeToken<List<BasicClientCookie>>() {
}.getType();
List<Cookie> cookies = new Gson().fromJson(cookiesStr, type);
cookieStore.addCookies(cookies.toArray(new Cookie[cookies.size()]));

Log.i(TAG, "RestoredCookiesSucc");
} else {
Log.i(TAG, "RestoredCookiesFail,will use new CookieStore");
}
PreferencesCookieStore preCookieStore = new PreferencesCookieStore(this);
List<Cookie> cookies = preCookieStore.getCookies();
cookieStore.addCookies(cookies.toArray(new Cookie[cookies.size()]));
}
return cookieStore;
}
Expand All @@ -124,17 +138,17 @@ public static MyApp getInstance() {
return mApp;
}

public FinalHttp getFinalHttp(String charset) {

public MyFinalHttp getFinalHttp(String charset) {
if (fh == null) {
fh = new FinalHttp();
fh = new MyFinalHttp();
fh.configTimeout(20 * 1000);// 20s
fh.configCookieStore(getRestoredCookies());
fh.addHeader(
"User-Agent",
"Mozilla/5.0 (Linux; U; Android 2.3.3; en-us; sdk Build/GRI34) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");
}
fh.setCharset(charset);
fh.configCharset(charset);
return fh;
}

}

0 comments on commit 0a8d77f

Please sign in to comment.