2

I have a JSON server (WCF REST) with a JSON POST method that accepts an object containing id (string) and an image (byte[]):

    [DataContract(Name = "image")]
public class CustomImage
{
    [DataMember(Name = "id")]
    public string Id { get; set; }
    [DataMember(Name = "imagestream")]
    public byte[] ImageStream { get; set; }
}

I have managed to send data to the server from an C# console application with the following code:` byte[] bytearray = null;

        Stream stream = File.OpenRead(@"c:\temp\snow.jpg");
        stream.Seek(0, SeekOrigin.Begin);
        bytearray = new byte[stream.Length];
        int count = 0;
        while (count < stream.Length)
        {
            bytearray[count++] = Convert.ToByte(stream.ReadByte());
        }

        CustomImage image = new CustomImage {Id = "43"};
        image.ImageStream = bytearray;
        WebClient Proxy1 = new WebClient();

        Proxy1.Headers["Content-type"] = "application/json";
        MemoryStream ms = new MemoryStream();
        DataContractJsonSerializer serializerToUpload = new DataContractJsonSerializer(typeof (CustomImage));
        serializerToUpload.WriteObject(ms, image);
        byte[] data = Proxy1.UploadData("http://localhost:5465/MyService/file/post", "POST", ms.ToArray());`

But I am not able to get this to work in my Android-application. I am using the following code:` public boolean UploadImage(String id, byte[] imageData) throws JSONException, UnsupportedEncodingException { BasicHttpContext localContext = new BasicHttpContext(); DefaultHttpClient httpClient = getHttpClient();

    HttpResponse response = null;
    HttpPost httpost = null;

    String url = String.format("file/post");
    url = String.format("%s/%s", API_ROOT, url);

    httpost = new HttpPost(url);

    //JSONArray jsonArray = new JSONArray();      
    //for(int i=0;i<imageData.length;i++) {
    //    jsonArray.put(imageData[i]);
    //}

    JSONObject data = new JSONObject();
    data.put("id", id);
    data.put("imagestream", imageData);
    data.put("imagestream", jsonArray);


    StringEntity se = null;
    se = new StringEntity(data.toString());

    httpost.setEntity(se);

    response = null;
    httpost.setHeader("Accept", "application/json");
    httpost.setHeader("Content-type", "application/json");

    try {

        response = httpClient.execute(httpost, localContext);
    } catch (ClientProtocolException e) {
        // System.out.println("HTTPHelp : ClientProtocolException : " +
        // e);
    } catch (IOException e) {
        // System.out.println("HTTPHelp : IOException : " + e);
    }

    if (response != null && response.getStatusLine() != null
            && response.getStatusLine().getStatusCode() == 200) {

        return true;
    }
    return false;
}

But all I am getting is:

`The request body could not be deserialized. End element imageStream from namespace '' expected. Found text '[B@406bb748'.

1 Answer 1

4

Check your image. Maybe it needs to be Base64 encoded before being submitted.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok i got it. But should i have to post String instead of that byte[] ?
Ok i have passed it as string in the json object and it works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.