如何在JSON在Android上发送文件? [英] How to send file in JSON on android?

查看:114
本文介绍了如何在JSON在Android上发送文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的HTTP客户端,我不知道我将如何开始任何人都可以建议我应该需要做的JSON发送文件?我要送这个JSON格式的数据:

I want to send files in JSON using http client I don't know how would I start can anyone suggest what should I need to do? I'm going to send data on this JSON format:

 {
    "Filename": "282850ad-de5c-498f-8280-2d4b6d77b774.jpg",
    "ChunkId":1,
    "ChunkLength":11397,
    "FileLength":11397
 }

正如你看到的,我会在块发送文件。我不知道如果我要将该文件转换成字节数组。如果我需要任何人都可以提供一些样品code的感谢。

As you see I'm going to send files in chunk. I don't know if I'm going to convert the file into byte array. If I needed can anyone give some sample code thanks.

推荐答案

要发送文本文件或图像文件在服务器上,你可以使用MultiPartEntity。

To send Text File or Image File on Server you can use 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());
      }

这是工作,因为这code是我正在运行的项目的一部分。

This is working, as this code is part of my Running project.

这篇关于如何在JSON在Android上发送文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