20
20
21
21
import android .app .AlertDialog ;
22
22
import android .content .DialogInterface ;
23
+ import android .content .res .Resources ;
23
24
import android .os .Bundle ;
24
25
import android .util .Log ;
25
- import android .widget .Toast ;
26
+ import com .fasterxml .jackson .core .JsonFactory ;
27
+ import com .fasterxml .jackson .databind .ObjectMapper ;
26
28
27
29
import com .loopj .android .http .AsyncHttpClient ;
28
- import com .loopj .android .http .BinaryHttpResponseHandler ;
30
+ import com .loopj .android .http .BaseJsonHttpResponseHandler ;
29
31
import com .loopj .android .http .RequestHandle ;
30
32
import com .loopj .android .http .ResponseHandlerInterface ;
33
+ import com .loopj .android .http .sample .util .SampleJSON ;
31
34
import com .loopj .android .http .sample .util .SecureSocketFactory ;
32
35
33
36
import org .apache .http .Header ;
@@ -53,9 +56,16 @@ public class CustomCASample extends SampleParentActivity {
53
56
54
57
private static final String LOG_TAG = "CustomCASample" ;
55
58
56
- private static final String SERVER_TEST_URL = "https://httpbin.org/get" ;
57
- private static final String STORE_ALIAS = "TheAlias" ;
58
- private static final String STORE_PASS = "ThePass" ;
59
+ // This is A TEST URL for use with AsyncHttpClient LIBRARY ONLY!
60
+ // It is provided courtesy of Fineswap (http://fineswap.com) and must never
61
+ // be used in ANY program except when running this sample (CustomCASample).
62
+ private static final String SERVER_TEST_URL = "https://api.fineswap.io/ahc" ;
63
+
64
+ // The certificate's alias.
65
+ private static final String STORE_ALIAS = "rootca" ;
66
+
67
+ // The certificate's password.
68
+ private static final String STORE_PASS = "Fineswap" ;
59
69
60
70
// Instruct the library to retry connection when this exception is raised.
61
71
static {
@@ -89,59 +99,18 @@ protected void onCreate(Bundle savedInstanceState) {
89
99
}
90
100
} catch (KeyStoreException e ) {
91
101
Log .e (LOG_TAG , "Unable to initialize key store" , e );
92
- Toast .makeText (
93
- this ,
94
- "Please read res/raw/custom_ca.txt\n to learn how to create your own\n key store containing a custom CA" ,
95
- Toast .LENGTH_LONG ).show ();
96
102
showCustomCAHelp ();
97
103
}
98
104
}
99
105
100
- /**
101
- * Returns contents of `custom_ca.txt` as CharSequence
102
- *
103
- * @return contents of custom_ca.txt from Assets
104
- */
105
- private CharSequence getReadmeText () {
106
- String rtn = "" ;
107
- try {
108
- InputStream stream = getResources ().openRawResource (R .raw .custom_ca );
109
- java .util .Scanner s = new java .util .Scanner (stream )
110
- .useDelimiter ("\\ A" );
111
- rtn = s .hasNext () ? s .next () : "" ;
112
- } catch (Exception | Error e ) {
113
- Log .e (LOG_TAG , "License couldn't be retrieved" , e );
114
- }
115
- return rtn ;
116
- }
117
-
118
- /**
119
- * Will display AlertDialog reading `custom_ca.txt` from Assets, to avoid strict Lint issue
120
- */
121
- private void showCustomCAHelp () {
122
- AlertDialog .Builder builder = new AlertDialog .Builder (this );
123
- builder .setTitle (R .string .title_custom_ca );
124
- builder .setMessage (getReadmeText ());
125
- builder .setNeutralButton (android .R .string .cancel ,
126
- new DialogInterface .OnClickListener () {
127
-
128
- @ Override
129
- public void onClick (DialogInterface dialog , int which ) {
130
- dialog .dismiss ();
131
- }
132
- }
133
- );
134
- builder .show ();
135
- }
136
-
137
106
@ Override
138
107
public int getSampleTitle () {
139
108
return R .string .title_custom_ca ;
140
109
}
141
110
142
111
@ Override
143
112
public boolean isRequestBodyAllowed () {
144
- return true ;
113
+ return false ;
145
114
}
146
115
147
116
@ Override
@@ -156,38 +125,78 @@ public String getDefaultURL() {
156
125
157
126
@ Override
158
127
public ResponseHandlerInterface getResponseHandler () {
159
- return new BinaryHttpResponseHandler () {
128
+ return new BaseJsonHttpResponseHandler <SampleJSON >() {
129
+
160
130
@ Override
161
131
public void onStart () {
162
132
clearOutputs ();
163
133
}
164
134
165
135
@ Override
166
- public String [] getAllowedContentTypes () {
167
- // Allowing all data for debug purposes
168
- return new String []{".*" };
169
- }
170
-
171
- public void onSuccess (int statusCode , Header [] headers , byte [] binaryData ) {
172
- debugStatusCode (LOG_TAG , statusCode );
136
+ public void onSuccess (int statusCode , Header [] headers , String rawJsonResponse , SampleJSON response ) {
173
137
debugHeaders (LOG_TAG , headers );
174
- debugResponse (LOG_TAG , "Received response is " + binaryData .length + " bytes" );
138
+ debugStatusCode (LOG_TAG , statusCode );
139
+ if (response != null ) {
140
+ debugResponse (LOG_TAG , rawJsonResponse );
141
+ }
175
142
}
176
143
177
144
@ Override
178
- public void onFailure (int statusCode , Header [] headers , byte [] errorResponse , Throwable e ) {
145
+ public void onFailure (int statusCode , Header [] headers , Throwable throwable , String rawJsonData , SampleJSON errorResponse ) {
179
146
debugHeaders (LOG_TAG , headers );
180
147
debugStatusCode (LOG_TAG , statusCode );
181
- debugThrowable (LOG_TAG , e );
148
+ debugThrowable (LOG_TAG , throwable );
182
149
if (errorResponse != null ) {
183
- debugResponse (LOG_TAG , "Received response is " + errorResponse . length + " bytes" );
150
+ debugResponse (LOG_TAG , rawJsonData );
184
151
}
185
152
}
153
+
154
+ @ Override
155
+ protected SampleJSON parseResponse (String rawJsonData , boolean isFailure ) throws Throwable {
156
+ return new ObjectMapper ().readValues (new JsonFactory ().createParser (rawJsonData ), SampleJSON .class ).next ();
157
+ }
186
158
};
187
159
}
188
160
189
161
@ Override
190
162
public RequestHandle executeSample (AsyncHttpClient client , String URL , Header [] headers , HttpEntity entity , ResponseHandlerInterface responseHandler ) {
191
163
return client .get (this , URL , headers , null , responseHandler );
192
164
}
165
+
166
+ /**
167
+ * Returns contents of `custom_ca.txt` file from assets as CharSequence.
168
+ *
169
+ * @return contents of custom_ca.txt file
170
+ */
171
+ private CharSequence getReadmeText () {
172
+ String rtn = "" ;
173
+ try {
174
+ InputStream stream = getResources ().openRawResource (R .raw .custom_ca );
175
+ java .util .Scanner s = new java .util .Scanner (stream )
176
+ .useDelimiter ("\\ A" );
177
+ rtn = s .hasNext () ? s .next () : "" ;
178
+ } catch (Resources .NotFoundException e ) {
179
+ Log .e (LOG_TAG , "License couldn't be retrieved" , e );
180
+ }
181
+ return rtn ;
182
+ }
183
+
184
+ /**
185
+ * Displays a dialog showing contents of `custom_ca.txt` file from assets.
186
+ * This is needed to avoid Lint's strict mode.
187
+ */
188
+ private void showCustomCAHelp () {
189
+ AlertDialog .Builder builder = new AlertDialog .Builder (this );
190
+ builder .setTitle (R .string .title_custom_ca );
191
+ builder .setMessage (getReadmeText ());
192
+ builder .setNeutralButton (android .R .string .cancel ,
193
+ new DialogInterface .OnClickListener () {
194
+ @ Override
195
+ public void onClick (DialogInterface dialog , int which ) {
196
+ dialog .dismiss ();
197
+ }
198
+ }
199
+ );
200
+ builder .show ();
201
+ }
193
202
}
0 commit comments