Featured post

Marshmallow Features Point by Point

Android Runtime (“ART”) Improved application performance and lower memory overhead for faster  multi-tasking. Battery Doze...

Monday 9 February 2015

Get Method web-service call in android (Get Response in String)


Api

private final static String URL_BASE="http://www.sunil.com/projects/demo/myProject/api/";
public final static String URL_FINDADEALER = URL_BASE
+ "get_dealers.php?Latitude=%s&Longitude=%s";

Create Request

String uriFindDealer_Request=String.format(SunWebServiceURL.URL_FINDADEALER
,SunConstantValue.SUNcurrent_Map_Lattiude
,SunConstantValue.SUNcurrent_Map_LongLattitude);

// Call Function

String response = webAPIRequest.performWebCallGet(uriFindDealer_Request);

// Create method for http Connection

public String performWebCallGet(String url)
{
String doc_string = null;
try
{
DefaultHttpClient client = new DefaultHttpClient();
URI uri = new URI(url);
HttpGet method = new HttpGet(uri);
HttpResponse res = client.execute(method);
InputStream data = res.getEntity().getContent();
doc_string = convertStreamToString(data);
}
catch (Exception e)
{
e.printStackTrace();
}
return doc_string;
}

public static String convertStreamToString(InputStream is) throws IOException {
if (is != null) {
StringBuilder sb = new StringBuilder();
String line;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((line = reader.readLine()) != null)
{
sb.append(line).append("\n");
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
is.close();
}
return sb.toString();
} else {      
return "false";
}
}

No comments:

Post a Comment