Skip to content

Commit

Permalink
yoyiyi
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyiyi committed Jul 25, 2017
1 parent 12ac99f commit 47b76a4
Show file tree
Hide file tree
Showing 9 changed files with 663 additions and 20 deletions.
39 changes: 23 additions & 16 deletions app/src/main/java/com/yoyiyi/soleil/base/BaseRVMoreActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yoyiyi.soleil.utils.AppUtils;
import com.yoyiyi.soleil.utils.NetworkUtils;
import com.yoyiyi.soleil.widget.CustomLoadMoreView;


/**
* @author zzq 作者 E-mail: [email protected]
* @date 创建时间:2017/6/29 17:22
* 描述:上拉加载更多
* 描述:
*/

public abstract class BaseRVMoreActivity<T extends BaseContract.BasePresenter, K, H extends BaseQuickAdapter> extends BaseRefreshActivity<T, K> implements
Expand All @@ -25,30 +26,35 @@ public abstract class BaseRVMoreActivity<T extends BaseContract.BasePresenter, K
protected void initRecyclerView() {
super.initRecyclerView();
mAdapter.setLoadMoreView(new CustomLoadMoreView());
//设置加载更多
// mAdapter.setOnLoadMoreListener(this, mRecycler);


}

@Override
public void onLoadMoreRequested() {
AppUtils.runOnUIDelayed(() -> {
//加载更多
if (mAdapter.getItemCount() >= mTotal) {
mAdapter.loadMoreEnd();//结束加载
} else {
if (!mIsError) {
mPage++;
loadData();
if (NetworkUtils.isConnected(this)){
AppUtils.runOnUIDelayed(() -> {
//加载更多
if (mAdapter.getItemCount() >= mTotal) {
mAdapter.loadMoreEnd();//结束加载
} else {
mIsError = true;
mAdapter.loadMoreFail();//加载失败
}
}
}, 650);
if (!mIsError) {
mPage++;
loadData();
} else {
mIsError = true;
mAdapter.loadMoreFail();//加载失败
}

}
}, 650);
}else {
mIsError = true;
mAdapter.loadMoreFail();//加载失败
}
}


/**
* 设置Adapter
*
Expand Down Expand Up @@ -77,6 +83,7 @@ public void showError(String msg) {
@Override
public void complete() {
super.complete();
//需要重新开启监听
mAdapter.setEnableLoadMore(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.yoyiyi.soleil.utils.ToastUtils;
import com.yoyiyi.soleil.widget.statusbar.StatusBarUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand All @@ -36,15 +37,13 @@
*/

