java 如何将字节 [] 和字符串发送到静态 Web 服务并在 Web 方法实现中检索此信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36067580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:56:01  来源:igfitidea点击:

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

javaandroidweb-servicesrestnetbeans-8

提问by skandal

I am trying to send a byte[] as one of the parameters from my android client and receive this information in my restful web service that i have implemented in netbeans.

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

this is my Android client code:

这是我的 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;
}

and this is my web service implementation code for this method

这是我针对此方法的 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;
}

i have currently tried puttin the encoded byte[] into the url but it cannot hold all of the byte[] information, i have looked at using the httpurlconnection POST method but i dont quite understand how to use it in my situation.

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

How would i go about doing this?

我该怎么做呢?

thanks

谢谢

采纳答案by skandal

So basically, with my POST methods, i changed the parameters in my webservice to @formparams and made it that it consumses application/x-www-form-urlencoded. i also changed the variable to a string so that the webservice recieves the base64 strings and then decoded the string with base64.geturldecoder.decode()

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

eg:

例如:

@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 ){

and in the android client i my post function:

在 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();

where the byte array was encoded to string with

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

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

it didnt work before because the byte array encoded string was too long as a url parameter, hence the only way to do it was to put it in the body of the method and send it that way.

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