Skip to content

Commit 1cdef63

Browse files
committed
Sample implemented File
1 parent e8b4797 commit 1cdef63

File tree

4 files changed

+122
-8
lines changed

4 files changed

+122
-8
lines changed
Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,85 @@
11
package com.loopj.android.http.sample;
22

3-
import android.app.Activity;
3+
import android.util.Log;
44

5-
public class FileSample extends Activity {
5+
import com.loopj.android.http.AsyncHttpClient;
6+
import com.loopj.android.http.AsyncHttpResponseHandler;
7+
import com.loopj.android.http.FileAsyncHttpResponseHandler;
8+
import com.loopj.android.http.sample.util.FileUtil;
69

10+
import org.apache.http.Header;
11+
import org.apache.http.HttpEntity;
12+
13+
import java.io.File;
14+
15+
public class FileSample extends SampleParentActivity {
16+
private static final String LOG_TAG = "FileSample";
17+
18+
@Override
19+
protected int getSampleTitle() {
20+
return R.string.title_file_sample;
21+
}
22+
23+
@Override
24+
protected boolean isRequestBodyAllowed() {
25+
return false;
26+
}
27+
28+
@Override
29+
protected boolean isRequestHeadersAllowed() {
30+
return true;
31+
}
32+
33+
@Override
34+
protected String getDefaultURL() {
35+
return "https://httpbin.org/robots.txt";
36+
}
37+
38+
@Override
39+
protected AsyncHttpResponseHandler getResponseHandler() {
40+
try {
41+
File tmpFile = File.createTempFile("temp_", "_response", getCacheDir());
42+
return new FileAsyncHttpResponseHandler(tmpFile) {
43+
@Override
44+
public void onStart() {
45+
clearOutputs();
46+
}
47+
48+
@Override
49+
public void onSuccess(int statusCode, Header[] headers, File file) {
50+
debugHeaders(LOG_TAG, headers);
51+
debugStatusCode(LOG_TAG, statusCode);
52+
debugFile(getTargetFile());
53+
}
54+
55+
@Override
56+
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
57+
debugHeaders(LOG_TAG, headers);
58+
debugStatusCode(LOG_TAG, statusCode);
59+
debugThrowable(LOG_TAG, e);
60+
debugFile(getTargetFile());
61+
}
62+
63+
private void debugFile(File file) {
64+
if (file == null || !file.exists()) {
65+
debugResponse(LOG_TAG, "Response is null");
66+
return;
67+
}
68+
try {
69+
debugResponse(LOG_TAG, FileUtil.getStringFromFile(file));
70+
} catch (Throwable t) {
71+
Log.e(LOG_TAG, "Cannot debug file contents", t);
72+
}
73+
}
74+
};
75+
} catch (Throwable t) {
76+
Log.e("FileSample", "Cannot create temporary file", t);
77+
}
78+
return null;
79+
}
80+
81+
@Override
82+
protected void executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, AsyncHttpResponseHandler responseHandler) {
83+
client.get(this, URL, headers, null, responseHandler);
84+
}
785
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.loopj.android.http.sample.util;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.InputStream;
7+
import java.io.InputStreamReader;
8+
9+
// Source: http://stackoverflow.com/questions/12910503/android-read-file-as-string
10+
public class FileUtil {
11+
12+
public static String convertStreamToString(InputStream is) throws Exception {
13+
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
14+
StringBuilder sb = new StringBuilder();
15+
String line = null;
16+
while ((line = reader.readLine()) != null) {
17+
sb.append(line).append("\n");
18+
}
19+
return sb.toString();
20+
}
21+
22+
public static String getStringFromFile(File file) throws Exception {
23+
FileInputStream fin = new FileInputStream(file);
24+
String ret = convertStreamToString(fin);
25+
//Make sure you close all streams.
26+
fin.close();
27+
return ret;
28+
}
29+
30+
}

sample/src/main/res/layout/parent_layout.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
android:fillViewport="true">
66

77
<LinearLayout
8+
android:focusable="true"
9+
android:focusableInTouchMode="true"
810
android:layout_width="match_parent"
911
android:layout_height="wrap_content"
1012
android:orientation="vertical">
1113

14+
<requestFocus />
15+
1216
<LinearLayout
1317
android:id="@+id/layout_url"
1418
android:layout_width="fill_parent"
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33

4-
<string name="app_name">Android Async Http Sample</string>
5-
<string name="title_get_sample">GET Sample</string>
6-
<string name="title_json_sample">JSON GET Sample</string>
7-
<string name="title_post_sample">POST Sample</string>
8-
<string name="title_put_sample">PUT Sample</string>
9-
<string name="title_delete_sample">DELETE Sample</string>
4+
<string name="app_name">Android Async Http Samples</string>
5+
<string name="title_get_sample">GET</string>
6+
<string name="title_json_sample">GET and JSON parse</string>
7+
<string name="title_post_sample">POST</string>
8+
<string name="title_put_sample">PUT</string>
9+
<string name="title_delete_sample">DELETE</string>
10+
<string name="title_file_sample">GET to File</string>
11+
<string name="title_binary_sample">GET to Binary</string>
1012
</resources>

0 commit comments

Comments
 (0)