Android uses retrofit to get JSON data

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.