Android:使用 POST 以 JSON 格式发送字节数组

标签 android json arrays http-post

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

    [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”。

最佳答案

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

关于Android:使用 POST 以 JSON 格式发送字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8578078/

相关文章:

android - 在 Kotlin 中对 ArrayList 进行排序

android - libphonenumber 可以获取国家代码和国家名称吗?

javascript - 使用 json html javascript 的地址簿

Javascript 表单输入从 json 动态创建,需要将对象映射到另一个对象的正确字段并保存

javascript - 错误 "Uncaught SyntaxError: Unexpected token with JSON.parse"

java - 如何将 string[] 转换为 json

android - 如何看待用户提出的问题,我们这边无法模拟

Android EditText 提示

java - 如何将 fragment 添加到 fragment 事务中?

javascript - 如何在javascript中基于相同的键和值合并两个对象数组?