forked from mcxiaoke/android-volley
-
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.
add new default Request , and update progress
- ByteRequest added - FileRequest added - Add Update Progress - Add VolleyBitmapLruCache Helper for Android 3.0 or above
- Loading branch information
Alan
committed
Nov 21, 2013
1 parent
5ae5662
commit 3eac4f1
Showing
11 changed files
with
325 additions
and
25 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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/> | ||
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/> | ||
<classpathentry kind="src" path="src"/> | ||
<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 exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/> | ||
<classpathentry kind="output" path="bin/classes"/> | ||
</classpath> |
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 |
---|---|---|
|
@@ -3,4 +3,4 @@ gen | |
build | ||
target | ||
.gradle | ||
|
||
.DS_Store |
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
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,52 @@ | ||
package com.android.volley.toolbox; | ||
|
||
import com.android.volley.NetworkResponse; | ||
import com.android.volley.Request; | ||
import com.android.volley.Response; | ||
import com.android.volley.Request.Method; | ||
import com.android.volley.Response.ErrorListener; | ||
import com.android.volley.Response.Listener; | ||
|
||
|
||
/** | ||
* A canned request for retrieving the response body at a given URL as a byte[]. | ||
*/ | ||
public class ByteRequest extends Request<byte[]> { | ||
private final Listener<byte[]> mListener; | ||
|
||
|
||
/** | ||
* Creates a new request with the given method. | ||
* | ||
* @param method the request {@link Method} to use | ||
* @param url URL to fetch the string at | ||
* @param listener Listener to receive the String response | ||
* @param errorListener Error listener, or null to ignore errors | ||
*/ | ||
public ByteRequest(int method, String url, Listener<byte[]> listener, | ||
ErrorListener errorListener) { | ||
super(method, url, errorListener); | ||
mListener = listener; | ||
} | ||
|
||
/** | ||
* Creates a new GET request. | ||
* | ||
* @param url URL to fetch the string at | ||
* @param listener Listener to receive the String response | ||
* @param errorListener Error listener, or null to ignore errors | ||
*/ | ||
public ByteRequest(String url, Listener<byte[]> listener, ErrorListener errorListener) { | ||
this(Method.GET, url, listener, errorListener); | ||
} | ||
|
||
@Override | ||
protected void deliverResponse(byte[] response) { | ||
mListener.onResponse(response); | ||
} | ||
|
||
@Override | ||
protected Response<byte[]> parseNetworkResponse(NetworkResponse response) { | ||
return Response.success(response.data, HttpHeaderParser.parseCacheHeaders(response)); | ||
} | ||
} |
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,131 @@ | ||
/* | ||
* Copyright (C) 2011 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.android.volley.toolbox; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
|
||
import com.android.volley.DefaultRetryPolicy; | ||
import com.android.volley.NetworkResponse; | ||
import com.android.volley.ParseError; | ||
import com.android.volley.Request; | ||
import com.android.volley.Response; | ||
import com.android.volley.VolleyLog; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.Bitmap.Config; | ||
import android.graphics.BitmapFactory; | ||
import android.os.Environment; | ||
import android.provider.MediaStore.Files; | ||
|
||
/** | ||
* A canned request for getting an file at a given URL and calling | ||
* back with a File. | ||
* | ||
* like BitmapDowlload one | ||
* | ||
* @author extralam@github | ||
*/ | ||
public class FileRequest extends Request<File> { | ||
|
||
/** Socket timeout in milliseconds for image requests */ | ||
private static final int FILE_TIMEOUT_MS = 6000; | ||
/** Default number of retries for image requests */ | ||
private static final int FILE_MAX_RETRIES = 3; | ||
/** Default backoff multiplier for image requests */ | ||
private static final float FILE_BACKOFF_MULT = 2f; | ||
|
||
private final Response.Listener<File> mListener; | ||
|
||
/** Decoding lock so that we don't decode more than one image at a time (to avoid OOM's) */ | ||
private static final Object sDecodeLock = new Object(); | ||
|
||
private File mFile = null; | ||
|
||
/** | ||
* | ||
* @param url | ||
* @param file provide a File in main Activity | ||
* @param listener | ||
* @param errorListener | ||
*/ | ||
public FileRequest(int method ,String url, File file, Response.Listener<File> listener, Response.ErrorListener errorListener) { | ||
super(method, url, errorListener); | ||
setRetryPolicy( | ||
new DefaultRetryPolicy(FILE_TIMEOUT_MS, FILE_MAX_RETRIES, FILE_BACKOFF_MULT)); | ||
mListener = listener; | ||
mFile = file; | ||
} | ||
|
||
public FileRequest(String url, File file, Response.Listener<File> listener, Response.ErrorListener errorListener) { | ||
super(Method.GET, url, errorListener); | ||
setRetryPolicy( | ||
new DefaultRetryPolicy(FILE_TIMEOUT_MS, FILE_MAX_RETRIES, FILE_BACKOFF_MULT)); | ||
mListener = listener; | ||
mFile = file; | ||
} | ||
|
||
@Override | ||
public Priority getPriority() { | ||
return Priority.LOW; | ||
} | ||
|
||
@Override | ||
protected Response<File> parseNetworkResponse(NetworkResponse response) { | ||
// Serialize all decode on a global lock to reduce concurrent heap usage. | ||
synchronized (sDecodeLock) { | ||
try { | ||
return doParse(response); | ||
} catch (OutOfMemoryError e) { | ||
VolleyLog.e("Caught OOM for %d byte image, url=%s", response.data.length, getUrl()); | ||
return Response.error(new ParseError(e)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* The real guts of parseNetworkResponse. Broken out for readability. | ||
*/ | ||
private Response<File> doParse(NetworkResponse response) { | ||
byte[] bytes = response.data; | ||
//convert array of bytes into file | ||
FileOutputStream stream; | ||
try { | ||
stream = new FileOutputStream(mFile); | ||
stream.write(bytes); | ||
stream.close(); | ||
} catch (FileNotFoundException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
if (mFile == null) { | ||
return Response.error(new ParseError(response)); | ||
} else { | ||
return Response.success(mFile, HttpHeaderParser.parseCacheHeaders(response)); | ||
} | ||
} | ||
|
||
@Override | ||
protected void deliverResponse(File response) { | ||
mListener.onResponse(response); | ||
} | ||
|
||
} |
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
Oops, something went wrong.