forked from aesion/bilisoleil
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
663 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
* | ||
|
@@ -77,6 +83,7 @@ public void showError(String msg) { | |
@Override | ||
public void complete() { | ||
super.complete(); | ||
//需要重新开启监听 | ||
mAdapter.setEnableLoadMore(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...yiyi/soleil/network/helper/HttpUtils.java → ...iyi/soleil/network/support/HttpUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
42 changes: 42 additions & 0 deletions
42
app/src/main/java/com/yoyiyi/soleil/network/support/cookie/CookieJarImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
208 changes: 208 additions & 0 deletions
208
app/src/main/java/com/yoyiyi/soleil/network/support/cookie/SerializableCookie.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.