本文介绍了使用 Http Post 发送图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Http Post 将图像从 android 客户端发送到 Django 服务器.该图像是从图库中选择的.目前,我使用列表值名称 Pairs 将必要的数据发送到服务器并以 JSON 格式接收来自 Django 的响应.是否可以对图像使用相同的方法(在 JSON 响应中嵌入图像的 url)?

I want to send an image from the android client to the Django server using Http Post. The image is chosen from the gallery. At present, I am using list value name Pairs to send the necessary data to the server and receiving responses from Django in JSON. Can the same approach be used for images (with urls for images embedded in JSON responses)?

另外,哪个是更好的方法:远程访问图像而不从服务器下载它们或将它们下载并存储在位图数组中并在本地使用它们?图像数量少(<10)且尺寸小(50*50 倾角).

Also, which is a better method: accessing images remotely without downloading them from the server or downloading and storing them in a Bitmap array and using them locally? The images are few in number (<10) and small in size (50*50 dip).

任何解决这些问题的教程将不胜感激.

Any tutorial to tackle these problems would be much appreciated.

从图库中选择的图像在缩放到所需大小后发送到服务器.

The images chosen from the gallery are sent to the server after scaling it to required size.

推荐答案

我假设您知道要上传的图像的路径和文件名.将此字符串添加到您的 NameValuePair 中,使用 image 作为键名.

I'm going to assume that you know the path and filename of the image that you want to upload. Add this string to your NameValuePair using image as the key-name.

可以使用 HttpComponents 库 来发送图像.下载最新的HttpClient(目前4.0.1) 带有依赖包的二进制文件并复制 apache-mime4j-0.6.jarhttpmime-4.0.1.jar 到您的项目并将它们添加到您的 Java 构建路径.

Sending images can be done using the HttpComponents libraries. Download the latest HttpClient (currently 4.0.1) binary with dependencies package and copy apache-mime4j-0.6.jar and httpmime-4.0.1.jar to your project and add them to your Java build path.

您需要将以下导入添加到您的类中.

You will need to add the following imports to your class.

import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;

现在您可以创建一个 MultipartEntity 以将图像附加到您的 POST 请求.以下代码显示了如何执行此操作的示例:

Now you can create a MultipartEntity to attach an image to your POST request. The following code shows an example of how to do this:

public void post(String url, List<NameValuePair> nameValuePairs) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpPost httpPost = new HttpPost(url);

    try {
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        for(int index=0; index < nameValuePairs.size(); index++) {
            if(nameValuePairs.get(index).getName().equalsIgnoreCase("image")) {
                // If the key equals to "image", we use FileBody to transfer the data
                entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue())));
            } else {
                // Normal string data
                entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
            }
        }

        httpPost.setEntity(entity);

        HttpResponse response = httpClient.execute(httpPost, localContext);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我希望这可以帮助您朝着正确的方向前进.

I hope this helps you a bit in the right direction.

这篇关于使用 Http Post 发送图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 20:26