public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {

long exitTime = 0L;

@BindView(R.id.nav_view)
NavigationView mNavView;
@BindView(R.id.drawer_layout)
DrawerLayout mDrawerLayout;
private int mCurrentPos = -1;
private List<Fragment> mFragments;
private List<Fragment> mFragments = new ArrayList<>();

@Override
protected int getLayoutId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public Cache getCache(Context context) {
return cache;
}



// 云端响应头拦截器,用来配置缓存策略
private Interceptor mRewriteCacheControlInterceptor = chain -> {
Request request = chain.request();
Expand All @@ -140,6 +142,7 @@ public Cache getCache(Context context) {
* 添加UA拦截器,B站请求API需要加上UA才能正常使用
*/
private static class UserAgentInterceptor implements Interceptor {

@Override
public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yoyiyi.soleil.network.helper;
package com.yoyiyi.soleil.network.support;

import android.util.Log;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

package com.yoyiyi.soleil.network.support.cookie;


import com.yoyiyi.soleil.network.support.cookie.store.CookieStore;

import java.util.List;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;

/**
* @author zzq 作者 E-mail: [email protected]
* @date 创建时间:2017/7/12 17:04
* 描述:管理cookie
*/
public class CookieJarImpl implements CookieJar {

private CookieStore cookieStore;

public CookieJarImpl(CookieStore cookieStore) {
if (cookieStore == null) {
throw new IllegalArgumentException("cookieStore can not be null!");
}
this.cookieStore = cookieStore;
}

@Override
public synchronized void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
cookieStore.saveCookie(url, cookies);
}

@Override
public synchronized List<Cookie> loadForRequest(HttpUrl url) {
return cookieStore.loadCookie(url);
}

public CookieStore getCookieStore() {
return cookieStore;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package com.yoyiyi.soleil.network.support.cookie;

import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Locale;

import okhttp3.Cookie;

/**
* @author zzq 作者 E-mail: [email protected]
* @date 创建时间:2017/7/12 17:04
* 描述:cookie序列化
*/

public class SerializableCookie implements Serializable{
private static final String TAG = "SerializableCookie";

private static final long serialVersionUID = 6374381323722046732L;

public static final String HOST = "host";
public static final String NAME = "name";
public static final String DOMAIN = "domain";
public static final String COOKIE = "cookie";

public String host;
public String name;
public String domain;
private transient Cookie cookie;
private transient Cookie clientCookie;

public SerializableCookie(String host, Cookie cookie) {
this.cookie = cookie;
this.host = host;
this.name = cookie.name();
this.domain = cookie.domain();
}

public Cookie getCookie() {
Cookie bestCookie = cookie;
if (clientCookie != null) {
bestCookie = clientCookie;
}
return bestCookie;
}

private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeObject(cookie.name());
out.writeObject(cookie.value());
out.writeLong(cookie.expiresAt());
out.writeObject(cookie.domain());
out.writeObject(cookie.path());
out.writeBoolean(cookie.secure());
out.writeBoolean(cookie.httpOnly());
out.writeBoolean(cookie.hostOnly());
out.writeBoolean(cookie.persistent());
}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
String name = (String) in.readObject();
String value = (String) in.readObject();
long expiresAt = in.readLong();
String domain = (String) in.readObject();
String path = (String) in.readObject();
boolean secure = in.readBoolean();
boolean httpOnly = in.readBoolean();
boolean hostOnly = in.readBoolean();
boolean persistent = in.readBoolean();
Cookie.Builder builder = new Cookie.Builder();
builder = builder.name(name);
builder = builder.value(value);
builder = builder.expiresAt(expiresAt);
builder = hostOnly ? builder.hostOnlyDomain(domain) : builder.domain(domain);
builder = builder.path(path);
builder = secure ? builder.secure() : builder;
builder = httpOnly ? builder.httpOnly() : builder;
clientCookie = builder.build();
}

public static SerializableCookie parseCursorToBean(Cursor cursor) {
String host = cursor.getString(cursor.getColumnIndex(HOST));
byte[] cookieBytes = cursor.getBlob(cursor.getColumnIndex(COOKIE));
Cookie cookie = bytesToCookie(cookieBytes);
return new SerializableCookie(host, cookie);
}

public static ContentValues getContentValues(SerializableCookie serializableCookie) {
ContentValues values = new ContentValues();
values.put(SerializableCookie.HOST, serializableCookie.host);
values.put(SerializableCookie.NAME, serializableCookie.name);
values.put(SerializableCookie.DOMAIN, serializableCookie.domain);
values.put(SerializableCookie.COOKIE, cookieToBytes(serializableCookie.host, serializableCookie.getCookie()));
return values;
}

/**
* cookies 序列化成 string
*
* @param cookie 要序列化
* @return 序列化之后的string
*/
public static String encodeCookie(String host, Cookie cookie) {
if (cookie == null) return null;
byte[] cookieBytes = cookieToBytes(host, cookie);
return byteArrayToHexString(cookieBytes);
}

public static byte[] cookieToBytes(String host, Cookie cookie) {
SerializableCookie serializableCookie = new SerializableCookie(host, cookie);
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ObjectOutputStream outputStream = new ObjectOutputStream(os);
outputStream.writeObject(serializableCookie);
} catch (IOException e) {
Log.d(TAG, e.toString());
return null;
}
return os.toByteArray();
}

/**
* 将字符串反序列化成cookies
*
* @param cookieString cookies string
* @return cookie object
*/
public static Cookie decodeCookie(String cookieString) {
byte[] bytes = hexStringToByteArray(cookieString);
return bytesToCookie(bytes);
}

public static Cookie bytesToCookie(byte[] bytes) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
Cookie cookie = null;
try {
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
cookie = ((SerializableCookie) objectInputStream.readObject()).getCookie();
} catch (Exception e) {
Log.d(TAG, e.toString());
}
return cookie;
}

/**
* 二进制数组转十六进制字符串
*
* @param bytes byte array to be converted
* @return string containing hex values
*/
private static String byteArrayToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 2);
for (byte element : bytes) {
int v = element & 0xff;
if (v < 16) {
sb.append('0');
}
sb.append(Integer.toHexString(v));
}
return sb.toString().toUpperCase(Locale.US);
}

/**
* 十六进制字符串转二进制数组
*
* @param hexString string of hex-encoded values
* @return decoded byte array
*/
private static byte[] hexStringToByteArray(String hexString) {
int len = hexString.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i + 1), 16));
}
return data;
}

/**
* host, name, domain 标识一个cookie是否唯一
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

SerializableCookie that = (SerializableCookie) o;

if (host != null ? !host.equals(that.host) : that.host != null) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
return domain != null ? domain.equals(that.domain) : that.domain == null;
}

@Override
public int hashCode() {
int result = host != null ? host.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (domain != null ? domain.hashCode() : 0);
return result;
}
}
Loading

0 comments on commit 47b76a4

Please sign in to comment.