Zatan
In the development of Android, our data is generally received through the interface, referring to the interface refers to the web Api,webservice,wcf,web applications, etc., they serve as the server and the database for direct communication, And the app this block by sending HTTP requests to these interfaces to obtain data, the advantage of the uncle thinks, can effectively reduce the software development difficulty, so the data interaction is separated to the service layer, and customer interaction is completely in the app side, which is similar to the current more popular SOA architecture, That is, a service for a variety of Terminal Services, whether your Web site, mobile phone iOS, mobile phone Android, tablet or other TV, and so on, all unified call the service layer interface!
It's a bit far, so let's look at how the session is shared with the Server API when HTTP is being sent to the app side
The principle is that we need to be clear.
-〉 Client
-〉 (Request) to access the server page
-〉 Server Generation SessionID
-〉 Storage to the server
-〉 (Response) corresponding to the client
-〉 client storage put SessionId into cookies (. NET platform cookies with the key named Asp.net_sessionid)
-〉 next request, the client will send the current SessionID to the server in the request header message.
-〉 service-side sessionid to maintain its effectiveness through expiration time
The code of practice comes from MSDN
Selected from: MSDN articles about HttpClient's cookiecontaineR
Uri uri =NewUri ("http://www.microsoft.com"); Httpclienthandler Handler=NewHttpclienthandler (); Handler. Cookiecontainer=NewCookiecontainer (); Handler. Cookiecontainer.add (URI,NewCookies ("name","value"));//Adding a CookieHttpClient client =NewHttpClient (handler); Httpresponsemessage Response=awaitclient. Getasync (URI); Cookiecollection Collection= handler. Cookiecontainer.getcookies (URI);//Retrieving a Cookie
The code in the Uncle's project
Uri uri =NewUri (GetString (Resource.String.apiHost)); Httpclienthandler Handler=NewHttpclienthandler (); Handler. Cookiecontainer=NewCookiecontainer (); Handler. Cookiecontainer.add (URI,NewCookies ("Asp.net_sessionid", Inmemory.sessionid));//Adding a Cookie using(varHTTP =NewHttpClient (handler)) { varContent =NewFormurlencodedcontent (Newdictionary<string,string>() { }); varResponse = http. Postasync (GetString (Resource.String.apiHost) +"/test/currenttasklistapi", content); varobj = jsonconvert.deserializeobject<list<task_info>>(response. Result.Content.ReadAsStringAsync (). Result); Listview.adapter=NewTask_infolistadapter ( This, obj); }
If you're using Xamarin to develop mobile apps, try it!
Finally, the uncle to say, the understanding of a concept determines the way you take the solution!
Uncle also said Xamarin~android article ~ for httpclient sharing Session,android and API session sharing mechanism