1

this is my client side code where i have made json object in the json object i have put byte[] and two string i just converted the bitmap to byte[] and i have receive it on the servlet i using httppost i am trying to send that json data.how can i receive the byte[] and two strings and then i have to show it on the browser and the two strings.

public void getServerData(byte[] img, String name, String gender)throws JSONException, ClientProtocolException, IOException {

        ArrayList<String> stringData = new ArrayList<String>();
        DefaultHttpClient httpClient = new DefaultHttpClient();
        ResponseHandler<String> resonseHandler = new BasicResponseHandler();
        HttpPost postMethod = new HttpPost(SERVER_URL);
        postMethod.setHeader("Content-Type", "application/json");
        JSONObject json = new JSONObject("mydata");
        json.put("image", img);
        json.put("name", name);
        json.put("gender", gender);
        postMethod.setEntity(new ByteArrayEntity(json.toString().getBytes(
                "UTF8")));
        String response = httpClient.execute(postMethod, resonseHandler);
        Log.e("response :", response);
    }

the int doGet(request,response){} i have to receive it.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
akash yadav
  • 349
  • 4
  • 14

1 Answers1

1
JSONObject.put

has no overload accepting a byte array, try encoding those bytes into a base64 string?

See https://stackoverflow.com/questions/9845767/base64-encoder-java

Community
  • 1
  • 1
Machinarius
  • 3,637
  • 3
  • 30
  • 53
  • @ machinarius i have seen this example Base64 but how can recieve it on the servlet – akash yadav Jan 17 '13 at 10:10
  • A Base64 encoded string is still a normal string, just parse the JSON in the servlet and use the decoding function on the linked question. No difficulty on that. – Machinarius Jan 17 '13 at 10:13
  • @ machinarius ok thanks but one more question when i decode it ,it will be in from of byte[] now i will have to convert it into some image form and how can show it onto the browser??? – akash yadav Jan 17 '13 at 10:16
  • 1
    http://stackoverflow.com/questions/1580038/byte-array-to-image-file Use that code, save the functions output somewhere in your server's path and then link it in a img tag or whatever you might want to use. – Machinarius Jan 17 '13 at 10:24
  • :getting image ==null exception at the servlet side java.lang.IllegalArgumentException: image == null! at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpe cifier.java:925) at javax.imageio.ImageIO.getWriter(ImageIO.java:1591) at javax.imageio.ImageIO.write(ImageIO.java:1578) – akash yadav Jan 17 '13 at 11:33
  • Are you sure you are encoding the image properly in the android side? `json.put("image", javax.xml.bind.DatatypeConverter.printBase64Binary(data));` should do it. `byte[] decoded = javax.xml.bind.DatatypeConverter.parseBase64Binary(json.getString("image"));` should do it in the server – Machinarius Jan 17 '13 at 11:49
  • this is my code for client side- byte[] buffer= out.toByteArray(); String ba1=Base64.encodeBytes(buffer); ArrayList nameValuePairs = new ArrayList(); nameValuePairs.add(new BasicNameValuePair("image",ba1)); nameValuePairs.add(new BasicNameValuePair("name",fbName)); nameValuePairs.add(new BasicNameValuePair("gender",fbGender)); – akash yadav Jan 17 '13 at 12:10
  • and on the server side-byte[] image=Base64.decode("image");bufferedImage img = ImageIO.read(new ByteArrayInputStream(image)); i want to send image with two strings Does it possible with this?if yes then how can i get those Strings onto servlet side. – akash yadav Jan 17 '13 at 12:11
  • Sadly, that is outside the scope of this question and i cant really answer it. Please ask another question and specify that you cant get your base64 encoded string from your android client to your servlet. I only have experience with NodeJS and Rails. – Machinarius Jan 17 '13 at 12:26
  • http://stackoverflow.com/questions/14379315/unable-to-get-values-seding-from-client-to-servlet-using-base64 – akash yadav Jan 17 '13 at 12:38