How can I return String or JSONObject from asynchronous callback using Retrofit?

后端 未结 7 1801
无人及你
无人及你 2020-11-30 00:34

For example, calling

api.getUserName(userId, new Callback() {...});

cause:

retrofit.RetrofitError: retrofit.         


        
7条回答
  •  醉梦人生
    2020-11-30 01:16

    Here is what I did, after poking around in the debugger. Note: this is for actually getting it inside an error callback, not the success callback.

    You'll see that the success type is found by calling retrofitError.getSuccessType() and returns and object of type Type

    You can then call retrofitError.getBodyAs(YourType.class) which is all I needed to do because for me its always the class I expect it to be.

    Here is the one-liner answer:

    retrofitError.getBodyAs(retrofitError.getSuccessType())
    

    Now, I'll note that I dont have to do anything like this regarding the success callback because it's already working magically.

提交回复
热议问题