|
1 | 1 | package com.smartdevicelink.util; |
2 | 2 |
|
| 3 | +import android.os.AsyncTask; |
| 4 | +import android.util.Log; |
| 5 | + |
| 6 | +import java.io.BufferedReader; |
| 7 | +import java.io.BufferedWriter; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.io.InputStreamReader; |
| 11 | +import java.io.OutputStreamWriter; |
| 12 | +import java.io.Writer; |
| 13 | +import java.net.HttpURLConnection; |
| 14 | +import java.net.URL; |
| 15 | + |
| 16 | + |
3 | 17 | @Deprecated |
4 | | -public class HttpRequestTask { |
| 18 | +public class HttpRequestTask extends AsyncTask<String, String, String> { |
| 19 | + private static final String TAG = "Http Request Task"; |
| 20 | + |
5 | 21 | public static final String REQUEST_TYPE_POST = "POST"; |
6 | 22 | public static final String REQUEST_TYPE_GET = "GET"; |
7 | 23 | public static final String REQUEST_TYPE_DELETE = "DELETE"; |
8 | 24 |
|
| 25 | + HttpRequestTaskCallback cb; |
| 26 | + |
| 27 | + /** |
| 28 | + * @param hcb callback for when this task finishes |
| 29 | + * <br><br><b> - When calling execute, params as followed: </b><br> |
| 30 | + * 1. Url String<br> |
| 31 | + * 2. Request type (Defined in this class) REQUEST_TYPE_POST, REQUEST_TYPE_GET, REQUEST_TYPE_DELETE<br> |
| 32 | + * 3. (Optional) Data to be sent. <br> |
| 33 | + * 4. (Optional) Content Type Default will be application/json<br> |
| 34 | + * 5. (Optional) Accept Type default will be application/json |
| 35 | + * |
| 36 | + */ |
9 | 37 | @Deprecated |
10 | | - public HttpRequestTask( HttpRequestTaskCallback hcb){ } |
| 38 | + public HttpRequestTask( HttpRequestTaskCallback hcb){ |
| 39 | + this.cb = hcb; |
| 40 | + } |
11 | 41 |
|
12 | 42 | @Deprecated |
13 | 43 | protected String doInBackground(String... params) { |
| 44 | + int length = params.length; |
| 45 | + String urlString = params[0]; |
| 46 | + String request_type = params[1]; |
| 47 | + |
| 48 | + //Grab and set data to be written if included |
| 49 | + String data; |
| 50 | + if(length>2){ |
| 51 | + data = params[2]; |
| 52 | + }else{ |
| 53 | + data = null; |
| 54 | + } |
| 55 | + |
| 56 | + //Grab and set content type for the header if included |
| 57 | + String contentType; |
| 58 | + if(length>3){ |
| 59 | + contentType = params[3]; |
| 60 | + }else{ |
| 61 | + contentType = "application/json"; |
| 62 | + } |
| 63 | + //Grab and set accept type for the header if included |
| 64 | + String acceptType; |
| 65 | + if(length>4){ |
| 66 | + acceptType = params[4]; |
| 67 | + }else{ |
| 68 | + acceptType = "application/json"; |
| 69 | + } |
| 70 | + |
| 71 | + if(urlString == null || request_type == null){ |
| 72 | + Log.e(TAG, "Can't process request, param error"); |
| 73 | + if(cb!=null){ |
| 74 | + cb.httpFailure(-1); |
| 75 | + cb = null; |
| 76 | + } |
| 77 | + return "Error"; |
| 78 | + } |
| 79 | + |
| 80 | + HttpURLConnection urlConnection = null; |
| 81 | + BufferedReader reader = null; |
| 82 | + try { |
| 83 | + URL url = new URL(urlString); |
| 84 | + urlConnection = (HttpURLConnection) url.openConnection(); |
| 85 | + urlConnection.setDoOutput(true); |
| 86 | + urlConnection.setRequestMethod(request_type); |
| 87 | + urlConnection.setRequestProperty("Content-Type", contentType); |
| 88 | + urlConnection.setRequestProperty("Accept", acceptType); |
| 89 | + //If we have data, we should write it out |
| 90 | + if(data !=null){ |
| 91 | + Writer writer = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8")); |
| 92 | + writer.write(data); |
| 93 | + writer.close(); |
| 94 | + } |
| 95 | + InputStream inputStream = urlConnection.getInputStream(); |
| 96 | + |
| 97 | + int responseCode = urlConnection.getResponseCode(); |
| 98 | + if (responseCode == 200) { //Success |
| 99 | + //input stream |
| 100 | + StringBuffer buffer = new StringBuffer(); |
| 101 | + if (inputStream == null) { |
| 102 | + // Nothing to do. |
| 103 | + if(cb!=null){ |
| 104 | + cb.httpCallComplete(null); |
| 105 | + cb = null; |
| 106 | + } |
| 107 | + return null; |
| 108 | + } |
| 109 | + reader = new BufferedReader(new InputStreamReader(inputStream)); |
| 110 | + |
| 111 | + String inputLine; |
| 112 | + while ((inputLine = reader.readLine()) != null) |
| 113 | + buffer.append(inputLine).append("\n"); |
| 114 | + if (buffer.length() == 0) { |
| 115 | + // Stream was empty. No point in parsing. |
| 116 | + if(cb!=null){ |
| 117 | + cb.httpCallComplete(null); |
| 118 | + cb = null; |
| 119 | + } |
| 120 | + return null; |
| 121 | + } |
| 122 | + String response = null; |
| 123 | + |
| 124 | + response = buffer.toString(); |
| 125 | + //send to post execute |
| 126 | + if(cb!=null){ |
| 127 | + cb.httpCallComplete(response); |
| 128 | + cb = null; |
| 129 | + } |
| 130 | + return response; |
| 131 | + }else{ |
| 132 | + if(cb!=null){ |
| 133 | + cb.httpFailure(responseCode); |
| 134 | + cb = null; |
| 135 | + } |
| 136 | + Log.e(TAG, "Failed to download file - " + responseCode); |
| 137 | + return null; |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + } catch (IOException e) { |
| 142 | + e.printStackTrace(); |
| 143 | + } catch (NullPointerException e){ // Only to catch error in urlConnection.getOutputStream() - when servers are down |
| 144 | + e.printStackTrace(); |
| 145 | + urlConnection = null; |
| 146 | + } |
| 147 | + finally { |
| 148 | + if (urlConnection != null) { |
| 149 | + urlConnection.disconnect(); |
| 150 | + } |
| 151 | + if (reader != null) { |
| 152 | + try { |
| 153 | + reader.close(); |
| 154 | + } catch (final IOException e) { |
| 155 | + Log.e(TAG, "Error closing stream", e); |
| 156 | + } |
| 157 | + } |
| 158 | + if(cb!=null){ |
| 159 | + cb.httpFailure(-1); |
| 160 | + } |
| 161 | + } |
14 | 162 | return null; |
15 | 163 | } |
16 | 164 |
|
| 165 | + /** |
| 166 | + * Callback interface for HTTP requests. |
| 167 | + * @author Joey Grover |
| 168 | + * |
| 169 | + */ |
17 | 170 | @Deprecated |
18 | 171 | public interface HttpRequestTaskCallback{ |
| 172 | + /** |
| 173 | + * Called when HTTP request is successfully completed. |
| 174 | + * @param response The response to the HTTP request. |
| 175 | + */ |
19 | 176 | public abstract void httpCallComplete(String response); |
| 177 | + /** |
| 178 | + * Called when HTTP request failed. |
| 179 | + * @param statusCode The HTTP failure code. |
| 180 | + */ |
20 | 181 | public abstract void httpFailure(int statusCode); |
21 | 182 | } |
22 | 183 |
|
|
0 commit comments