【问题标题】:Upload base64 encoding Image to WCF service from android从android上传base64编码图像到WCF服务
【发布时间】:2014-03-26 12:59:36
【问题描述】:

我有一个可以将图像上传到 WCF 服务的 Android 程序。 WCF 服务接受带有图像名称、文件夹名称和图像的 base64 编码的 JSON 对象。

我使用 fiddler 来测试该服务,我对来自在线服务的图像进行编码并将其放入对象中。然后组成一个 POST 请求;然后WCF服务成功上传图片。

但是当我使用 Android HTTP 客户端并做同样的事情时,WCF 接受了请求,但我无法识别图像。

这是我的 Android 代码:

try {
    HttpPost httpPost=new HttpPost(this.remoteBasePath);
    JSONObject obj = new JSONObject();
    obj.put(FILE_NAME, fileName);
    obj.put(FILE_STREAM,fileStream);
    obj.put(FOLDER_NAME, remoteFolder);
    httpPost.setHeader("Content-type", "application/json; charset=utf-8");
    httpPost.setEntity(new StringEntity(obj.toString()));
    httpPost.setHeader(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    httpResponse=httpClient.execute(httpPost);
    if(httpResponse!=null){
        TLLog.d(TAG,"StatusCode : "+ httpResponse.getStatusLine().getStatusCode());
        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {                               
            InputStream instream = httpResponse.getEntity().getContent();
            BufferedReader r = new BufferedReader(
            new InputStreamReader(instream));
            StringBuilder total = new StringBuilder();
            String line;
            while ((line = r.readLine()) != null) {
                total.append(line);
            }
            instream.close();
            String result = total.toString();
            TLLog.d(TAG,"Image posting result : "+ result);
            return true;
        }
    }
} catch (ClientProtocolException e) {
    TLLog.e(TAG, e.getStackTrace().toString());
} catch (IOException e) {
    TLLog.e(TAG, e.getStackTrace().toString());
} catch (JSONException e) {
    e.printStackTrace();
}

我无法理解问题。

【问题讨论】:

  • 如果只是图片的问题,我猜fileStream放到JSON对象里面就已经错了。
  • 感谢您的回复。现在我正在努力。

标签: android wcf base64


【解决方案1】:

大家好,我终于找到了问题。 问题在于编码,所以我使用从互联网下载的 Base64.java 类。我使用以下方法进行编码

public static String encodeTobase64(Bitmap image) throws IOException
{
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();  
    image.compress(Bitmap.CompressFormat.JPEG, 50, outputStream);
    byte[] bs = outputStream.toByteArray();
    String imageEncoded=Base64.encodeBytes(bs);
    return imageEncoded;
}

并使用返回值作为文件流。这样我的问题就解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    相关资源
    最近更新 更多