Skip to content

Commit c871ed7

Browse files
committed
Added sample for resuming downloads using RangeFileAsyncHttpResponseHandler
1 parent dc2aa34 commit c871ed7

File tree

6 files changed

+101
-3
lines changed

6 files changed

+101
-3
lines changed

sample/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<activity android:name=".Http401AuthSample"/>
4242
<activity android:name=".AsyncBackgroundThreadSample"/>
4343
<activity android:name=".ContentTypeForHttpEntitySample"/>
44+
<activity android:name=".ResumeDownloadSample"/>
4445

4546
<service android:name=".services.ExampleIntentService"/>
4647
</application>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.loopj.android.http.sample;
2+
3+
import android.util.Log;
4+
5+
import com.loopj.android.http.AsyncHttpClient;
6+
import com.loopj.android.http.RangeFileAsyncHttpResponseHandler;
7+
import com.loopj.android.http.RequestHandle;
8+
import com.loopj.android.http.ResponseHandlerInterface;
9+
10+
import org.apache.http.Header;
11+
import org.apache.http.HttpEntity;
12+
13+
import java.io.File;
14+
import java.io.IOException;
15+
16+
public class ResumeDownloadSample extends SampleParentActivity {
17+
18+
private File downloadTarget;
19+
private static final String LOG_TAG = "ResumeDownloadSample";
20+
21+
private File getDownloadTarget() {
22+
try {
23+
if (downloadTarget == null) {
24+
downloadTarget = File.createTempFile("download_", "_resume", getCacheDir());
25+
}
26+
} catch (IOException e) {
27+
Log.e(LOG_TAG, "Couldn't create cache file to download to");
28+
}
29+
return downloadTarget;
30+
}
31+
32+
@Override
33+
public ResponseHandlerInterface getResponseHandler() {
34+
return new RangeFileAsyncHttpResponseHandler(getDownloadTarget()) {
35+
@Override
36+
public void onFailure(int statusCode, Header[] headers, Throwable throwable, File file) {
37+
debugStatusCode(LOG_TAG, statusCode);
38+
debugHeaders(LOG_TAG, headers);
39+
debugThrowable(LOG_TAG, throwable);
40+
if (file != null) {
41+
addView(getColoredView(LIGHTGREEN, "Download interrupted (" + statusCode + "): (bytes=" + file.length() + "), path: " + file.getAbsolutePath()));
42+
}
43+
}
44+
45+
@Override
46+
public void onSuccess(int statusCode, Header[] headers, File file) {
47+
debugStatusCode(LOG_TAG, statusCode);
48+
debugHeaders(LOG_TAG, headers);
49+
if (file != null) {
50+
addView(getColoredView(LIGHTGREEN, "Request succeeded (" + statusCode + "): (bytes=" + file.length() + "), path: " + file.getAbsolutePath()));
51+
}
52+
}
53+
};
54+
}
55+
56+
@Override
57+
public String getDefaultHeaders() {
58+
return "Range=bytes=10-20";
59+
}
60+
61+
@Override
62+
public String getDefaultURL() {
63+
return PROTOCOL + "www.google.com/images/srpr/logo11w.png";
64+
}
65+
66+
@Override
67+
public boolean isRequestHeadersAllowed() {
68+
return true;
69+
}
70+
71+
@Override
72+
public boolean isRequestBodyAllowed() {
73+
return false;
74+
}
75+
76+
@Override
77+
public int getSampleTitle() {
78+
return R.string.title_resume_download;
79+
}
80+
81+
@Override
82+
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
83+
return client.get(this, URL, headers, null, responseHandler);
84+
}
85+
}

sample/src/main/java/com/loopj/android/http/sample/SampleInterface.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public interface SampleInterface {
5656

5757
String getDefaultURL();
5858

59+
String getDefaultHeaders();
60+
5961
boolean isRequestHeadersAllowed();
6062

6163
boolean isRequestBodyAllowed();

sample/src/main/java/com/loopj/android/http/sample/SampleParentActivity.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ protected AsyncHttpRequest newAsyncHttpRequest(DefaultHttpClient client, HttpCon
6666
private EditText urlEditText, headersEditText, bodyEditText;
6767
private LinearLayout responseLayout;
6868
private final List<RequestHandle> requestHandles = new LinkedList<RequestHandle>();
69+
private static final String LOG_TAG = "SampleParentActivity";
6970

7071
protected static final String PROTOCOL = "https://";
7172
protected static final int LIGHTGREEN = Color.parseColor("#00FF66");
@@ -89,6 +90,7 @@ protected void onCreate(Bundle savedInstanceState) {
8990
responseLayout = (LinearLayout) findViewById(R.id.layout_response);
9091

9192
urlEditText.setText(getDefaultURL());
93+
headersEditText.setText(getDefaultHeaders());
9294

9395
bodyLayout.setVisibility(isRequestBodyAllowed() ? View.VISIBLE : View.GONE);
9496
headersLayout.setVisibility(isRequestHeadersAllowed() ? View.VISIBLE : View.GONE);
@@ -161,10 +163,11 @@ public List<Header> getRequestHeadersList() {
161163

162164
String headerName = line.substring(0, equalSignPos).trim();
163165
String headerValue = line.substring(1 + equalSignPos).trim();
166+
Log.d(LOG_TAG, String.format("Added header: [%s:%s]", headerName, headerValue));
164167

165168
headers.add(new BasicHeader(headerName, headerValue));
166169
} catch (Throwable t) {
167-
Log.e("SampleParentActivity", "Not a valid header line: " + line, t);
170+
Log.e(LOG_TAG, "Not a valid header line: " + line, t);
168171
}
169172
}
170173
}
@@ -182,7 +185,7 @@ public HttpEntity getRequestEntity() {
182185
try {
183186
return new StringEntity(bodyText);
184187
} catch (UnsupportedEncodingException e) {
185-
Log.e("SampleParentActivity", "cannot create String entity", e);
188+
Log.e(LOG_TAG, "cannot create String entity", e);
186189
}
187190
}
188191
return null;
@@ -277,6 +280,11 @@ protected View getColoredView(int bgColor, String msg) {
277280
return tv;
278281
}
279282

283+
@Override
284+
public String getDefaultHeaders() {
285+
return null;
286+
}
287+
280288
protected final void addView(View v) {
281289
responseLayout.addView(v);
282290
}

sample/src/main/java/com/loopj/android/http/sample/WaypointsActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public class WaypointsActivity extends ListActivity {
5454
new SampleConfig(R.string.title_range_sample, RangeResponseSample.class),
5555
new SampleConfig(R.string.title_401_unauth, Http401AuthSample.class),
5656
new SampleConfig(R.string.title_pre_post_processing, PrePostProcessingSample.class),
57-
new SampleConfig(R.string.title_content_type_http_entity, ContentTypeForHttpEntitySample.class)
57+
new SampleConfig(R.string.title_content_type_http_entity, ContentTypeForHttpEntitySample.class),
58+
new SampleConfig(R.string.title_resume_download, ResumeDownloadSample.class)
5859
};
5960

6061
@Override

sample/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@
3838
<string name="title_pre_post_processing">Pre-/Post-processing</string>
3939
<string name="title_async_background_thread">Async on background thread</string>
4040
<string name="title_content_type_http_entity">Content-Type with HttpEntity</string>
41+
<string name="title_resume_download">Resuming Download</string>
4142
</resources>

0 commit comments

Comments
 (0)