FileNotFoundException for imgur URL (Android app)

329 views Asked by At

I'm trying to write code that uploads a photo taken from my Google Glass to imgur and gets the URL. Strangely, I'm getting a FileNotFoundException for the imgur URL. What could I be doing wrong. This is a mishmash of code I've borrowed from elsewhere, but just can't seem to get it working... I've also tried it with the URL https://api.imgur.com/3/image

The image file is currently located at /storage/emulated/0/DCIM/Camera/20141123_120731_953.jpg

public class UploadImgur extends AsyncTask<String, Integer, String> {

    private static final String imgurURL = "https://api.imgur.com/3/";
    private static final String clientID = "*****"; // filled in in my code
    private static final String clientSecret = "*****"; // filled in in my code
    private static final String TAG = "UploadImgur";


    @Override
    protected String doInBackground(String... sourceFileURL) {

        Log.i(TAG, "Now establishing a connection online...");
        String fileName = sourceFileURL[0];

try {
    URL url;
    url = new URL(imgurURL);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    String data = URLEncoder.encode("image", "UTF-8") + "="
            + URLEncoder.encode(fileName, "UTF-8");

    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization", "Client-ID " + clientID);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded");

    Log.i(TAG, "Connection Open");

    conn.connect();
    StringBuilder stb = new StringBuilder();
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    Log.i(TAG, "Data written");
    wr.flush();

    // Get the response
    BufferedReader rd = new BufferedReader(
            new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        stb.append(line).append("\n");
    }
    Log.i(TAG, "Response Received");
    wr.close();
    rd.close();

    resultURL = stb.toString();
} catch(MalformedURLException me) {
    Log.e(TAG, "Malformed URL " + imgurURL);
} catch(UnsupportedEncodingException uee) {
    Log.e(TAG, "UnsupportedEncoding Exception: UTF-8");
} catch(IOException ioe){
    ioe.printStackTrace();
    Log.e(TAG, ioe.toString());
}
    return resultURL;
    }


    protected void onPostExecute(String result) {
        Log.i(TAG,result);
        Log.i(TAG,resultURL);
    }
}

The error:

11-23 12:07:40.465  20090-20342/com.example.cerveau.recognizeplaces I/UploadImgur﹕ Connection Open
11-23 12:07:40.700  20090-20342/com.example.cerveau.recognizeplaces D/dalvikvm﹕ GC_FOR_ALLOC freed 334K, 7% free 5272K/5644K, paused 29ms, total 29ms
11-23 12:07:40.762  20090-20342/com.example.cerveau.recognizeplaces I/UploadImgur﹕ Data written
11-23 12:07:40.801  20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ java.io.FileNotFoundException: https://api.imgur.com/3/
11-23 12:07:40.801  20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
11-23 12:07:40.801  20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:246)
11-23 12:07:40.801  20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at com.example.cerveau.recognizeplaces.UploadImgur.doInBackground(UploadImgur.java:61)
11-23 12:07:40.801  20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at com.example.cerveau.recognizeplaces.UploadImgur.doInBackground(UploadImgur.java:19)
11-23 12:07:40.801  20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:302)
11-23 12:07:40.801  20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-23 12:07:40.801  20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:240)
11-23 12:07:40.801  20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-23 12:07:40.801  20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-23 12:07:40.801  20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
11-23 12:07:40.801  20090-20342/com.example.cerveau.recognizeplaces E/UploadImgur﹕ java.io.FileNotFoundException: https://api.imgur.com/3/
0

There are 0 answers