2013-06-30 10 views
41
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(); 
    } 
} 
} 

Così ho quel codice e l'ho presa da uploading to Imgur v3 using Java https errors ed ottengo un errore sulla linea 50 per "List" dicendomiL'Elenco tipi non è generico; esso non può essere parametrizzato con argomenti [HTTPClient]

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

Cosa posso fare per risolvere questo?

Sto usando http://hc.apache.org/httpclient-3.x/ e voglio caricare un'immagine su imgur usando la loro API v3.

MODIFICA: dopo aver modificato l'importazione, ora ricevo questi errori.

Ciò risolve questo, ma mi dà altri due errori.

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

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

E

post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

The constructor UrlEncodedFormEntity(List) is undefined

risposta

128

tuo importazione ha un errore sottile:

import java.awt.List; 

dovrebbe essere:

import java.util.List; 

Il problema è che sia il pacchetto awt che Java forniscono una classe denominata List. Il primo è un elemento di visualizzazione, il secondo è un tipo generico utilizzato con le raccolte. Inoltre, java.util.ArrayList estende java.util.List, nonjava.awt.List quindi, se non fosse per i generici, sarebbe stato comunque un problema.

Edit: (per affrontare ulteriori domande fornite dal PO) come una risposta al tuo commento, sembra che ci sia antere sottile problema di importazione.

import org.omg.DynamicAny.NameValuePair; 

dovrebbe essere

import org.apache.http.NameValuePair 

nameValuePairs ora utilizza il corretto parametro di tipo generico, l'argomento generico per new UrlEncodedFormEntity, che è List<? extends NameValuePair>, diventa valido, dal momento che tua NameValuePair è ora lo stesso di loro NameValuePair. Prima, org.omg.DynamicAny.NameValuePair non si estendeva org.apache.http.NameValuePair e il nome del tipo abbreviato NameValuePair valutato su org.omg... nel file, ma org.apache... nel codice.

+0

sto modificando la mia risposta in questo momento ... @ user2526311 E 'fissato ora. – hexafraction

+0

Come mai quando eseguo il programma restituisce "null: null" o dovrei postarlo in una nuova domanda? – user2526311

+0

@ user2526311 Non impostarlo come null all'inizio, impostarlo su una stringa fittizia o vuota. – hexafraction

11

tenta di importare

java.util.List; 

invece di

java.awt.List; 
+5

@hexafraction Non ho visto la tua risposta, dopo averla postata ho capito che l'hai già identificata. In genere vado in modalità di scrittura risposta e ricevo popup c'è una nuova risposta. Hai vinto nel tempo. –

-1

ho ottenuto lo stesso errore, ma quando l'ho fatto, come di seguito, che ha risolto il problema.
Invece di scrivere in questo modo:

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

utilizzare il sotto di un:

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
+0

Entrambi avete usato un'importazione errata. È necessaria l'interfaccia per 'List' da' java.util.List'. – hofmeister

Problemi correlati