package fr.free.nrw.commons.upload;


import android.os.AsyncTask;
import android.util.Log;

import org.mediawiki.api.ApiResult;
import org.mediawiki.api.MWApi;

import java.io.IOException;
import java.util.ArrayList;

import fr.free.nrw.commons.CommonsApplication;

/**
 * Sends asynchronous queries to the Commons MediaWiki API to check that file doesn't already exist
 * Returns true if file exists, false if it doesn't
 */
public class ExistingFileAsync extends AsyncTask<Void, Void, Boolean> {

    private static final String TAG = fr.free.nrw.commons.upload.ExistingFileAsync.class.getName();
    private String fileSHA1;

    public ExistingFileAsync(String fileSHA1) {
        super();
        this.fileSHA1 = fileSHA1;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Boolean doInBackground(Void... voids) {

        MWApi api = CommonsApplication.createMWApi();
        ApiResult result;

        // https://commons.wikimedia.org/w/api.php?action=query&list=allimages&format=xml&aisha1=801957214aba50cb63bb6eb1b0effa50188900ba
        try {
            result = api.action("query")
                    .param("format", "xml")
                    .param("list", "allimages")
                    .param("aisha1", fileSHA1)
                    .get();
            Log.d(TAG, "Searching Commons API for existing file: " + result.toString());
        } catch (IOException e) {
            Log.e(TAG, "IO Exception: ", e);
            return false;
        }

        ArrayList<ApiResult> resultNodes = result.getNodes("/api/query/allimages/");
        boolean fileExists = resultNodes!=null;
        Log.d(TAG, "File already exists in Commons:" + fileExists);

        return fileExists;
    }
}
@@ -1616,16 +1616,70 @@
 ges/%22);%0A
+%0A        Log.d(TAG, %22Result nodes: %22 + resultNodes);%0A%0A
         
@@ -1696,19 +1696,30 @@
 leExists
- = 
+;%0A        if (
 resultNo
@@ -1727,17 +1727,201 @@
 es!=null
-;
+) %7B%0A            fileExists = true;%0A        %7D else %7B%0A            fileExists = false;%0A        %7D%0A        %0A        //FIXME: This always returns false even when file (and nodes) exists, why?
 %0A       

Add checks for result Nodes and fileExists