Consume webservice in Android app

First of all give internet permission to app for that below steps.
1. Open “AndroidManifest.xml” file.
2. Copy below code under

Now make one function which is consume web service.
public String doRequest(String requestURL)
{
InputStream is = null;
String jsonString = “”;
HttpClient httpClient = null;
HttpGet request = null;

try {

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters,20*1000); // Connection timeout
HttpConnectionParams.setSoTimeout(httpParameters, 20*1000); // Socket timeout

// Create a new HttpClient and Post Header
httpClient = new DefaultHttpClient(httpParameters);

request = new HttpGet(requestURL);

HttpResponse httpResponse = httpClient.execute(request);

is = httpResponse.getEntity().getContent();

byte buff[] = new byte[1024];
int c = 0;
while ((c = is.read(buff)) != -1) {
jsonString += new String(buff, 0, c);
}
// Log.d(tag, “JSON-String =” + jsonString);
} catch (Exception e) {

e.printStackTrace();
return null;

} finally {
// Close opened streams.
// FIX
try {
if (is != null)
is.close();

if (httpClient != null)
httpClient = null;

if (request != null)
request = null;

} catch (IOException e) {
e.printStackTrace();
}
}
// Log.d(“HttpRequest”, “### Returning ” + jsonString);
return jsonString;
}

— CAll this function when you want to get result of web service —
String requestStr = “http://salesbook.iglobeapps.com/json/loginservice.svc/validateuser?email=demo1&password=demo111”;
String str = doRequest(requestStr);

Share