Just came out of school, alas, from Java to Android.
The teacher told us that Android is not important, the result, all the Android class is not playing mobile phone is talking, not serious listening and not serious, homework is not serious, now think good regret ah, so that they will only a button and TextView,
Recently in the company, I first put the previous network programming, and then slightly learn a bit okhttp, because I now only need to know how to use it, after all, the previous Android Foundation is not very good, first learn to use, later have time in slowly understand his method of realization, And httpclient Some of the methods, now the time is estimated too late, first learn to use again. Let me tell you about some of my recent experiences.
The first okhttp POST request (carries an administrator user name (userName) for admin888 request data in the request body, everyone expands according to individual needs):
Request Request = Buildmultipartformrequest ( newnewnullNew Formencodingbuilder (); Builder.add ("UserName", "admin888",new request.builder () . URL (URL) . Post ( Builder.build ()) . Build (); Mokhttpclient.newcall (Request). Enqueue (new Callback () {});
The second okhttp file is uploaded (a key-value pair is passed to the server userName:admin888 and an ebook txt with a file name of 1):
File File =NewFile (Environment.getexternalstoragedirectory (), "1.txt"); Requestbody Filebody= Requestbody.create (Mediatype.parse ("Application/octet-stream"), file); Requestbody Requestbody=NewMultipartbuilder (). Type (Multipartbuilder.form). Addpart (Headers.of ("Content-disposition", "Form-data; Name=\ "Username\" "), Requestbody.create (NULL, "admin888"). Addpart (Headers.of ("Content-disposition", "Form-data; Name=\ "Mfile\"; Filename=\ "1.txt\" "), Filebody). Build (); Request Request=Newrequest.builder (). URL ("File Upload Address"). Post (Requestbody). build (); Call Call=Mokhttpclient.newcall (request); Call.enqueue (NewCallback () {//Write a method here});
Get started with Android from Java proficiency