How to send byte[] and strings to a restful webser

2019-07-11 21:10发布

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.

this is my Android client code:

@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

@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.

How would i go about doing this?

thanks

1条回答
Root(大扎)
2楼-- · 2019-07-11 22:03

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

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:

 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.

查看更多
登录 后发表回答