Skip to content

Commit dcbb008

Browse files
Merge pull request #1381 from smartdevicelink/bugfix/issue_1377_HttpRequestTask
Bugfix/issue 1377 http request task
2 parents 36dd4cb + c2bd565 commit dcbb008

4 files changed

Lines changed: 185 additions & 1 deletion

File tree

base/src/main/java/com/smartdevicelink/util/HttpRequestTask.java renamed to android/sdl_android/src/main/java/com/smartdevicelink/util/HttpRequestTask.java

File renamed without changes.

base/src/main/java/android/os/AsyncTask.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package android.os;
2020

21+
@Deprecated
2122
public abstract class AsyncTask<Params, Progress, Result> {
2223

2324

baseAndroid/src/main/java/com/smartdevicelink/util/HttpRequestTask.java

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
package com.smartdevicelink.util;
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+
17+
@Deprecated
18+
public class HttpRequestTask extends AsyncTask<String, String, String> {
19+
private static final String TAG = "Http Request Task";
20+
21+
public static final String REQUEST_TYPE_POST = "POST";
22+
public static final String REQUEST_TYPE_GET = "GET";
23+
public static final String REQUEST_TYPE_DELETE = "DELETE";
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+
*/
37+
@Deprecated
38+
public HttpRequestTask( HttpRequestTaskCallback hcb){
39+
this.cb = hcb;
40+
}
41+
42+
@Deprecated
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+
}
162+
return null;
163+
}
164+
165+
/**
166+
* Callback interface for HTTP requests.
167+
* @author Joey Grover
168+
*
169+
*/
170+
@Deprecated
171+
public interface HttpRequestTaskCallback{
172+
/**
173+
* Called when HTTP request is successfully completed.
174+
* @param response The response to the HTTP request.
175+
*/
176+
public abstract void httpCallComplete(String response);
177+
/**
178+
* Called when HTTP request failed.
179+
* @param statusCode The HTTP failure code.
180+
*/
181+
public abstract void httpFailure(int statusCode);
182+
}
183+
184+
}

0 commit comments

Comments
 (0)