Skip to content

Commit d013f81

Browse files
committed
JsonSample using JacksonJson library and BaseJsonHttpResponseHandler
1 parent 656f699 commit d013f81

File tree

4 files changed

+180
-1
lines changed

4 files changed

+180
-1
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.loopj.android.http.sample;
2+
3+
import com.fasterxml.jackson.core.JsonFactory;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.loopj.android.http.AsyncHttpClient;
6+
import com.loopj.android.http.RequestFactory;
7+
import com.loopj.android.http.handlers.BaseJsonHttpResponseHandler;
8+
import com.loopj.android.http.interfaces.ResponseHandlerInterface;
9+
import com.loopj.android.http.sample.util.SampleJSON;
10+
import com.loopj.android.http.sample.util.SampleJSONWrapper;
11+
import com.loopj.android.http.utils.RequestHandle;
12+
13+
import cz.msebera.android.httpclient.Header;
14+
import cz.msebera.android.httpclient.HttpEntity;
15+
16+
public class JsonSample extends SampleParentActivity {
17+
@Override
18+
public ResponseHandlerInterface getResponseHandler() {
19+
return new BaseJsonHttpResponseHandler<SampleJSON>() {
20+
@Override
21+
public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, SampleJSON response) {
22+
debugHeaders(getLogTag(), headers);
23+
debugStatusCode(getLogTag(), statusCode);
24+
debugResponse(getLogTag(), rawJsonResponse);
25+
debugResponse(getLogTag(), formatResponse(response));
26+
}
27+
28+
@Override
29+
public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, SampleJSON errorResponse) {
30+
debugHeaders(getLogTag(), headers);
31+
debugStatusCode(getLogTag(), statusCode);
32+
debugThrowable(getLogTag(), throwable);
33+
debugResponse(getLogTag(), rawJsonData);
34+
debugResponse(getLogTag(), formatResponse(errorResponse));
35+
}
36+
37+
@Override
38+
protected SampleJSON parseResponse(String rawJsonData, boolean isFailure) throws Throwable {
39+
return new ObjectMapper().readValues(new JsonFactory().createParser(rawJsonData), SampleJSONWrapper.class).next().getHeaders();
40+
}
41+
};
42+
}
43+
44+
protected String formatResponse(SampleJSON json) {
45+
StringBuilder buf = new StringBuilder();
46+
47+
buf.append("Parsed SampleJSON object is ").append(json == null ? "null" : "not-null");
48+
if (json != null) {
49+
buf.append("\nAccepts: ").append(json.getAccept());
50+
buf.append("\nAcceptLanguage: ").append(json.getAcceptLanguage());
51+
buf.append("\nReferer: ").append(json.getReferer());
52+
buf.append("\nUserAgent: ").append(json.getUserAgent());
53+
buf.append("\nConnection: ").append(json.getConnection());
54+
}
55+
return buf.toString();
56+
}
57+
58+
@Override
59+
public String getDefaultURL() {
60+
return PROTOCOL + "httpbin.org/headers";
61+
}
62+
63+
@Override
64+
public boolean isRequestHeadersAllowed() {
65+
return true;
66+
}
67+
68+
@Override
69+
public boolean isRequestBodyAllowed() {
70+
return false;
71+
}
72+
73+
@Override
74+
public int getSampleTitle() {
75+
return R.string.title_json_sample;
76+
}
77+
78+
@Override
79+
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
80+
return client.sendRequest(RequestFactory.get(URL, headers), responseHandler);
81+
}
82+
}

sample/src/main/java/com/loopj/android/http/sample/WaypointsActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public class WaypointsActivity extends ListActivity {
3737
new SampleConfig(R.string.title_patch_sample, PatchSample.class),
3838
new SampleConfig(R.string.title_put_sample, PutSample.class),
3939
new SampleConfig(R.string.title_head_sample, HeadSample.class),
40-
new SampleConfig(R.string.title_options_sample, OptionsSample.class)
40+
new SampleConfig(R.string.title_options_sample, OptionsSample.class),
41+
new SampleConfig(R.string.title_json_sample, JsonSample.class)
4142
};
4243

4344
@Override
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Android Asynchronous Http Client Sample
3+
Copyright (c) 2014 Marek Sebera <[email protected]>
4+
https://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+
https://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.util;
20+
21+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
22+
import com.fasterxml.jackson.annotation.JsonProperty;
23+
24+
@JsonIgnoreProperties(ignoreUnknown = true)
25+
public class SampleJSON {
26+
27+
private String Accept;
28+
private String Referer;
29+
private String AcceptLanguage;
30+
private String Connection;
31+
private String UserAgent;
32+
33+
public String getAccept() {
34+
return Accept;
35+
}
36+
37+
@JsonProperty("Accept-Encoding")
38+
public void setAccept(String accept) {
39+
Accept = accept;
40+
}
41+
42+
public String getReferer() {
43+
return Referer;
44+
}
45+
46+
@JsonProperty("Referer")
47+
public void setReferer(String referer) {
48+
Referer = referer;
49+
}
50+
51+
public String getAcceptLanguage() {
52+
return AcceptLanguage;
53+
}
54+
55+
@JsonProperty("Accept-Language")
56+
public void setAcceptLanguage(String acceptLanguage) {
57+
AcceptLanguage = acceptLanguage;
58+
}
59+
60+
public String getConnection() {
61+
return Connection;
62+
}
63+
64+
@JsonProperty("Connection")
65+
public void setConnection(String connection) {
66+
Connection = connection;
67+
}
68+
69+
public String getUserAgent() {
70+
return UserAgent;
71+
}
72+
73+
@JsonProperty("User-Agent")
74+
public void setUserAgent(String userAgent) {
75+
UserAgent = userAgent;
76+
}
77+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.loopj.android.http.sample.util;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
6+
@JsonIgnoreProperties(ignoreUnknown = true)
7+
public class SampleJSONWrapper {
8+
9+
private SampleJSON Headers;
10+
11+
@JsonProperty("headers")
12+
public void setHeaders(SampleJSON headers) {
13+
Headers = headers;
14+
}
15+
16+
public SampleJSON getHeaders() {
17+
return Headers;
18+
}
19+
}

0 commit comments

Comments
 (0)