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

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

For example, calling

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

cause:

retrofit.RetrofitError: retrofit.         


        
7条回答
  •  日久生厌
    2020-11-30 01:05

    A possible solution would be to use JsonElement as the Callback type (Callback). In your original example:

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

    In the success method you can convert the JsonElement to either a String or a JsonObject.

    JsonObject jsonObj = element.getAsJsonObject();
    String strObj = element.toString();
    

提交回复
热议问题