2

我正在尝试从 android 调用球衣服务

@POST
@Path("/share")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response shareProgramVideo(@FormParam("from")Integer from, @FormParam("to")List<Integer> to,@FormParam("programIds")List<Integer> programIds) 

该服务适用于 html 表单

<html>
    <head>
        <title>REST with Forms</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <br />
        <form method="post" action="../dataService/rest/secure/program/share">

             from: <input type="text" name="from" id="from" /><br />

             to 1: <input type="text" name="to" id="to" /><br />
             to 2: <input type="text" name="to" id="to" /><br />

             programIds 1: <input type="text" name="programIds" id="programIds" /><br />
             programIds 2: <input type="text" name="programIds" id="programIds" /><br />
            <input type="submit" value="Submit" />
        </form>
    </body>
</html>

我正在使用 android 和 HttpRequest 库(https://github.com/kevinsawicki/http-request),但它给了我 400 bad request 错误

     List<Integer> programs = new ArrayList<Integer>();
programs.add(1);
programs.add(2);

List<Integer> to = new ArrayList<Integer>();
to.add(1);
to.add(2);

Map<String, Object> data = new HashMap<String, Object>();
data.put("from", 1);
data.put("to", to);
data.put("programIds", programs);

HttpRequest request = HttpRequest.post(url)
        .basic(WebServiceConfig.ADMIN, WebServiceConfig.ADMIN_PSW).contentType("application/x-www-form-urlencoded")
        .acceptJson().form(data);

if (request.created() || request.ok()) {

    return true;

} else {

    Log.e("CreateUserProgramTask",
            "request.code()=" + request.code() + "request.body()="
                    + request.body());

    return false;

}
4

1 回答 1

0

我不知道您正在使用的库,但我认为问题可能出在您创建“程序”和“到”参数的方式上:

List<Integer> programs = new ArrayList<Integer>();
programs.add(1);
programs.add(2);

List<Integer> to = new ArrayList<Integer>();
to.add(1);
to.add(2);

Map<String, Object> data = new HashMap<String, Object>();
data.put("from", 1);
data.put("to", to);
data.put("programIds", programs); 

似乎该库只是在列表中执行 toString() 调用,但我认为您应该序列化为:

programs = "program1,program2"
to = "to1,to2"

我认为这不是数据地图中所写的内容

于 2013-05-27T18:56:38.370 回答