Because you are using a raw type in
ResponseHandler handler = ...
With raw types, the type variables in the method declarations are erased. So everything appears as Object
(or whatever the leftmost bound of the type parameter is).
Instead, use a parameterized type
ResponseHandler<String> handler = ...
This also works because BasicResponseHandler
extends ResponseHandler<String>
.
Now
httpclient.execute(request, handler);
will have a return type associated with the type argument used when declaring handler
, which is String
and the result can therefore be assigned to a String
variable (or anywhere a String
is expected).