Upload the file from Android to the Django web service

I am working on an Android application that interacts with the Django backend. I deployed webservice using mod_wsgi and I have many web service calls that work and have been tested, so I know that ALL SHOULD work. All other calls work fine. This is the method that I call in Android to send the photo.

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"; } } 

This is the method that I use in the views.py file to get and process the image:

 @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}) 

When I try to use this method, I get a really useful Apache error 500 Internal Server Error, and I donโ€™t know what is happening. I know this method works because I can use a custom html page that I wrote to get into a web service without Android, and it works fine:

 <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> 

So, I think that something is wrong with the way I format the call on the Android side. I would provide an error log or stack, but there are none. I look at my apache error log and nothing appears. Anyone have any tips?

EDIT : So I created django logging and was able to debug my boot method when I hit it from the phone. It seems to stop working when I say server_id = request.POST ['server_id'], so somehow this piece of data is not set correctly when I make a request in the uploadFile method. Does anyone see me doing the wrong request?

+6
source share
1 answer

I realized what I'm doing wrong. After setting up django registration, I was able to see where it failed. It crashes when I tried to get the variable "server_id". I ended up adding this variable to the multi-part entity as the body of the string, and not setting it as the header.

+2
source

Source: https://habr.com/ru/post/903455/


All Articles