digitalsteez
digitalsteez

Reputation: 355

Cannot get HttpClient to execute Imgur API v3 post request

I am trying to build an app which uploads anonymously to Imgur API v3, and this seems to cause a NullPointerException. I do have the Internet permission added to my manifest so I'm not sure why this isn't working. The client-id is removed for obvious reasons. Any suggestions would be greatly appreciated.

public HttpResponse uploadImage(String image_contents) {
    /* defining imgur api location */
    String API = ("https://api.imgur.com/3/image.json");
    Uri imgurAPI = (Uri.parse(API));

    // url encoding for bitmap

    // String dev_key = ("anon-dev-key-removed");
    String client_id = ("client-id-removed");
    /* String client_secret = ("secret-id-removed"); */
    /* constructing query */
    // spawning apache httpclient and post instance
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(API);
    // defining namevaluepairs for post data and setting entity
    // httpclient help from this url:
    // http://www.vogella.com/articles/ApacheHttpClient/article.html
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("image", image_contents));
    /* for debugging purposes */
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    /* end for debugging purposes */
    try {
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // adding oauth2 header with clientid
    httppost.addHeader("Authorization", "Client-ID " + client_id);
    // execute the post and collect the response as a httpresponse object

    HttpResponse response = null;
    changeText(httppost.toString());
    try {
        response = httpclient.execute(httppost);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    /* shut down http client */
    httpclient.getConnectionManager().shutdown();

    return response;
}

uploadImage() doesn't crash, but when I try to handle the HttpResponse I get the NullPointerException:

public void provideURL(HttpResponse response) {
    int responseCode=response.getStatusLine().getStatusCode();
    changeText("test");
}

Upvotes: 2

Views: 636

Answers (1)

Eggman87
Eggman87

Reputation: 571

It may have changed recently... but to use the Apache HTTP client with imgur 3.0 API (at least when I coded for it). You will have to add some special SSL handling code for your HTTP client. If you don't, your requests should fail with:

 // io exception with the message: (hostname in certificate didn't match: <api.imgur.com>    != <imgur.com> OR <imgur.com>)

You can see what exactly you need to do here (where I explained the issue to someone else): https://groups.google.com/forum/#!topic/imgur/RQytzya5aa4

Are you seeing any exceptions at all when making the request (in your catch blocks)? If not, maybe the SSL configuration is no longer a issue.

Upvotes: 1

Related Questions