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 + "¶m2=" +param2+ "¶m3="+param3+"¶m4="+param4+"¶m5="+param5+"¶m6="+ 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
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:
and in the android client i my post function:
where the byte array was encoded to string with
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.