Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting a null return from BufferedReader
    primarykey
    data
    text
    <p>So I'm trying to upload an image to Imgur using their v3 API (<a href="http://api.imgur.com/" rel="nofollow">http://api.imgur.com/</a>) and when I get the response it returns me with </p> <pre><code>null : null </code></pre> <p>Here is my full code.</p> <pre><code>import java.util.List; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.InputStreamReader; import java.util.ArrayList; import javax.imageio.ImageIO; import org.apache.commons.codec.binary.Base64; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; //import org.omg.DynamicAny.NameValuePair; import org.apache.http.NameValuePair; public class Upload { public static void main (String[] args) { System.out.println(Imgur("C:\\Users\\wiesa\\Desktop\\image.jpg", "2313fab45c25337")); } public static String Imgur (String imageDir, String clientID) { //create needed strings String address = "https://api.imgur.com/3/image"; //Create HTTPClient and post HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(address); //create base64 image BufferedImage image = null; File file = new File(imageDir); try { //read image image = ImageIO.read(file); ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); ImageIO.write(image, "png", byteArray); byte[] byteImage = byteArray.toByteArray(); String dataImage = new Base64().encodeAsString(byteImage); //add header post.addHeader("Authorization", "Client-ID" + clientID); //add image List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(1); nameValuePairs.add(new BasicNameValuePair("image", dataImage)); post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); //execute HttpResponse response = client.execute(post); //read response BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String all = null; //loop through response while (rd.readLine() != null) { all = all + " : " + rd.readLine(); } return all; } catch (Exception e){ return "error: " + e.toString(); } } } </code></pre> <p>Depending what I initilize the string "all" to if I make it a</p> <pre><code>null </code></pre> <p>Then the return is</p> <blockquote> <p>null : null</p> </blockquote> <p>BUt if I initialize it to </p> <pre><code>"" </code></pre> <p>Then the return is</p> <blockquote> <p>: null</p> </blockquote> <p>I Changed the line to </p> <pre><code>String all = null; //loop through response String line = null; while ((line = rd.readLine()) != null) { all = all + " : " + line; } return all; </code></pre> <p>and the I get the return</p> <blockquote> <p>null : {"data":{"error":"Malformed auth header","request":"/3/image","parameters":"image = iVBORw0KGgoAAAANSUhEUgAAB4AAAASwCAIAAACVUsChAACAAElEQVR42uzdCXebyrI2YEuyY8fzPCbZOyfZd597v///...","method":"POST"},"success":false,"status":403}</p> </blockquote>
    singulars
    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.
    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