24
24
import org .springframework .http .HttpMethod ;
25
25
import org .springframework .http .MediaType ;
26
26
import org .springframework .http .ResponseEntity ;
27
+ import org .springframework .http .converter .json .MappingJacksonHttpMessageConverter ;
27
28
import org .springframework .web .client .HttpClientErrorException ;
28
29
import org .springframework .web .client .RestTemplate ;
29
30
@@ -50,9 +51,9 @@ public void onCreate(Bundle savedInstanceState) {
50
51
super .onCreate (savedInstanceState );
51
52
setContentView (R .layout .main_activity_layout );
52
53
53
- // Initiate the JSON POST request when the JSON button is clicked
54
- final Button buttonJson = (Button ) findViewById (R .id .submit );
55
- buttonJson .setOnClickListener (new View .OnClickListener () {
54
+ // Initiate the request to the protected service
55
+ final Button submitButton = (Button ) findViewById (R .id .submit );
56
+ submitButton .setOnClickListener (new View .OnClickListener () {
56
57
public void onClick (View v ) {
57
58
new FetchSecuredResourceTask ().execute ();
58
59
}
@@ -91,22 +92,20 @@ protected void onPreExecute() {
91
92
protected Message doInBackground (Void ... params ) {
92
93
final String url = getString (R .string .base_uri ) + "/getmessage" ;
93
94
94
- // Populate the HTTP Basic Auth header with the username and password
95
+ // Populate the HTTP Basic Authentitcation header with the username and password
95
96
HttpAuthentication authHeader = new HttpBasicAuthentication (username , password );
96
97
HttpHeaders requestHeaders = new HttpHeaders ();
97
98
requestHeaders .setAuthorization (authHeader );
98
99
requestHeaders .setAccept (Collections .singletonList (MediaType .APPLICATION_JSON ));
99
100
100
- // HttpEntity object to use for the request
101
- HttpEntity <?> requestEntity = new HttpEntity <Object >(requestHeaders );
102
-
103
101
// Create a new RestTemplate instance
104
102
RestTemplate restTemplate = new RestTemplate ();
103
+ restTemplate .getMessageConverters ().add (new MappingJacksonHttpMessageConverter ());
105
104
106
105
try {
107
106
// Make the network request
108
107
Log .d (TAG , url );
109
- ResponseEntity <Message > response = restTemplate .exchange (url , HttpMethod .GET , requestEntity , Message .class );
108
+ ResponseEntity <Message > response = restTemplate .exchange (url , HttpMethod .GET , new HttpEntity < Object >( requestHeaders ) , Message .class );
110
109
return response .getBody ();
111
110
} catch (HttpClientErrorException e ) {
112
111
Log .e (TAG , e .getLocalizedMessage (), e );
0 commit comments