Export to GitHub

imageshackapi - issue #40

Java connection refused


Posted on Jan 18, 2013 by Massive Dog

What steps will reproduce the problem? 1. run the code I have provided below

What is the expected output? What do you see instead? I expect a standard return from the ImageshackAPI instead I recieve that the connection have been refused. I presume this is due to me not knowing what type the file should be in when I upload.

Code:

public String ImageShack (String imageDir, String myKey) {

    //set file
    BufferedImage image = null;
    File file = new File(imageDir);

    //Set namevalue pairs
    List<NameValuePair> params = new ArrayList<NameValuePair>();

    try
    {
        image = ImageIO.read(file);
        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        ImageIO.write(image, "png", byteArray);
        byteArray.flush();
        //byte[] byteImage = byteArray.toByteArray();
        Base64 base = new Base64(false);
        String dataImage = base.encodeAsString(byteArray.toByteArray());
        byteArray.close();
        dataImage = java.net.URLEncoder.encode(dataImage, "ISO-8859-1");

        //Assign name valued pars
        params.add(new BasicNameValuePair("key", myKey));
        params.add(new BasicNameValuePair("fileupload", dataImage));
        params.add(new BasicNameValuePair("format", "json"));

        //Create HTTPClient and Client
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://www.imageshack.us/upload_api.php");

        //set entities
       httpPost.setEntity(new UrlEncodedFormEntity(params));

        //Execute & get response
        HttpResponse response = httpClient.execute(httpPost);
        return response.toString();

    }
    catch(Exception e)
    {
            return "Error: " + e.getMessage();
}
}

Status: New

Labels:
Type-Defect Priority-Medium