I am new to programming, please have patience - I successfully done a POST to a web service, but now I want to see the data I got back, how do that? Here is my code:
try {
String data = "{\"params\":{\"stopName\":\"%lauro linhares%\"}}";
CredentialsProvider credProvider = new BasicCredentialsProvider();
credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials("WKD4N7YMA1uiM8V", "DtdTtzMLQlA0hk2C1Yi5pLyVIlAQ68"));
//
DefaultHttpClient http = new DefaultHttpClient();
http.setCredentialsProvider(credProvider);
HttpPost post = new HttpPost("https://api.appglu.com/v1/queries/findRoutesByStopName/run");
try {
post.setEntity(new StringEntity(data, "UTF8"));
} catch (UnsupportedEncodingException e) {
System.out.println( "UnsupportedEncoding: "+ e);
}
post.addHeader("Content-Type", "application/json");
post.addHeader("X-AppGlu-Environment", "staging");
HttpResponse response = http.execute(post);
System.out.println("hi! This is what we get back:"+response.getStatusLine().toString()+", "+response.getEntity().toString());
//Hi! This is what we get back:HTTP/1.1 200 OK, org.apache.http.conn.BasicManagedEntity@4246cb88
// got status 200, which is good, but what can
// I write next to see the
//data i got back from web service ?
} catch (ClientProtocolException e) {
//
System.out.println( "nadir Client protocol exception" + e);
} catch (IOException e) {
//
System.out.println( "nadir IOException"+ e);
}
Try to use:
String str = EntityUtils.toString(response.getEntity());
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments