android - Getting Data from REST Api -
I have a node.js application with an angular webfind and an android app that connects to the backend through the rest Now I'm not sure how to return items to customers from the node:
Route 1:
res.send (req. User);
If I do this, I can load the user object from angular:
$ http.post ('/ api / login' , User). Civet (function) {$ rootScope.currentUser = data;
and in my Android app (just called, I use Spring & GSN) :
user = springRestTemplate.getRestTemplate (). PostForObject (Routes.getLogin (), User, User.class);
This works fine , But what do I really want to return to node?
Way 2:
will be res.send ({user: req.user});
Coyon remains too much:
$ rootScope.currentUser = data.user;
But for me Android, No other method was found compared to typing conversions:
Progressively field & lt; map & gt; maps = vestor template. Gaterest template (). Postfontent (routes.login (), DUSER, map . category); Linkedream & lt; String, user & gt; Map2 = (LinkedTremap & lt; Strin G, User & gt;) map.getBody (); Map ltm = (map) map.get ("user"); String id = (string) ltm.get ("_ id"); String Username = (string) ltm.get ("userName"); // ... and so on
I got it, why is this a matter (why did I have to use another map except), but I wonder if I have any easy way On the other side is
I left the model and the SpringRestTemplate code because it is a more general structure than how JSON can be controlled with the following structure:
{user: {_id : "1", userName: "foo", ...}}
How much guidance would be appreciated to deal with this problem!
This is simplified, but it will solve your problem.
- Create a POJO that matches the JSON schema
- If you can, use the API to use or more easily to use the Android can do.
- Create a retrofit service (refer to the old link above) and then connect to the API and you will have user object. Option B: Use your object to distribute your spring material to POOS. But I recommend using retropit or its easy and cleaner.
Comments
Post a Comment