Android 使用Retrofit擷取JSON資料

來源:互聯網
上載者:User

標籤:接收   enter   void   response   ace   base   刪除   android   char   

        在大家使用網路請求的時候,往往會出現一種情況:需要拿到伺服器返回來的JSON字串,而Retrofit會預設將Json解析,而又沒有直接暴露出拿到Json字串的方法;

      今天測介面的時候,發現當資料正常時,伺服器會返回{"code":200,"datas":"1"},當請求參數有錯誤時,會返回{"code":400,"datas":{"error":"\u59d3\u540d\u4e0d\u80fd\u4e3a\u7a7a"}},datas裡一會兒是字串,一會兒是對象,這時就不能用JAVABEAN的形式接收返回資料了。

樣本:

//刪除地址@FormUrlEncoded@POST(ServiceAddr.ADDR_DEL_URL)Observable<ResponseBody> delAddr(@FieldMap HashMap<String,String> map);
//刪除地址@FormUrlEncoded@POST(ServiceAddr.ADDR_DEL_URL)Call<ResponseBody> delAddr2(@FieldMap HashMap<String,String> map);
Presenter層:
public void delAddr(String baseUrl,HashMap<String,String> map){    addrManageModel.delAddr(baseUrl, map)            .subscribe(new Subscriber<ResponseBody>() {                @Override                public void onCompleted() {                    Logger.t(TAG).d("地址刪除成功");                }                @Override                public void onError(Throwable e) {                    Logger.t(TAG).d("地址刪除失敗"+e.getMessage());                }                @Override                public void onNext(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);                        //擷取Response的body的字串 並列印                        Logger.t(TAG).d(buffer.clone().readString(charset));                        addrManageView.delAddrShow(buffer.clone().readString(charset));                    }                }            });}
或者
public void delAddr2(String baseUrl,HashMap<String,String> map){    addrManageModel.delAddr2(baseUrl, map).enqueue(new Callback<ResponseBody>() {        @Override        public void onResponse(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);                //擷取Response的body的字串 並列印                Logger.t(TAG).d(buffer.clone().readString(charset));                addrManageView.delAddrShow(buffer.clone().readString(charset));            }        }        @Override        public void onFailure(Call<ResponseBody> call, Throwable t) {        }    });}
在頁面上調用:
delAddrShow()更新UI即可。

 





Android 使用Retrofit擷取JSON資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.