2024年1月1日发(作者:)

public class ProgressRequestBody extends RequestBody { private File mFile; private String mPath; private String mMediaType; private BaseView mListener; private int mEachBufferSize = 1024; public ProgressRequestBody(final File file, String mediaType, BaseView baseView) { mFile = file; mMediaType = mediaType; mListener = baseView; } public ProgressRequestBody(final File file, String mediaType, int eachBufferSize, BaseView baseView) { mFile = file; mMediaType = mediaType; mEachBufferSize = eachBufferSize; mListener = baseView; } @Override public MediaType contentType() { // i want to upload only images return (mMediaType); } @Override public void writeTo(BufferedSink sink) throws IOException { long fileLength = (); byte[] buffer = new byte[mEachBufferSize]; FileInputStream in = new FileInputStream(mFile); long uploaded = 0; try { int read; Handler handler = new Handler(nLooper()); while ((read = (buffer)) != -1) { // update progress on UI thread (new ProgressUpdater(uploaded, fileLength)); uploaded += read; (buffer, 0, read); } } finally { (); } } private class ProgressUpdater implements Runnable { private long mUploaded; private long mTotal; public ProgressUpdater(long uploaded, long total) { mUploaded = uploaded; mTotal = total; } @Override public void run() { ress((int) (100 * mUploaded / mTotal)); } }}5、 定义MainPresenter,并继承BasePresenter

public class DoubleDefault0Adapter implements JsonSerializer, JsonDeserializer { @Override public Double deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { if (tring().equals("") || tring().equals("null")) {//定义为double类型,如果后台返回""或者null,则返回0.00 return 0.00; } } catch (Exception ignore) { } try { return ouble(); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } } @Override public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src); }}4、 对int类型处理,返回“”,或“null”,动态更改为默认值0,新建DoubleDefaultAdapter类public class IntegerDefaultAdapter implements JsonSerializer, JsonDeserializer { @Override public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { if (tring().equals("") || tring().equals("null")) {//定义为int类型,如果后台返回""或者null,则返回0 return 0; } } catch (Exception ignore) { } try { return nt(); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } } @Override public JsonElement serialize(Integer src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src); }}5、 对Long类型处理,返回“”,或“null”,动态更改为默认值0,新建DoubleDefaultAdapter类

public class OkHttpCookies implements Serializable { private transient final Cookie cookies; private transient Cookie clientCookies; public OkHttpCookies(Cookie cookies) { s = cookies; } public Cookie getCookies() { Cookie bestCookies = cookies; if (clientCookies != null) { bestCookies = clientCookies; } return bestCookies; } private void writeObject(ObjectOutputStream out) throws IOException { bject(()); bject(()); ong(sAt()); bject(()); bject(()); oolean(()); oolean(ly()); oolean(ly()); oolean(tent()); } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { String name = (String) ject(); String value = (String) ject(); long expiresAt = ng(); String domain = (String) ject(); String path = (String) ject(); boolean secure = olean(); boolean httpOnly = olean(); boolean hostOnly = olean(); boolean persistent = olean(); r builder = new r(); builder = (name); builder = (value); builder = sAt(expiresAt); builder = hostOnly ? lyDomain(domain) : (domain); builder = (path); builder = secure ? () : builder; builder = httpOnly ? ly() : builder; clientCookies =(); }}(3)创建PersistentCookieStore类public class PersistentCookieStore { private static final String LOG_TAG = "PersistentCookieStore"; private static final String COOKIE_PREFS = "Cookies_Prefs"; private final Map> cookies; private final SharedPreferences cookiePrefs; public PersistentCookieStore(Context context) { cookiePrefs = redPreferences(COOKIE_PREFS, 0); cookies = new HashMap<>(); //将持久化的cookies缓存到内存中 即map cookies Map prefsMap = (); for ( entry : et()) { String[] cookieNames = ((String) ue(), ","); for (String name : cookieNames) { String encodedCookie = ing(name, null);

String encodedCookie = ing(name, null); if (encodedCookie != null) { Cookie decodedCookie = decodeCookie(encodedCookie); if (decodedCookie != null) { if (!nsKey(())) { ((), new ConcurrentHashMap()); } (()).put(name, decodedCookie); } } } } } protected String getCookieToken(Cookie cookie) { return () + "@" + (); } public void add(HttpUrl url, Cookie cookie) { String name = getCookieToken(cookie); //将cookies缓存到内存中 如果缓存过期 就重置此cookie if (!tent()) { if (!nsKey(())) { ((), new ConcurrentHashMap()); } (()).put(name, cookie); } else { if (nsKey(())) { (()).remove(name); } } //讲cookies持久化到本地 prefsWriter = (); ing((), (",", (()).keySet())); ing(name, encodeCookie(new OkHttpCookies(cookie))); (); } public List get(HttpUrl url) { ArrayList ret = new ArrayList<>(); if (nsKey(())) { ((()).values()); } return ret; } public boolean removeAll() { prefsWriter = (); (); (); (); return true; } public boolean remove(HttpUrl url, Cookie cookie) { String name = getCookieToken(cookie); if (nsKey(()) && (()).containsKey(name)) { (()).remove(name); prefsWriter = (); if (ns(name)) { (name); } ing((), (",", (()).keySet())); (); return true; } else { return false; }