如何在Android上用JSON发送文件?
作者:互联网
我想用http客户端发送JSON文件我不知道如何启动任何人都可以建议我应该做什么?我将发送有关此JSON格式的数据:
{
"Filename": "282850ad-de5c-498f-8280-2d4b6d77b774.jpg",
"ChunkId":1,
"ChunkLength":11397,
"FileLength":11397
}
如你所见,我将以块状发送文件.我不知道我是否要将文件转换为字节数组.如果我需要,任何人都可以给一些示例代码谢谢.
解决方法:
要在服务器上发送文本文件或图像文件,您可以使用MultiPartEntity.
DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient();
FileBody localFileBody = new FileBody(new File(this.picturePath), "image/jpg");
HttpPost localHttpPost = new HttpPost("http://website.com/path/....");
MultipartEntity localMultipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
try
{
Log.d("picturepath", this.picturePath);
localMultipartEntity.addPart("Email", new StringBody("emailid@gmail.com"));
localMultipartEntity.addPart("password", new StringBody("password"));
localMultipartEntity.addPart("phone", new StringBody("9875......."));
localMultipartEntity.addPart("profilepicture", localFileBody);
localHttpPost.setEntity(localMultipartEntity);
HttpResponse localHttpResponse = localDefaultHttpClient.execute(localHttpPost);
System.out.println("responsecode" + localHttpResponse.getStatusLine().getStatusCode());
}
catch (Exception e)
{
Log.d("exception", e.toString());
}
这是有效的,因为此代码是我的Running项目的一部分.
标签:json,android,chunked 来源: https://codeday.me/bug/20190714/1460303.html