Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将数组作为HashMap发送会导致不同的格式_Java_Arrays_Retrofit2 - Fatal编程技术网

Java 将数组作为HashMap发送会导致不同的格式

Java 将数组作为HashMap发送会导致不同的格式,java,arrays,retrofit2,Java,Arrays,Retrofit2,我试图将一个String[]数组作为HashMap中的名称-值对发送到后端,但后端收到的负载如下所示: Parameters: {"gallery_file_ids"=>"[Ljava.lang.String;@4acac2e"} 不会发送数组值,并且数组值周围还有一个附加的“”。这是我的密码: 设置阵列: String[] gallery_array = new String[2]; gallery_array[0] = "2134564"; HashMap<String, O

我试图将一个
String[]
数组作为HashMap中的名称-值对发送到后端,但后端收到的负载如下所示:

Parameters: {"gallery_file_ids"=>"[Ljava.lang.String;@4acac2e"}
不会发送数组值,并且数组值周围还有一个附加的“”。这是我的密码:

设置阵列:

String[] gallery_array = new String[2];
gallery_array[0] = "2134564";

HashMap<String, Object> params = new HashMap<>();
params.put("gallery_file_ids", gallery_array);
//An object is sent in the body instead of a HashMap
@PATCH("api/profile.json")
Call<BusinessProfile> updateProfile(@Body NewGalleryUpload newGalleryUpload);
String[]gallery_数组=新字符串[2];
图库_数组[0]=“2134564”;
HashMap params=新的HashMap();
参数put(“库文件ID”,库数组);
改装API

@FormUrlEncoded
@PATCH("api/profile.json")
Call<Profile> updateProfile(@FieldMap HashMap<String, Object> params);
@FormUrlEncoded
@补丁(“api/profile.json”)
调用updateProfile(@FieldMap HashMap params);

据我所知,字符串数组的字符串表示形式是通过改装形成的。如何解决这个问题?

我通过创建一个包含ArrayList而不是string数组的自定义java类来解决这个问题:

自定义类-NewGalleryUpload.java

public class NewGalleryUpload {
    public ArrayList<String> gallery_file_ids = new ArrayList<>();
}
公共类NewGalleryUpload{
public ArrayList gallery_file_id=new ArrayList();
}
改装API更改为以下内容:

String[] gallery_array = new String[2];
gallery_array[0] = "2134564";

HashMap<String, Object> params = new HashMap<>();
params.put("gallery_file_ids", gallery_array);
//An object is sent in the body instead of a HashMap
@PATCH("api/profile.json")
Call<BusinessProfile> updateProfile(@Body NewGalleryUpload newGalleryUpload);
//在主体中发送对象,而不是HashMap
@补丁(“api/profile.json”)
调用updateProfile(@Body NewGalleryUpload NewGalleryUpload);

因此,基本上,
NewGalleryUpload
的一个新对象被初始化,值被添加到
gallery\u file\u id
ArrayList中,并传递到请求体中。希望这对别人有帮助

@FieldMap
接受实数类型为
Map
,无论您的值是
String[]
还是其他值,都会使用
String将值转换为字符串。valueOf(Object)
,它与

如果要将数组用作参数,可以尝试以下代码:

@FormUrlEncoded
@PATCH("api/profile.json")
Call<Profile> updateProfile(@Field("gallery_file_ids")  ArrayList<String> ids);
@FormUrlEncoded
@补丁(“api/profile.json”)
调用updateProfile(@Field(“gallery\u file\u id”)数组列表id);

或者您可以看到问题

如果您想在Hashmap中添加值,那么您必须一次输入一个索引值,如下所示:params.put(“gallery_file_id”,gallery_array[0])

@Headers(“内容类型:application/json”)
@POST(“api/profile.json”)
调用updateProfile(@body List data);
您需要将类类型列表传递给post jsonArrayList

要将自定义与自定义标题一起使用,它将如下所示-

@Headers("Content-Type: application/json")
@POST("api/profile.json")
Call<Profile> updateProfile(@HeaderMap Map<String, String> headers, @body List<YuorClassName> data);
@Headers(“内容类型:application/json”)
@POST(“api/profile.json”)
调用updateProfile(@HeaderMap-headers,@body-List-data);
从你的活动中——

    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put("SESSION_ID",getSeassionDataNew().getData().getSESSIONID());
    headers.put("SESSION_TOKEN", getSeassionDataNew().getData().getSESSIONTOKEN());

    .....

    List<YuorClassName> data = new ArrayList<>();

    ......

    Call call = apiInterface.updateProfile(headers , data);
HashMap headers=newhashmap();
headers.put(“SESSION_ID”,getsessiondatanew().getData().getSESSIONID());
headers.put(“SESSION_TOKEN”,getsessiondatanew().getData().getSESSIONTOKEN());
.....
列表数据=新的ArrayList();
......
Call Call=apinterface.updateProfile(标题、数据);

gallery\u array
只是对包含数组内容的内存地址的引用。您必须说明要对数组执行什么操作(即将其转换为逗号分隔的列表)。也许对于改装来说,将
HashMap
声明为
@nbokmans就足够了。仍然会在后端记录相同的响应