改造 - 如何转换响应HashMap类?HashMap

2023-09-06 07:53:05 作者:只是过客.何必再念

我有一个关于使用改造由广场Android开发问题。比方说,我有一个Response对象实际上是一个的HashMap<字符串,字符串方式> 对象

I have a question about Android development using Retrofit by Square. Let's say I have a Response object which is actually a HashMap<String, String> object.

有没有办法从这个改造对象获得的HashMap 对象?

Is there a way to get a HashMap object from this Retrofit object?

谢谢!

推荐答案

响应对象包含的各种信息有关的响应从服务器返回。

Response object contains various information about response returned from the server.

如果你的意思是响应体是的HashMap 您可以解析时身体成功()被调用,或者当你建立 RestAdapter 和写解串器将解析和填充的HashMap 注册类型的适配器。

If you mean that response body is HashMap you can either parse the body when success() is called, or register type adapter when build your RestAdapter and write deserializer that will parse and populate the HashMap.

 private RestAdapter getRestAdapter(){
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(new TypeToken<HashMap<String, String>>(){}.getType(), new MyHashMapDeserializer());
        RestAdapter.Builder builder = new RestAdapter.Builder();
        builder.setClient(new OkClient());
        builder.setLogLevel(RestAdapter.LogLevel.FULL);
        builder.setExecutors(Executors.newCachedThreadPool(), new MainThreadExecutor());
        builder.setConverter(new GsonConverter(gsonBuilder.create()));
        builder.setEndpoint(API_END_POINT_URL);
        return builder.build();
    }

然后创建一个新的类(或内部类),将实施JsonDeserializer&LT; HashMap的&LT;字符串,字符串&GT;&GT; 和实施反序列化的逻辑

请确保你适应 K,V 类型您的需求。

Make sure you adapt K,V types to your needs.