发布数组的Retrofit2问题

时间:2016-12-08 18:26:19

标签: android retrofit2

我正在为Android开发一个客户端应用程序,我的API要求我在def upload(instances, u1): for instance in instance: try: u1.recv() #do_something except: #do_something_else continue def stop(instances, s1): for instance in instances: RunningStatus = instance[4] if RunningStatus.lower() == 'stopped'.lower(): s1.send(Exception) # I want to raise exception in upload function # from here if __name__ == '__main__': s1, u1 = multiprocessing.Pipe() s = multiprocessing.Process(target = stop, args = (instances, s1,)) u = multiprocessing.Process(target = upload, args = (instances, u1)) s.start() u.start() u.join() 中发送图片名称,如:

ArrayList<String>

但是当我发送它时,它的形式如下:

collection[0] = 15a877ce9f22bc8349cac80565c4bff6.jpg
collection[1] = 25a877ce9f22bc8349cac80565c4bff6.jpg

我的改造界面:

collection[] = 15a877ce9f22bc8349cac80565c4bff6.jpg
collection[] = 25a877ce9f22bc8349cac80565c4bff6.jpg

如何达到要求的结果?

任何帮助将不胜感激!谢谢!

2 个答案:

答案 0 :(得分:0)

不要将[]放在@Field名称中,我不确切知道你为什么要这样做,但这可能会令人困惑,因为那些是保留字符... < / p>

您可以使用以下内容:

// ApiService.java
@FormUrlEncoded
@POST("/api/projectLost")
public void projectMarkLost(
    @Field("apiKey") String apiKey,
    @Field("project_id") int projectId,
    @Field("lost_project_remark") String lostProjectRemark,
    @Field("lost_project_reasons") ArrayList<Integer> lostProjectReasons,
    Callback<JsonObject> cb
);

// Content wish to post
POST content:
apiKey = EzAvFvScs45a2DRI0MktdIDeVRbP59
project_id = 986
remark = testing
details = [detail_1,detail_2]

答案 1 :(得分:0)

我找到了使用Map来获得理想结果的解决方案

@FormUrlEncoded
@POST("user/news")
Call<CreateNews> createNews(
        @Field("text") String text,
        @FieldMap Map<String, String> collection);

并在方法中:

public void createNews(String text, ArrayList<String> collection, final Callback<CreateNews> callback){
    News createNews = ServiceGenerator.createService(News.class);

    SortedMap fields = new TreeMap<String, String>();

    for (int i = collection.size()-1 ; i >= 0 ; i--) {
        fields.put("collection["+i+"]", collection.get(i));
    }

    Call<CreateNews> call = createNews.createNews(text, fields);
    call.enqueue(callback);