|
| 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 | +} |
0 commit comments