Android POST Images (Files) to Django Server

Hi Guys,

I was working on an app where we have to send images taken from App to Server (Django). I initially thought it would be starightforward sending images with HTTPUrlConnection.  Later realized that it’s one hell of a job.

One of my friend suggested to check codepath. Its has a very good collection of libraries. After browsing for some time, I decided to use retofit (v2.0.1) and okhttp(v3.2) both developed by Square Inc. people. Below are the gradle imports-

compile ''
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.okhttp3:okhttp:3.2.0'

I’m little bit impatient now so I’m pasting the code with little explanation. Here’s how you initialize Retrofit object and create and APIInterface –

retrofit = new Retrofit.Builder()

apiService = retrofit.create(ApiEndpointInterface.class);
public interface ApiEndpointInterface {
 Call<ResponseBody> postSnapshots(@Part("description") String description, @PartMap Map<String, RequestBody> image);

Below is sample code to send multiple send files (Images in my case) –

File file;
Map<String, RequestBody> imgFiles = new HashMap<String,RequestBody>();
for(String imgName: imgFilePaths.keySet()){
    file = new File(imgFilePaths.get(imgName));
    imgFiles.put("file\"; filename=\""+imgName,RequestBody.create(MEDIA_TYPE_JPG, file));

Call<ResponseBody> call = getApiService().postSnapshots("DummyValue", imgFiles);
call.enqueue(new Callback<ResponseBody>() {
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        Log.d("PostSnapResponse", response.message());
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        Log.d("PostSnapResponse", t.getMessage());
return null;

There is a small catch here – Actually a small hack was needed to send images properly for Django server to identify files in the request and provide in request.FILES to access (People are building very actively, soon it could be better).  Instead of giving form field name, cleverly, field name along with filename attribute is being given. I took the code from Stack Overflow posts and Github issue page of retofit(luckily I found the issue quick, thanks to Google search, Stack Overflow and contributors of Retrofit)  😛

Below is the server side code-

class SaveSnapshots(APIView):
    def handlefileupload(self, ufiles):
        for ufile in ufiles:
            with open("images/", 'wb+') as destination:
                for chunk in ufile.chunks():

    def post(self,request, *args, **kwargs):
        return Response({"status":"success"})

Google SpreadSheet Date problem – swapping day and month


Recently I had to work with data entry guys and did some data entry myself too 🙂 One problem that I noticed was date format was MM-DD-YYYY in spreadsheet. But, when we write in normal excel sheets, its DD-MM-YYYY. Pasting data created inconsistency. After few times, I tried to find a solution for this. It was pretty simple setting.

All you need to do is open Spreadsheet Settings in your File menu of Google SpreadSheet. Select “United Kingdom” locale instead of “United States”.

Problem was, in US locale, date format is MM-DD-YYYY while for UK DD-MM-YYYY which is pretty much standard for most of the world. Small issue but ruined my day 😛


Android Studio – Git ignore files

Hi Folks,

This post is for people who are working on Android Studio Project in conjunction with Git.

I used to develop Android Apps in eclipse initially. I saw many people using Android Studio in stack overflow posts. Even my friend(Abhinav) has suggested to try Android Studio. I too got habituated using Git for all the projects. I consider Git is a breakthrough solving numerous problems. While learning to work on Android Studio, one main problem which I faced was setting the files to be tracked by git. So, below is the .gitignore file which suited me best.


I do understand that if above files are are ignored, its not possible for someone to clone the project and get started easily. In such cases, compress it and just send the project or create another repository.

If above mentioned files are not ignored, each time you make changes to remote repo, your team-mates need to struggle to get the updated project working.

App Stores – Difficulties


As far as I can remember everything was straight forward for Android and documentation was very helpful. Android studio is rich in terms of features but still not completely evolved, often reports bugs, freezes. One major problem(Android not to be blamed here though, just the file structure) that I struggled with was getting the .gitignore file right. Its a common practice to use git(Version control) for all projects.  Click here to checkout my other post about .gitignore file for android studio project.

To sign the apps, you need to create a keystore file and then use the password. There is an option in Android Studio – “Generate Signed APK” under build menu item to sign the apps. Generated Release Apk  file will be at this location –  ProjectRoot/app/app-release.apk . As soon as you upload your app, it gets validated and goes live in a day.

There is a scope to improve the supported devices functionality. Right now when you upload your app, it shows a list of devices supported by your app (excluded devices too). It would be great if we can know exactly which permission in Android Manifest is making the devices unsupported. We need to compile multiple times and check to support maximum devices.


Register for Developer Program, add the credentials to Xcode. Best thing was, we can run app validation tests locally and then upload App file to App store with just a button click. You need to fill in the App details and submit for review in Itunesconnect App Store.

Everything was smooth with IOS too. Only problem was the time taken to verify apps. I read in other blogs that avg. time is around 8 working days. There is a provision to speed up this process to 2 working days, called as Expedited reviews. As one can expect, it is like a lifeline, granted to people only upon checking reasons and that too only once in a while.


I really can’t describe the trouble I’ve been through just to complete the payment process. And yeah, till date i’ve tried around 15-20 times to pay using 7-8 different cards and 2 different live accounts but still could not get through. All I see is an error message which does not even indicate exact problem. There is a “contact us” link along with error message which is broken. I can only say that this is an epic failure of Microsoft. They are not in a state to receive money when people are ready to pay (for Developer Account).

UPDATE:  After several desperate days.. one day I got an email from Microsoft team with a coupon.  They have smoothened my pain a bit with that free coupon for developer account. It was easy to upload app and submit from there on. Only other problem was, compiling for different versions of windows 😐

I’ve initially compiled my app into .xap format for windows 8 devices. Later I realized that to support windows phone 8.1+ devices, I need to compile into .appx format and for this I need a windows 8.1+ device (I was like, holy ****).


Signing the app is not so easy but with some attention, we can get it done. You need to request signing keys and place it in specific location as described in documentation. “authout.p12, bbidtoken.csk” are the two file you would be needing to sign the application.

Real mess started only after this step. When I tried to fill up the details in vendor portal, I realized that, it is one of the lame portals I’ve ever seen. Content Rating popup is not working, there are 4/5 form submission need to be made which are always resulting in errors. Event the error messages are not understandable sometimes. I’ve been struggling to get it done.


No wonder why some companies are not even building windows and blackberry applications. Applications are like pillars to every operating system. If the process is so tough, how long can they sustain.

Cordova Whitelist Cross-domain request issue

Hi Folks,

As I mentioned somewhere in my previous posts,  installing whitelist plugin for cordova(5.0+) platforms in now compulsary to get server call working. In config.xml file add below line-

<access origin=”*” subdomains=”true” />

Above will ensure ajax requests happen properly on Android, IOS and Windows phones. But Blackberry10, it needs one more fix. Without adding below line, I was able to make a server call but only OPTIONS request was being received. As soon as the response is received, blackberry browser is blocking the server calls treating it as Cross-Domain request.

<preference name=”websecurity” value=”disable” />

You need to disable websecurity by adding above line to your config.xml file to get it working for Blackerry10. I’ve seen few posts mentioning this should not be used in production but as of now, I could not find any alternative. I’ll update this post shortly if I find anything better.

Cookie Manager – Android

Hi folks,

For an android application I was working on, I was using session authentication. For that we need cookies to work. In order to fix that, I was storing Set-cookie headers from server responses and adding cookies to each request.

Everything is working fine. Later I realized that I need to extend session expiry time for App Users as it won’t be good UX to ask App users to login every month.(Most of them would not even care to remember their passwords). So I considered to extend user session expiry time when they make specific request to server. I’ve changed expiry date on server side upon user making a specific request(which will happen automatically as soon as the users opens the App). I’ve tested in on website and everything was very much fine.

Here comes the important matter, After few days I was seeing many people getting logged out. After tracing each step, I realized that, CookieManager will store expiry date too and when accessed, getCookies method will give only cookies which did’nt expire.  Finally I saved all cookies and the problem got fixed.

Take aways : No matter how small things you are working on .. each time you make some change to production level code, you need to test the complete feature from end to end just exactly how an end user will see it. I have many times overlooked this fact and found myself in embarrassing situations. I agree that sometimes we need to simulate and create environments to test properly but trust me, its totally worth it.

Custom Popup: Strange bug or not so strange

Problem: Select element not responding to click events

When I was working on crossplatform mobile application using Cordova, I was trying to create a popup element. Below is the CSS and HTML markup



    position: absolute;
    top: 40%;

    border:1px solid #CCC;
    padding:20px 5px 20px 5px;
   border:1px solid #FFF;

   margin-left: auto;
   margin-right: auto;

    border-style: solid;
    border-width: 0px 0px 1px 0px;
    border-color: #CCC;



Continue reading “Custom Popup: Strange bug or not so strange”