使用Android将文件上载到Django web服务

使用Android将文件上载到Django web服务,android,django,file-upload,Android,Django,File Upload,我正在开发一个Android应用程序,它与Django后端接口。我已经使用mod_wsgi部署了web服务,并且有许多web服务调用可以工作并且已经过测试,所以我知道一切都应该工作。其他电话都很好。这是我在Android中调用的发送照片的方法 public static String uploadFile(String url, int server_id, String filepath) { try { client.getParams().setParameter(

我正在开发一个Android应用程序,它与Django后端接口。我已经使用mod_wsgi部署了web服务,并且有许多web服务调用可以工作并且已经过测试,所以我知道一切都应该工作。其他电话都很好。这是我在Android中调用的发送照片的方法

public static String uploadFile(String url, int server_id, String filepath) {
    try {
        client.getParams().setParameter("http.socket.timeout", 90000); // 90 second
        HttpPost post = new HttpPost(url);

        MultipartEntity mpEntity = new MultipartEntity();
        mpEntity.addPart("image", new FileBody(new File(filepath), "image/jpeg"));
        post.setEntity(mpEntity);
        post.addHeader("server_id", String.valueOf(server_id));

        HttpResponse response = Connector.client.execute(post);
        if (response.getStatusLine().getStatusCode() != 200) { return "false"; }
        return convertStreamToString(response.getEntity().getContent());
    } catch (Exception e) {
        if (Constants.DEBUG) e.printStackTrace();
        return "false";
    }
}
这是我在views.py文件中用于接收和处理图像的方法:

@csrf_exempt
def incident_upload(request):
    if request.method == 'POST':
            server_id = request.POST['server_id']
            incident = get_object_or_404(Incident, pk=server_id)
            imagePath = '/Library/WebServer/program/uploads/' + str(int(time.time() * 1000)) + '.jpg'
            destination = open(imagePath, 'wb+')
            for chunk in request.FILES['image'].chunks():
                    destination.write(chunk)
            destination.close()
            incident.ImagePath = imagePath
            incident.save()
            return render_to_response('webservice/incident_read.html', {'incident': incident, 'device': incident.device})
当我尝试这种方法时,我得到了非常有用的apacheerror500内部服务器错误,我不知道发生了什么。我知道这种方法是有效的,因为我可以使用我编写的自定义html页面在没有Android的情况下访问web服务,而且效果很好:

<html>
<body>
<form action="http://xxxxxxxxxxx.local/hermes/incident/upload/" enctype="multipart/form-data" method="post">
server_id: <input type="text" name="server_id" />
<br />
file: <input type="file" name="image" />
<br />
<input type="submit" value="send" />
</form>
</body>
</html>

服务器id:

文件:
所以我认为我在安卓端设置通话格式肯定有问题。我会提供错误日志或堆栈跟踪,但没有。我正在查看我的apache错误日志,但没有显示任何内容。有人有什么建议吗

编辑
因此,我设置了django日志记录,并且能够在我从电话中点击上传方法时调试它。当我说server\u id=request.POST['server\u id']时,它似乎停止了工作,因此,不知何故,当我在uploadFile方法中发出请求时,该数据段设置不正确。有人知道我是如何错误地执行请求的吗?

我知道我做错了什么。在设置django日志之后,我能够看到它在哪里崩溃。当我试图检索“server_id”变量时,它崩溃了。我最终将该变量作为字符串体添加到多部分实体中,而不是将其设置为头。

Apache错误日志怎么说?什么都没有!哈哈,一点用都没有!当我设置mod_wsgi时,它有一些错误,我可以找到并修复,但是没有出现任何错误。我在想,问题可能是你的内容类型标题,但你正在发送表单。是的,我真的不知道如何修复它。正如我所说的,令人沮丧的是我没有任何错误。