【问题标题】:Android: Send byte array in JSON using POSTAndroid:使用 POST 以 JSON 格式发送字节数组
【发布时间】:2011-12-20 15:48:39
【问题描述】:

我有一个带有 JSON POST 方法的 JSON 服务器 (WCF REST),该方法接受包含 id(字符串)和图像(byte[])的对象:

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

我已经设法使用以下代码从 C# 控制台应用程序向服务器发送数据:` 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());`

但我无法让它在我的 Android 应用程序中工作。我正在使用以下代码:` 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;
}

但我得到的只是:

`无法反序列化请求正文。应来自命名空间“”的结束元素 imageStream。找到文本“[B@406bb748”。

【问题讨论】:

    标签: android json bytearray http-post


    【解决方案1】:

    检查您的图像。可能需要在提交前进行 Base64 编码。

    【讨论】:

    • 好的,我明白了。但是我应该发布 String 而不是那个 byte[] 吗?
    • 好的,我已经将它作为字符串传递给 json 对象,它工作正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多