您的位置:

解决方案:retrofit Utils.parameterError(method, p, "Part map contained null key.")

  发布时间:2025-02-20 23:15:38
本文介绍了在使用Retrofit时出现"Part map contained null key."错误的原因及解决方案。错误通常是因为在传入的Map中包含了空键(null key),解决方法是确保传入的Map对象不包含空键。同时提供了代码示例说明如何正确使用@PartMap注解并排除空键。具体例子展示了如何构建上传文件的网络请求接口并在构建Part Map时避免包含null key。通过检查是否包含null key,可以避免出现错误并确保网络请求的正常发送。

问题原因

retrofit 出现 "Part map contained null key." 错误的原因可能是因为在使用 Retrofit 时,传递的参数中包含了一个空的 key,即参数的 key 为 null。这可能是由于程序逻辑错误、数据处理错误或网络请求拼接错误等原因导致的。在 Retrofit 中,请求参数的 key 不能为空,否则会导致这个错误的出现。

解决方案

当Retrofit出现"Part map contained null key."错误时,这通常是因为在使用@PartMap注解的时候,传入的Map中包含了空键(null key)。要解决这个问题,需要确保传入的Map对象不包含空键。 解决这个问题的方法是在构建Map对象的时候,确保不包含空键,或者在传入Map对象之前对其进行处理,排除空键。另外,也可以考虑使用@Part注解代替@PartMap注解,直接将键值对作为参数传入。 下面是一个简单的例子,演示如何正确使用@PartMap注解并确保不包含空键:


// 创建一个包含键值对的Map对象
Map dataMap = new HashMap<>();
dataMap.put("key1", "value1");
dataMap.put("key2", "value2");

// 移除可能存在的空键
dataMap.entrySet().removeIf(entry -> entry.getKey() == null);

// 使用Retrofit的@PartMap注解传入Map对象
@Multipart
@POST("upload")
Call uploadData(@PartMap Map data);

通过以上方法,可以避免在使用Retrofit时出现"Part map contained null key."的错误。

具体例子

retrofit 中出现 Utils.parameterError(method, p, "Part map contained null key.") 错误通常是由于在进行网络请求时上传的 Part Map 包含了 null key 导致的。要解决这个问题,需要确保在构建 Part Map 时不包含 null key。 为了正确使用 retrofit 避免出现 "Part map contained null key." 错误,可以在构建 Part Map 时,先检查数据是否为 null,再添加到 Part Map 中。下面是一个具体的例子:


import retrofit2.Call;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.PartMap;
import retrofit2.http.QueryMap;
import okhttp3.RequestBody;
import okhttp3.MultipartBody;

import java.util.Map;

public interface ApiService {
    @Multipart
    @POST("upload")
    Call uploadFile(@PartMap Map partMap, @QueryMap Map queryMap);
}

在上述例子中,我们定义了一个上传文件的网络请求接口 uploadFile,其中包含一个 @PartMap 注解用于上传文件的 Part Map。为了避免出现 "Part map contained null key." 错误,我们可以在构建 Part Map 时先检查是否包含 null key,如下所示:


import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import okhttp3.MediaType;
import okhttp3.RequestBody;

import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://example.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ApiService apiService = retrofit.create(ApiService.class);

        Map partMap = new HashMap<>();
        partMap.put("file", RequestBody.create(MediaType.parse("multipart/form-data"), "fileData"));

        // 添加前先检查是否包含 null key
        if (!partMap.containsKey(null)) {
            partMap.put("key", RequestBody.create(MediaType.parse("multipart/form-data"), "value"));
        }

        Map queryMap = new HashMap<>();
        queryMap.put("param", "value");

        Call call = apiService.uploadFile(partMap, queryMap);
        // 发起网络请求
    }
}

通过在构建 Part Map 时先检查是否包含 null key,我们可以避免出现 "Part map contained null key." 错误,并确保网络请求的正常发送。