java - First time making a REST service, calling from android -


i've started learning android programming , how create restful web service. made simple yahtzee game android , expand knowledge on both platforms wanted implement 2 player system using restful service.

i made service in asp.net mvc code below request url/games/2 return:

<game>    <id>2</id>    <p1>100</p1>     <p2>99</p2>     <turn>1</turn> </game> 

all want able create new game calling post , use check if turn yet , when finish turn using put update game, changing score , turn other players request let client know turn. know basic 2 player functionality it's i'm trying learn process. have game object in android i'm not sure how proceed. direction i'm working on @ moment httpclient post looking like:

httppost httppost = new httppost("url/games") 

but don't see how pass parameter. post method in service takes game object parameter. if give me advice appreciate it.

model, game.cs:

namespace yahtztest.models {     public class game     {         public int id { get; set; }         public int turn { get; set; }         public int p1 { get; set; }         public int p2 { get; set; }     } } 

controller, gamescontroller.cs:

    namespace yahtztest.controllers {     public class gamescontroller : apicontroller     {         static readonly igamerepository repository = new gamerepository();          public ienumerable<game> getallgames()         {             return repository.getall();         }          public game getgame(int id)         {             game item = repository.get(id);             if (item == null)             {                 throw new httpresponseexception(httpstatuscode.notfound);             }             return item;         }          public httpresponsemessage postgame(game item)         {             item = repository.add(item);             var response = request.createresponse<game>(httpstatuscode.created, item);              string uri = url.link("defaultapi", new { id = item.id });             response.headers.location = new uri(uri);             return response;         }          public void putgame(int id, game game)         {             game.id = id;             if (!repository.update(game))             {                 throw new httpresponseexception(httpstatuscode.notfound);             }         }          public void deletegame(int id)         {             game item = repository.get(id);             if (item == null)             {                 throw new httpresponseexception(httpstatuscode.notfound);             }              repository.remove(id);         }      } } 

and couple other files tutorial followed included storing games, gamerepository.cs , igamerepository.cs:

    namespace yahtztest.models {     interface igamerepository     {         ienumerable<game> getall();         game get(int id);         game add(game item);         void remove(int id);         bool update(game item);     } }       namespace yahtztest.models {     public class gamerepository : igamerepository     {         private list<game> games = new list<game>();         private int _nextid = 1;          public gamerepository()         {             add(new game { turn = 0, p1 = 0, p2 = 0 });             add(new game { turn = 1, p1 = 100, p2 = 99 });             add(new game { turn = 0, p1 = 45, p2 = 75 });         }            public ienumerable<game> getall()         {             return games;         }          public game get(int id)         {             return games.find(p => p.id == id);         }          public game add(game item)         {             if (item == null)             {                 throw new argumentnullexception("item");             }             item.id = _nextid++;             games.add(item);             return item;         }          public void remove(int id)         {             games.removeall(p => p.id == id);         }          public bool update(game item)         {             if (item == null)             {                 throw new argumentnullexception("item");             }             int index = games.findindex(p => p.id == item.id);             if (index == -1)             {                 return false;             }             games.removeat(index);             games.add(item);             return true;         }     } } 

basically write string on post. , on server side, when onpostreceived need recreate object string. don't know how want send data (content-type), 1

if want post object server can this: (json example)

        httpclient httpclient = httphelper.gethttpclient();         httppost httppost = new httppost("yourserveraddress");         httppost.setheader("accept", "application/json; charset=utf-8");         httppost.setheader("content-type", "application/json; charset=utf-8");          // stringentity         string instr = yourobject.tostring();         stringentity se = new stringentity(instr, http.utf_8);          // params         httpparams params = new basichttpparams();         params.setparameter(coreprotocolpnames.http_element_charset, http.utf_8);         httppost.setparams(params);                  httppost.setentity(se);          // fire , read response         httpresponse response = httpclient.execute(httppost);          // read answer         string content = null;         inputstream stream = null;         try {             if (response != null) {                 stream = response.getentity().getcontent();                 inputstreamreader reader = new inputstreamreader(stream, http.utf_8);                 bufferedreader buffer = new bufferedreader(reader);                 stringbuilder sb = new stringbuilder();                 string cur;                 while ((cur = buffer.readline()) != null) {                     sb.append(cur);                 }                 //here's whole response server if provide                 content = sb.tostring();             }         } {             if (stream != null) {                 stream.close();             }         }                 } catch (exception e) {         e.printstacktrace();     } 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -