Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a RESTful web service from Android?

I have written a REST web service in Netbean IDE using Jersey Framework and Java.

For every request the user needs to provide a username and a password, I know that this authentication is not a best practice (using a curl command like: curl -u username:password -X PUT http://localhsot:8080/user).

Now I want to call a REST web service from an Android Class.

How should I do it?

I have an Android Class which uses DefaultHttpClient and CredentialUsernameAndPassword, but when I run it in Eclipse, sometimes I get a runtime exception or SDK exception.

like image 863
sudo Avatar asked May 18 '11 15:05

sudo


People also ask

What is restful API in Android?

A RESTful API is an architectural style for an application program interface (API) that uses HTTP requests to access and use data. That data can be used to GET, PUT, POST and DELETE data types, which refers to the reading, updating, creating and deleting of operations concerning resources.


2 Answers

This is an sample restclient class

public class RestClient {     public enum RequestMethod     {         GET,         POST     }     public int responseCode=0;     public String message;     public String response;     public void Execute(RequestMethod method,String url,ArrayList<NameValuePair> headers,ArrayList<NameValuePair> params) throws Exception     {         switch (method)         {             case GET:             {                 // add parameters                 String combinedParams = "";                 if (params!=null)                 {                     combinedParams += "?";                     for (NameValuePair p : params)                     {                         String paramString = p.getName() + "=" + URLEncoder.encode(p.getValue(),"UTF-8");                         if (combinedParams.length() > 1)                             combinedParams += "&" + paramString;                         else                             combinedParams += paramString;                     }                 }                 HttpGet request = new HttpGet(url + combinedParams);                 // add headers                 if (headers!=null)                 {                     headers=addCommonHeaderField(headers);                     for (NameValuePair h : headers)                         request.addHeader(h.getName(), h.getValue());                 }                 executeRequest(request, url);                 break;             }             case POST:             {                 HttpPost request = new HttpPost(url);                 // add headers                 if (headers!=null)                 {                     headers=addCommonHeaderField(headers);                     for (NameValuePair h : headers)                         request.addHeader(h.getName(), h.getValue());                 }                 if (params!=null)                     request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));                 executeRequest(request, url);                 break;             }         }     }     private ArrayList<NameValuePair> addCommonHeaderField(ArrayList<NameValuePair> _header)     {         _header.add(new BasicNameValuePair("Content-Type","application/x-www-form-urlencoded"));         return _header;     }     private void executeRequest(HttpUriRequest request, String url)     {         HttpClient client = new DefaultHttpClient();         HttpResponse httpResponse;         try         {             httpResponse = client.execute(request);             responseCode = httpResponse.getStatusLine().getStatusCode();             message = httpResponse.getStatusLine().getReasonPhrase();             HttpEntity entity = httpResponse.getEntity();              if (entity != null)             {                 InputStream instream = entity.getContent();                 response = convertStreamToString(instream);                 instream.close();             }         }         catch (Exception e)         { }     }      private static String convertStreamToString(InputStream is)     {         BufferedReader reader = new BufferedReader(new InputStreamReader(is));         StringBuilder sb = new StringBuilder();         String line = null;         try         {             while ((line = reader.readLine()) != null)             {                 sb.append(line + "\n");             }             is.close();         }         catch (IOException e)         { }         return sb.toString();     } } 
like image 85
Varghese John Avatar answered Sep 24 '22 06:09

Varghese John


Recently discovered that a third party library - Square Retrofit can do the job very well.


Defining REST endpoint

public interface GitHubService {    @GET("/users/{user}/repos")    List<Repo> listRepos(@Path("user") String user,Callback<List<User>> cb); } 

Getting the concrete service

RestAdapter restAdapter = new RestAdapter.Builder()     .setEndpoint("https://api.github.com")     .build(); GitHubService service = restAdapter.create(GitHubService.class); 

Calling the REST endpoint

List<Repo> repos = service.listRepos("octocat",new Callback<List<User>>() {      @Override     public void failure(final RetrofitError error) {         android.util.Log.i("example", "Error, body: " + error.getBody().toString());     }     @Override     public void success(List<User> users, Response response) {         // Do something with the List of Users object returned         // you may populate your adapter here     } }); 

The library handles the json serialization and deserailization for you. You may customize the serialization and deserialization too.

Gson gson = new GsonBuilder()     .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)     .registerTypeAdapter(Date.class, new DateTypeAdapter())     .create();  RestAdapter restAdapter = new RestAdapter.Builder()     .setEndpoint("https://api.github.com")     .setConverter(new GsonConverter(gson))     .build(); 
like image 43
Tianhai Avatar answered Sep 22 '22 06:09

Tianhai