Skip to content

Commit 144c597

Browse files
committed
Provided JSON generic superclass, Closes android-async-http#346
1 parent dfc8d55 commit 144c597

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
Android Asynchronous Http Client
3+
Copyright (c) 2011 James Smith <[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;
20+
21+
import android.util.Log;
22+
23+
import org.apache.http.Header;
24+
import org.apache.http.HttpStatus;
25+
26+
public abstract class BaseJsonHttpResponseHandler<JSON_TYPE> extends TextHttpResponseHandler {
27+
private static final String LOG_TAG = "BaseJsonHttpResponseHandler";
28+
29+
/**
30+
* Creates a new JsonHttpResponseHandler
31+
*/
32+
33+
public BaseJsonHttpResponseHandler() {
34+
super(DEFAULT_CHARSET);
35+
}
36+
37+
public BaseJsonHttpResponseHandler(String encoding) {
38+
super(encoding);
39+
}
40+
41+
public void onSuccess(int statusCode, Header[] headers, JSON_TYPE response) {
42+
}
43+
44+
public void onFailure(int statusCode, Header[] headers, Throwable e, JSON_TYPE errorResponse) {
45+
}
46+
47+
@Override
48+
public void onSuccess(final int statusCode, final Header[] headers, final String responseBody) {
49+
if (statusCode != HttpStatus.SC_NO_CONTENT) {
50+
new Thread(new Runnable() {
51+
@Override
52+
public void run() {
53+
try {
54+
final JSON_TYPE jsonResponse = parseResponse(responseBody);
55+
postRunnable(new Runnable() {
56+
@Override
57+
public void run() {
58+
onSuccess(statusCode, headers, jsonResponse);
59+
}
60+
});
61+
} catch (final Throwable t) {
62+
Log.d(LOG_TAG, "parseResponse thrown an problem", t);
63+
postRunnable(new Runnable() {
64+
@Override
65+
public void run() {
66+
onFailure(statusCode, headers, t, (JSON_TYPE) null);
67+
}
68+
});
69+
}
70+
}
71+
}).start();
72+
} else {
73+
onSuccess(statusCode, headers, (JSON_TYPE) null);
74+
}
75+
}
76+
77+
@Override
78+
public void onFailure(final int statusCode, final Header[] headers, final String responseBody, final Throwable e) {
79+
if (responseBody != null) {
80+
new Thread(new Runnable() {
81+
@Override
82+
public void run() {
83+
try {
84+
final JSON_TYPE jsonResponse = parseResponse(responseBody);
85+
postRunnable(new Runnable() {
86+
@Override
87+
public void run() {
88+
onFailure(statusCode, headers, e, jsonResponse);
89+
}
90+
});
91+
} catch (Throwable t) {
92+
Log.d(LOG_TAG, "parseResponse thrown an problem", t);
93+
postRunnable(new Runnable() {
94+
@Override
95+
public void run() {
96+
onFailure(statusCode, headers, e, (JSON_TYPE) null);
97+
}
98+
});
99+
}
100+
}
101+
}).start();
102+
} else {
103+
onFailure(statusCode, headers, e, (JSON_TYPE) null);
104+
}
105+
}
106+
107+
protected abstract JSON_TYPE parseResponse(String responseBody) throws Throwable;
108+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.loopj.android.http.sample.util;
2+
3+
/**
4+
* Created by msebera on 10/20/13.
5+
*/
6+
public class SampleJSON {
7+
}

0 commit comments

Comments
 (0)