上传base64编码图像从Android WCF服务 [英] Upload base64 encoding Image to WCF service from android
问题描述
我有一个Android程序,可以上传图片到WCF服务。 WCF服务与接受映像名称,文件夹名称和图像的base64编码的JSON对象。
I have an Android program that can upload an image to a WCF service. The WCF service accepts a JSON object with image name, folder name and a base64 encoding of the image.
我用小提琴手从在线服务测试该服务,我带codeD图像并把它放在对象。然后由POST请求;然后WCF服务上传成功的形象。
I use fiddler for testing that service, I encoded the image from an online service and put it in object. Then composed a POST request ; and then WCF service successfully uploaded image.
但是,当我使用Android的HTTP客户端,做WCF接受请求同样的事情,但我不认识的形象。
But when I use Android HTTP client and doing same thing WCF accepts the request, but I don't recognize the image.
这是我的Android code:
Here is my Android code:
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();
}
我不明白的问题。
I can't understand problem.
推荐答案
大家好我终于想通了问题。
问题是编码,所以我使用类 Base64.java ,我从互联网上下载。我用下面的方法encording
Hi guys finally i figured out problem. Problem is encoding so I use class Base64.java that I downloaded from internet. I use following method for encording
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;
}
和使用返回值作为文件流。所以我的问题就解决了。
and use returned value as file stream. So my problem is solved.
这篇关于上传base64编码图像从Android WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!