[Solved]-Android-Django Image upload

3👍

Should be urlString = "http://192.168.1.2/photos/upload";

But then if it doesn’t work, you’ll get a different error, and we’ll probably need that error to answer further.

Also, it looks like you don’t have a real boundary string set and you aren’t using it correctly.

Have a look here. Notice how he uses a unique boundary string and writes it to the output stream?

👤synic

1👍

Even after you overcome these difficulties, you’ll run afoul of Django’s requirement that you supply a csrfmiddlewaretoken with your POST parameters. And I don’t see how you can obtain that on an Android device; by design that token is there to prevent calling Django backend code (i.e. a “view”) from anything other than a Django frontend (i.e. a “template”). I.e. it’s designed to thwart doing exactly what you’re trying to do.

You could disable the csrf feature on a particular view — use the “@csrf_exempt” decorator. And then you can decide if you care enough about security, to work out a substitute of your own for what the csrf thing gives you.

Or, instead of uploading a picture from an Android app, write a web app to upload a picture, and have your Django project serve up that web app. Your Android app could launch the browser (as an Intent) and point it at that web app. Here’s some code that’ll do that: https://simpleisbetterthancomplex.com/tutorial/2016/08/01/how-to-upload-files-with-django.html (It won’t win any beauty contests, but it does work.)

Leave a comment