Skip to content

Commit 5185f9d

Browse files
committed
Added DirectorySample for new supported behavior of FileAsyncHttpResponseHandler
1 parent f39b23f commit 5185f9d

File tree

4 files changed

+130
-4
lines changed

4 files changed

+130
-4
lines changed

sample/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<activity android:name=".JsonSample" />
3030
<activity android:name=".JsonStreamerSample" />
3131
<activity android:name=".FileSample" />
32+
<activity android:name=".DirectorySample" />
3233
<activity android:name=".BinarySample" />
3334
<activity android:name=".GzipSample" />
3435
<activity android:name=".Redirect302Sample" />
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
Android Asynchronous Http Client Sample
3+
Copyright (c) 2014 Marek Sebera <[email protected]>
4+
http://loopj.com
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package com.loopj.android.http.sample;
20+
21+
import android.os.Bundle;
22+
import android.util.Log;
23+
import android.view.View;
24+
import android.widget.Button;
25+
26+
import com.loopj.android.http.AsyncHttpClient;
27+
import com.loopj.android.http.FileAsyncHttpResponseHandler;
28+
import com.loopj.android.http.RequestHandle;
29+
import com.loopj.android.http.ResponseHandlerInterface;
30+
import com.loopj.android.http.sample.util.FileUtil;
31+
32+
import org.apache.http.Header;
33+
import org.apache.http.HttpEntity;
34+
35+
import java.io.File;
36+
37+
public class DirectorySample extends SampleParentActivity {
38+
private static final String LOG_TAG = "DirectorySample";
39+
private FileAsyncHttpResponseHandler lastResponseHandler = null;
40+
41+
@Override
42+
public int getSampleTitle() {
43+
return R.string.title_directory_sample;
44+
}
45+
46+
@Override
47+
public boolean isRequestBodyAllowed() {
48+
return false;
49+
}
50+
51+
@Override
52+
public boolean isRequestHeadersAllowed() {
53+
return true;
54+
}
55+
56+
@Override
57+
public String getDefaultURL() {
58+
return "https://httpbin.org/robots.txt";
59+
}
60+
61+
@Override
62+
protected void onCreate(Bundle savedInstanceState) {
63+
super.onCreate(savedInstanceState);
64+
Button deleteTargetFile = new Button(this);
65+
deleteTargetFile.setText(R.string.button_delete_target_file);
66+
deleteTargetFile.setOnClickListener(new View.OnClickListener() {
67+
@Override
68+
public void onClick(View v) {
69+
clearOutputs();
70+
if (lastResponseHandler != null) {
71+
File toBeDeleted = lastResponseHandler.getTargetFile();
72+
debugResponse(LOG_TAG, String.format("File was deleted? %b", toBeDeleted.delete()));
73+
debugResponse(LOG_TAG, String.format("Delete file path: %s", toBeDeleted.getAbsolutePath()));
74+
} else {
75+
debugThrowable(LOG_TAG, new Error("You have to Run example first"));
76+
}
77+
}
78+
});
79+
customFieldsLayout.addView(deleteTargetFile);
80+
}
81+
82+
@Override
83+
public ResponseHandlerInterface getResponseHandler() {
84+
lastResponseHandler = new FileAsyncHttpResponseHandler(getCacheDir(), false, true) {
85+
@Override
86+
public void onStart() {
87+
clearOutputs();
88+
}
89+
90+
@Override
91+
public void onSuccess(int statusCode, Header[] headers, File response) {
92+
debugHeaders(LOG_TAG, headers);
93+
debugStatusCode(LOG_TAG, statusCode);
94+
debugFile(response);
95+
}
96+
97+
@Override
98+
public void onFailure(int statusCode, Header[] headers, Throwable throwable, File file) {
99+
debugHeaders(LOG_TAG, headers);
100+
debugStatusCode(LOG_TAG, statusCode);
101+
debugThrowable(LOG_TAG, throwable);
102+
debugFile(file);
103+
}
104+
105+
private void debugFile(File file) {
106+
if (file == null || !file.exists()) {
107+
debugResponse(LOG_TAG, "Response is null");
108+
return;
109+
}
110+
try {
111+
debugResponse(LOG_TAG, file.getAbsolutePath() + "\r\n\r\n" + FileUtil.getStringFromFile(file));
112+
} catch (Throwable t) {
113+
Log.e(LOG_TAG, "Cannot debug file contents", t);
114+
}
115+
}
116+
};
117+
return lastResponseHandler;
118+
}
119+
120+
@Override
121+
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
122+
return client.get(this, URL, headers, null, responseHandler);
123+
}
124+
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
import android.widget.ArrayAdapter;
2626
import android.widget.ListView;
2727

28-
import com.loopj.android.http.RequestParams;
29-
3028
import java.util.ArrayList;
3129
import java.util.List;
3230

@@ -42,6 +40,7 @@ public class WaypointsActivity extends ListActivity {
4240
new SampleConfig(R.string.title_json_streamer_sample, JsonStreamerSample.class),
4341
new SampleConfig(R.string.title_sax_example, SaxSample.class),
4442
new SampleConfig(R.string.title_file_sample, FileSample.class),
43+
new SampleConfig(R.string.title_directory_sample, DirectorySample.class),
4544
new SampleConfig(R.string.title_binary_sample, BinarySample.class),
4645
new SampleConfig(R.string.title_gzip_sample, GzipSample.class),
4746
new SampleConfig(R.string.title_redirect_302, Redirect302Sample.class),
@@ -67,11 +66,11 @@ public class WaypointsActivity extends ListActivity {
6766
@Override
6867
protected void onCreate(Bundle savedInstanceState) {
6968
super.onCreate(savedInstanceState);
70-
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getTitlesList()));
69+
setListAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, getTitlesList()));
7170
}
7271

7372
private List<String> getTitlesList() {
74-
List<String> titles = new ArrayList<String>();
73+
List<String> titles = new ArrayList<>();
7574
for (SampleConfig config : samplesConfig) {
7675
titles.add(getString(config.titleId));
7776
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<string name="button_run">Run</string>
1010
<string name="button_cancel">Cancel</string>
11+
<string name="button_delete_target_file">Delete original target file</string>
1112

1213
<string name="menu_use_https">Use HTTPS</string>
1314
<string name="menu_clear_view">Clear Outputs</string>
@@ -24,6 +25,7 @@
2425
<string name="title_patch_sample">PATCH</string>
2526
<string name="title_delete_sample">DELETE</string>
2627
<string name="title_file_sample">GET to File</string>
28+
<string name="title_directory_sample">GET to Directory</string>
2729
<string name="title_binary_sample">GET binary data</string>
2830
<string name="title_cancel_all">Cancel all requests</string>
2931
<string name="title_sax_example">SAX Example</string>

0 commit comments

Comments
 (0)