When you are using a network request, there is often a situation where you need to get the JSON string returned by the server, and retrofit will parse the JSON by default without directly exposing the method to get the JSON string;
Today, when the interface is measured, the server will return {"Code" when the data is normal, "datas": "1"}, when the request parameter has an error, it will return {"code": "Datas": {"error": "\u59d3\u540d\u4e0d \u80fd\u4e3a\u7a7a "}},datas in a moment is a string, a moment is an object, then you can not use the form of JavaBean to receive the return data.
Example:
// Delete Address @FormUrlEncoded @post (serviceaddr.addr_del_url) Observable<ResponseBody> deladdr (@FieldMap HashMap <String,String> map);
Or
// Delete Address @FormUrlEncoded @post (serviceaddr.addr_del_url)call <ResponseBody> delAddr2 (@FieldMap hashmap< string,string> map);
Presenter Layer:
Public voidDELADDR (String baseurl,hashmap<string,string>map) {addrmanagemodel.deladdr (BASEURL, map). Subscribe (NewSubscriber<responsebody>() {@Override Public voidoncompleted () {logger.t (TAG). D ("Address removal succeeded"); } @Override Public voidOnError (Throwable e) {logger.t (TAG). D ("Address deletion failed"+e.getmessage ()); } @Override Public voidOnNext (responsebody responsebody) {Bufferedsource source=Responsebody.source (); Try{source.request (long.max_value);//Buffer the entire body.}Catch(IOException e) {e.printstacktrace (); } Buffer Buffer=Source.buffer (); Charset Charset=UTF8; MediaType ContentType=Responsebody.contenttype (); if(ContentType! =NULL) {CharSet=Contenttype.charset (UTF8); //gets a string of response body and printslogger.t (TAG). D (Buffer.clone (). ReadString (charset)); Addrmanageview.deladdrshow (Buffer.clone (). ReadString (charset)); } } });}
Or
Public voidDELADDR2 (String baseurl,hashmap<string,string>map) {ADDRMANAGEMODEL.DELADDR2 (BASEURL, map). Enqueue (NewCallback<responsebody>() {@Override Public voidOnresponse (call<responsebody> call, Retrofit2. Response<responsebody>response) {logger.t (TAG). D (Response.body (). toString ()+"===="+response.message () +"==="+Response.code ()); Bufferedsource Source=response.body (). source (); Try{source.request (long.max_value);//Buffer the entire body.}Catch(IOException e) {e.printstacktrace (); } Buffer Buffer=Source.buffer (); Charset Charset=UTF8; MediaType ContentType=response.body (). ContentType (); if(ContentType! =NULL) {CharSet=Contenttype.charset (UTF8); //gets a string of response body and printslogger.t (TAG). D (Buffer.clone (). ReadString (charset)); Addrmanageview.deladdrshow (Buffer.clone (). ReadString (charset)); }} @Override Public voidOnFailure (call<responsebody>Call , Throwable t) { } });}
Called on the page:
Deladdrshow () to update the UI.
Android uses retrofit to get JSON data