Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit 52e01a8

Browse files
committed
adding back custom http code instead of android-async-http for easier distrobution
1 parent ee43cb0 commit 52e01a8

File tree

6 files changed

+208
-29
lines changed

6 files changed

+208
-29
lines changed

libs/android-async-http-1.4.3.jar

-28.5 KB
Binary file not shown.

src/main/java/com/rollbar/android/Notifier.java

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
import android.content.Context;
1414
import android.content.pm.PackageInfo;
1515
import android.content.pm.PackageManager.NameNotFoundException;
16+
import android.content.res.Configuration;
1617
import android.util.Log;
1718

18-
import com.loopj.android.http.AsyncHttpClient;
19-
import com.loopj.android.http.AsyncHttpResponseHandler;
19+
import com.rollbar.android.http.HttpRequestManager;
20+
import com.rollbar.android.http.HttpResponse;
21+
import com.rollbar.android.http.HttpResponseHandler;
2022

2123
public class Notifier {
2224
private static final String NOTIFIER_VERSION = "0.0.2";
@@ -31,8 +33,6 @@ public class Notifier {
3133

3234
private int versionCode;
3335
private String versionName;
34-
35-
private AsyncHttpClient httpClient;
3636

3737
public Notifier(Context context, String accessToken, String environment) {
3838
this.context = context;
@@ -48,8 +48,6 @@ public Notifier(Context context, String accessToken, String environment) {
4848
} catch (NameNotFoundException e) {
4949
Log.e(Rollbar.TAG, "Error getting package info!");
5050
}
51-
52-
httpClient = new AsyncHttpClient();
5351

5452
endpoint = DEFAULT_ENDPOINT;
5553
reportUncaughtExceptions = true;
@@ -107,37 +105,21 @@ private JSONObject buildPayload(String level, JSONObject body) throws JSONExcept
107105
}
108106

109107
private void postItem(JSONObject payload) {
110-
HttpEntity entity;
111-
112-
try {
113-
entity = new StringEntity(payload.toString());
114-
} catch (UnsupportedEncodingException e) {
115-
Log.e(Rollbar.TAG, e.toString());
116-
return;
117-
}
118-
119108
Log.i(Rollbar.TAG, "Sending item payload...");
120109

121-
httpClient.post(null, this.endpoint + "item/", entity,
122-
"application/json", new AsyncHttpResponseHandler() {
123-
@Override
124-
public void onStart() {
125-
}
126-
110+
HttpRequestManager.getInstance().postJson(this.endpoint + "item/", payload,
111+
new HttpResponseHandler() {
112+
127113
@Override
128-
public void onSuccess(String response) {
129-
Log.i(Rollbar.TAG, "Success.");
114+
public void onSuccess(HttpResponse response) {
115+
Log.i(Rollbar.TAG, "Success");
130116
}
131-
117+
132118
@Override
133-
public void onFailure(Throwable e, String response) {
119+
public void onFailure(HttpResponse response) {
134120
Log.e(Rollbar.TAG, "There was a problem reporting to Rollbar!");
135121
Log.e(Rollbar.TAG, "Response: " + response);
136122
}
137-
138-
@Override
139-
public void onFinish() {
140-
}
141123
});
142124
}
143125

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.rollbar.android.http;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.BufferedOutputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.OutputStream;
8+
import java.io.UnsupportedEncodingException;
9+
import java.net.HttpURLConnection;
10+
import java.net.URL;
11+
import java.util.HashMap;
12+
import java.util.Map.Entry;
13+
14+
import android.util.Log;
15+
16+
import com.rollbar.android.Rollbar;
17+
18+
public class HttpRequest implements Runnable {
19+
private URL url;
20+
private HttpResponseHandler handler;
21+
22+
private HttpURLConnection connection;
23+
private HashMap<String, String> requestProperties;
24+
25+
private String method;
26+
private byte[] body;
27+
28+
public HttpRequest(URL url, String method, HttpResponseHandler handler) {
29+
this.url = url;
30+
this.method = method;
31+
this.handler = handler;
32+
33+
this.requestProperties = new HashMap<String, String>();
34+
}
35+
36+
public void setMethod(String method) {
37+
this.method = method;
38+
}
39+
40+
public void setRequestProperty(String key, String value) {
41+
requestProperties.put(key, value);
42+
}
43+
44+
public void setBody(String body) {
45+
try {
46+
this.body = body.getBytes("UTF-8");
47+
} catch (UnsupportedEncodingException e) {
48+
Log.e(Rollbar.TAG, "Cannot encode body: " + e.toString());
49+
}
50+
}
51+
52+
@Override
53+
public void run() {
54+
try {
55+
connection = (HttpURLConnection) url.openConnection();
56+
} catch (IOException e) {
57+
handler.onFailure(new HttpResponse(e.toString()));
58+
return;
59+
}
60+
61+
try {
62+
connection.setRequestMethod(this.method);
63+
64+
for (Entry<String, String> pair : requestProperties.entrySet()) {
65+
connection.setRequestProperty(pair.getKey(), pair.getValue());
66+
}
67+
68+
if (body != null) {
69+
connection.setDoOutput(true);
70+
71+
OutputStream out = new BufferedOutputStream(connection.getOutputStream());
72+
out.write(this.body);
73+
out.close();
74+
}
75+
76+
int responseCode = connection.getResponseCode();
77+
78+
InputStream in;
79+
if (responseCode == 200) {
80+
in = new BufferedInputStream(connection.getInputStream());
81+
} else {
82+
in = new BufferedInputStream(connection.getErrorStream());
83+
}
84+
85+
String responseText = getResponseText(in);
86+
HttpResponse response = new HttpResponse(responseCode, responseText);
87+
88+
if (responseCode == 200) {
89+
handler.onSuccess(response);
90+
} else {
91+
handler.onFailure(response);
92+
}
93+
} catch (IOException e) {
94+
handler.onFailure(new HttpResponse(e.toString()));
95+
} finally {
96+
connection.disconnect();
97+
}
98+
}
99+
100+
private String getResponseText(InputStream in) throws IOException {
101+
byte[] contents = new byte[1024];
102+
103+
int bytesRead = 0;
104+
String response = "";
105+
106+
while ((bytesRead = in.read(contents)) != -1) {
107+
response += new String(contents, 0, bytesRead);
108+
}
109+
110+
return response;
111+
}
112+
113+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.rollbar.android.http;
2+
3+
import java.net.MalformedURLException;
4+
import java.net.URL;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.ThreadPoolExecutor;
7+
8+
import org.json.JSONObject;
9+
10+
public class HttpRequestManager {
11+
private static HttpRequestManager instance = null;
12+
private ThreadPoolExecutor executor;
13+
14+
private HttpRequestManager() {
15+
executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
16+
}
17+
18+
public static HttpRequestManager getInstance() {
19+
if (instance == null) {
20+
instance = new HttpRequestManager();
21+
}
22+
return instance;
23+
}
24+
25+
public void postJson(String urlString, JSONObject json, HttpResponseHandler handler) {
26+
URL url;
27+
28+
try {
29+
url = new URL(urlString);
30+
} catch (MalformedURLException e) {
31+
handler.onFailure(new HttpResponse(e.toString()));
32+
return;
33+
}
34+
35+
HttpRequest request = new HttpRequest(url, "POST", handler);
36+
37+
request.setRequestProperty("Content-Type", "application/json");
38+
request.setRequestProperty("Accept", "application/json");
39+
40+
request.setBody(json.toString());
41+
42+
executor.execute(request);
43+
}
44+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.rollbar.android.http;
2+
3+
public class HttpResponse {
4+
private String result;
5+
6+
private int statusCode;
7+
private String responseText;
8+
9+
public HttpResponse(String result) {
10+
this.result = result;
11+
}
12+
13+
public HttpResponse(int statusCode, String responseText) {
14+
this.statusCode = statusCode;
15+
this.responseText = responseText;
16+
}
17+
18+
public int getStatusCode() {
19+
return statusCode;
20+
}
21+
22+
public String getResponseText() {
23+
return responseText;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
if (responseText != null) {
29+
return responseText;
30+
}
31+
32+
return result;
33+
}
34+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.rollbar.android.http;
2+
3+
public interface HttpResponseHandler {
4+
public void onSuccess(HttpResponse response);
5+
public void onFailure(HttpResponse response);
6+
}

0 commit comments

Comments
 (0)