How to create Rest api,and how to consume the rest api with android apps -


how have create rest api our websites,i want connect server android apps using rest api.how connect rest api android apps,what ways behind those.

here have nice rest api, basics doing http post , get:

public class restclient {  private arraylist<namevaluepair> params; private arraylist<namevaluepair> headers; private string url; private string response; private int responsecode;  public string getresponse() {     return response; }  public int getresponsecode() {     return responsecode; }  public restclient(string url) {     this.url = url;     params = new arraylist<namevaluepair>();     headers = new arraylist<namevaluepair>(); }  public void addparam(string name, string value) {     params.add(new basicnamevaluepair(name, value)); }  public void addheader(string name, string value) {     headers.add(new basicnamevaluepair(name, value)); }  public void execute(requesttype requesttype) throws exception {     switch(requesttype)     {         case get:         {             string combinedparams = "";             if (!params.isempty())             {                 combinedparams += "?";                 (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);              (namevaluepair h: headers)                 request.addheader(h.getname(),h.getvalue());              executerequest(request, url);             break;         }         case post:         {             httppost request = new httppost(url);              (namevaluepair h : headers)             {                 request.addheader(h.getname(), h.getvalue());             }              if(!params.isempty()){                 request.setentity(new urlencodedformentity(params, http.utf_8));             }              executerequest(request, url);             break;         }     } }  public void executerequest(httpurirequest request, string url)  {     httpclient client = new defaulthttpclient();     httpresponse httpresponse;     try     {         httpresponse = client.execute(request);         responsecode = httpresponse.getstatusline().getstatuscode();          httpentity entity = httpresponse.getentity();          if (entity != null)         {             inputstream in = entity.getcontent();             response = convertstreamtostring(in);             in.close();         }     }     catch (clientprotocolexception e)  {         client.getconnectionmanager().shutdown();         e.printstacktrace();         } catch (ioexception e) {         log.e("rest_client", "execute request: " + e.getmessage());          client.getconnectionmanager().shutdown();          e.printstacktrace();     }        }  private string convertstreamtostring(inputstream in) {     bufferedreader reader = new bufferedreader(new inputstreamreader(in));     stringbuilder sb = new stringbuilder();      string line = null;     try      {         while ((line = reader.readline()) != null) {         sb.append(line + "\n");     }     }     catch (ioexception e)      {         e.printstacktrace();     }           {         try          {             in.close();         }          catch (ioexception e)          {              log.e("rest_client", "convertstreamtostring: " + e.getmessage());               e.printstacktrace();         }     }     return sb.tostring(); } 

}


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -