59
import java.awt.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;

public class Upload {

    public static void main (String[] args) {

        System.out.println(Imgur("C:\\Users\\username\\Desktop\\image.jpg",     "clientID"));
    }

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<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(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();
    }
}
}

So I have that code and I got it from uploading to Imgur v3 using Java https errors and I get an error on line 50 for "List" telling me

The type List is not generic; it cannot be parameterized with arguments

What can I do to solve this?

I'm using http://hc.apache.org/httpclient-3.x/ and want to upload an image to imgur using their v3 API.

EDIT: After changing the import I now get these errors.

That solves that but give me two more errors.

nameValuePairs.add(new BasicNameValuePair("image", dataImage));

The method add(NameValuePair) in the type List is not applicable for the arguments (BasicNameValuePair)

And

post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

The constructor UrlEncodedFormEntity(List) is undefined

Community
  • 1
  • 1
user2526311
  • 992
  • 2
  • 9
  • 19

4 Answers4

204

Your import has a subtle error:

import java.awt.List;

It should be:

import java.util.List;

The problem is that both awt and Java's util package provide a class called List. The former is a display element, the latter is a generic type used with collections. Furthermore, java.util.ArrayList extends java.util.List, not java.awt.List so if it wasn't for the generics, it would have still been a problem.

Edit: (to address further questions given by OP) As an answer to your comment, it seems that there is anther subtle import issue.

import org.omg.DynamicAny.NameValuePair;

should be

import org.apache.http.NameValuePair

nameValuePairs now uses the correct generic type parameter, the generic argument for new UrlEncodedFormEntity, which is List<? extends NameValuePair>, becomes valid, since your NameValuePair is now the same as their NameValuePair. Before, org.omg.DynamicAny.NameValuePair did not extend org.apache.http.NameValuePair and the shortened type name NameValuePair evaluated to org.omg... in your file, but org.apache... in their code.

nanofarad
  • 36,174
  • 4
  • 79
  • 104
  • How come when I run the program it returns "null : null" or should I post this in a new question? – user2526311 Jun 30 '13 at 00:58
  • @user2526311 Don't set it as null in the beginning, Set it to a dummy or empty string. – nanofarad Jun 30 '13 at 00:59
  • Changed it to 'String all = "";' and it gave me " : null" as a return. – user2526311 Jun 30 '13 at 01:01
  • You seem to have some issue with the buffered reader and the request-respondse. I'd ask a new question. – nanofarad Jun 30 '13 at 01:07
  • Wow, Great but why eclipse imports `java.awt.List` not `java.utils.List`? –  Jul 26 '16 at 06:25
  • @ehsan First, it's `java.util.List`, not `utils`. Second, the reason for this is because `java.awt.List` is also an available class, just used for something else. – nanofarad Jul 26 '16 at 11:32
  • Thanks that fix my issue ,but may I know why Eclips has imported the wrong library ? is there a way to avoid that ? – Osama Al-Banna Feb 07 '17 at 18:26
  • @OsamaAl-Banna Because `java.awt.List` *is* a valid library, that has other uses. Eclipse can't psychically predict which you want. – nanofarad Feb 07 '17 at 22:12
  • I had a similar error - The type List is not generic; it cannot be parameterized with arguments . But this totally fixed my code. Thank you! – Combine Dec 21 '19 at 14:33
16

Try to import

java.util.List;

instead of

java.awt.List;
Juned Ahsan
  • 63,914
  • 9
  • 87
  • 123
1

Adding java.util.list will resolve your problem because List interface which you are trying to use is part of java.util.list package.

Sumit Sood
  • 371
  • 5
  • 15
0

I got the same error, but when i did as below, it resolved the issue.
Instead of writing like this:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);

use the below one:

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
סטנלי גרונן
  • 2,740
  • 21
  • 43
  • 62
Kotes
  • 11