如何将字节 [] 和字符串发送到 restful 网络服务并在网络方法实现中检索此信息

How to send byte[] and strings to a restful webservice and retrieve this information in the web method implementation

我正在尝试从我的 android 客户端发送一个字节 [] 作为参数之一,并在我在 netbeans 中实现的 restful Web 服务中接收此信息。

这是我的 Android 客户端代码:

@Override
protected String doInBackground(String... params) {

    String param1 = params[0];
    String param2 = params[1];
    String param3 = params[2];
    String param4 = params[3];
    String param5 = params[4];
    String param6 = params[5];

   //param6 is a Base64 encoded byte array to string

    String url = "http://ipAdress:port/example/api/booking/Booking?";

    String parameters = "param1=" + param1 + "&param2=" +param2+ "&param3="+param3+"&param4="+param4+"&param5="+param5+"&param6="+ URLEncoder.encode( new String(param6),"UTF-8"));
    try {
        URL Url = new URL(url);

        HttpURLConnection con = (HttpURLConnection) 
        Url.openConnection();

        con.setInstanceFollowRedirects( false );
        con.setRequestMethod( "POST" );

        con.setDoOutput( true );
        DataOutputStream wr = new DataOutputStream(connect2Rest.getOutputStream());
        wr.writeBytes(parameters);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();


        BufferedReader is = new BufferedReader(new InputStreamReader(connect2Rest.getInputStream()));
        String inString;
        StringBuilder sb = new StringBuilder();
        while ((inString = is.readLine()) != null) {
            sb.append(inString + "\n");
        }

        is.close();
        String xml = sb.toString();

        System.out.print(responseCode);

        return xml;



    } catch (MalformedURLException e) {
        System.out.println("MalformedURLException: " + e);
    } catch (IOException e) {
        System.out.println("IOException: " + e);
    }
    return null;
}

这是我针对此方法的 Web 服务实现代码

@POST
@Path("createBooking")
@Consumes({MediaType.APPLICATION_XML})
public Bookings createBooking(@QueryParam("param1") String param1, @QueryParam("param2") String param2, @QueryParam("param3") String param3, @QueryParam("param4") String param4, @QueryParam("param5") String param5, @QueryParam("param6") byte[] param6 ){

    // needs implementation
    return null;
}

我目前已经尝试将编码的 byte[] 放入 url 但它不能保存所有的 byte[] 信息,我已经查看了使用 httpurl 连接 POST 方法,但我不太明白如何在我的情况下使用它。

我该怎么做?

谢谢

所以基本上,使用我的 POST 方法,我将我的网络服务中的参数更改为 @formparams 并使其消耗 application/x-www-form-urlencoded。我还将变量更改为字符串,以便 web 服务接收 base64 字符串,然后使用 base64.geturldecoder.decode()

解码字符串

例如:

@POST
@Path("createBooking")
@Consumes({MediaType.APPLICATION_FORM_URLENCODED})
@Produces({MediaType.TEXT_PLAIN})
public String createBooking(@FormParam("param1") String param1){@FormParam("param1") String param1, @FormParam("param2") String param2, @FormParam("param3") String param3, @FormParam("param4") String param4, @FormParam("param5") String param5, @FormParam("param6") String param6 ){

并且在 android 客户端中我的 post 函数:

 con.setRequestMethod( "POST" );
        con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        con.setRequestProperty( "charset", "utf-8");
        con.setRequestProperty( "Content-Length", Integer.toString( postData.length ));


        con.setDoOutput( true );

        con.setUseCaches( false );
        DataOutputStream wr = new DataOutputStream(connect2Rest.getOutputStream());
        wr.write(postData);
        wr.flush();
        wr.close();

其中字节数组被编码为字符串

Base64.encodeToString(byte[],  Base64.NO_WRAP + Base64.URL_SAFE);

它之前没有工作,因为字节数组编码的字符串作为 url 参数太长,因此唯一的方法是将它放在方法的主体中并以这种方式发送。