calling rest web service?

i'm new to android development. i want to call a rest web service and show a simple txt-file or and xls-file. is that possible? and if it is, how? thank you in advance...


You can send a POST for the server and read the result.

This is my implementation with a JSON object, you should do something like it:

JSONObject obj = new JSONObject(); obj.put("jsonrpc", "2.0"); obj.put("method", "getSomething"); JSONObject auth = new JSONObject(); auth.put("email", "user"); auth.put("password", "pass"); params.put("auth", auth); obj.put("params", params); int TIMEOUT_MILLISEC = 10000; // = 10 seconds HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC); HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); HttpClient client = new DefaultHttpClient(httpParams); HttpPost request = new HttpPost("server address"); try { StringEntity entity = new StringEntity(obj.toString()); entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); request.setEntity(entity); ResponseHandler<String> handler = new BasicResponseHandler(); String returnValue = client.execute(request, handler); //returnValue has the return from the server. } catch (Throwable e) { e.printStackTrace(); }


It is better to do this in an async call so that the main UI thread is no blocked. You could consider using this library.

It provides an async wrapper to Apache httpclient available in Android.



Yes, definitely possible. I do variations of this in my own app and what you want to do is use basic networking APIs from the Java platform such as URLConnection.


I have written an async http library. You can check it out https://github.com/darko1002001/android-rest-client.

I have also a simple example added for doing requests with parameters.


You just have to use following method if you are sending parameters in json format`and thats it then you just need to parse the json response .

public String getApiResponse(String url,JSONObject param) { //Log.e("url",url); //Log.e("Params",param.toString()); String responseString = null; HttpResponse httpResponse; HttpClient client=new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(),20000); HttpPost httpPost=new HttpPost(url); try { StringEntity entity=new StringEntity(param.toString()); httpPost.setEntity(entity); httpResponse=client.execute(httpPost); responseString=convertStreamToString(httpResponse.getEntity().getContent()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //Log.e("Response",responseString); return responseString; }` String line; StringBuilder builder=new StringBuilder(); InputStreamReader reader=new InputStreamReader(inputStream); BufferedReader bufferedReader=new BufferedReader(reader); try { while ((line=bufferedReader.readLine())!=null) { builder.append(line); } } catch (IOException e) { e.printStackTrace(); } return builder.toString(); }`



  • Read data of post method in PHP
  • Insert values in mySQL using wcf REST and Android
  • Java Android AsyncHttpClient convert byte[]responseBody to InputStream
  • sotimeout not working in a multipart http post on android 2.1
  • Can't send a correct POST request in Java (Android Studio)
  • How to create a nested json as HttpPost Entity
  • passing JSON object in URL of RESTFUL Web service in Android?
  • How to use android-json-rpc for Android(client)/Java(server) setup?
  • Android: Check network and real internet connectivity
  • Post method from android doesn't work
  • setMaxForRoute does not work in ThreadSafeClientConnManager
  • Web.Api not deserializing properties of Child Array in Post
  • MVC 4 Passing a list from a view to a controller
  • DateTime required in ModelState, however never set to be
  • Android: How to make my retrieved (from mysql) JSON parsed data added to ListView refresh it self ev
  • In MVC/Razor, how can you open a new view instead of as a partial view?
  • kendoui grid in mvc3 security vulnerability, how do i get around it?
  • File upload .NET Core 'IFormFile' does not contain a definition for 'SaveAsASync'
  • Openejb rest integration tests with exception mappers
  • Null Pointer Exception: HTTP Entity
  • Android json parse not working
  • SSL Connection timeout and read timeouts
  • Render a treeview using a for-loop
  • Why action not found in my controller?
  • Returning Json from controller, never a success
  • how to avoid the dependencies hell with unit test in angular 2+
  • WSO2 ESB 4.0.3 - Error installing Data Services feature from 4.0.* repositories
  • How to make http call with file in groovy to upload a file and build arguments
  • Sending cookie value via httpget but not getting the desired response
  • what makes a request a new request in asp.net C#
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • Spring Cloud Microservice Architecture Confusion
  • Transactional Create with Validation in ServiceStack Redis Client
  • Meteor: Do Something On Email Verification Confirmation
  • Cannot resolve symbol 'MyApi'
  • Handling un-mapped Rest path
  • Seeking advice on Jetty HttpClient Hang
  • How to get address from latitude and longitude android google map v2 [duplicate]
  • PHP - How to update data to MySQL when click a radio button
  • using HTMLImports.whenReady not working in chrome