Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot get HttpClient to execute Imgur API v3 post request
    primarykey
    data
    text
    <p>I am trying to build an app which uploads anonymously to Imgur API v3, and this seems to cause a <code>NullPointerException</code>. I do have the Internet permission added to my manifest so I'm not sure why this isn't working. The <code>client-id</code> is removed for obvious reasons. Any suggestions would be greatly appreciated.</p> <pre><code>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&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(1); nameValuePairs.add(new BasicNameValuePair("image", image_contents)); /* for debugging purposes */ if (android.os.Build.VERSION.SDK_INT &gt; 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; } </code></pre> <p><code>uploadImage()</code> doesn't crash, but when I try to handle the <code>HttpResponse</code> I get the <code>NullPointerException</code>:</p> <pre><code>public void provideURL(HttpResponse response) { int responseCode=response.getStatusLine().getStatusCode(); changeText("test"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload