2013-06-30 60 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(); 
    } 
} 
} 

所以我有一個代碼,我得到了它從uploading to Imgur v3 using Java https errors和我得到的第50行錯誤的「列表」告訴我類型列表不是通用的;它不能與參數進行參數[了HTTPClient]

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

我能做些什麼解決這個問題?

我正在使用http://hc.apache.org/httpclient-3.x/並希望使用他們的v3 API將圖像上傳到imgur。

編輯:改變導入後,我現在得到這些錯誤。

解決了這個問題,但給我兩個更多的錯誤。

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

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

而且

post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

The constructor UrlEncodedFormEntity(List) is undefined

回答

128

你的進口有一個微妙的錯誤:

import java.awt.List; 

它應該是:

import java.util.List; 

問題是awt和Java的util包都提供了一個名爲List的類。前者是顯示元素,後者是與集合一起使用的泛型類型。此外,java.util.ArrayList延伸java.util.List,而不是java.awt.List因此,如果它不是泛型,它將仍然是一個問題。

編輯:(以解決OP給出進一步的問題)作爲回答您的評論,似乎有花葯微妙的進口問題。

import org.omg.DynamicAny.NameValuePair; 

應該

import org.apache.http.NameValuePair 

nameValuePairs現在使用正確的泛型類型參數,new UrlEncodedFormEntity通用的說法,這是List<? extends NameValuePair>,變得有效,因爲的NameValuePair現在一樣 NameValuePair。之前,org.omg.DynamicAny.NameValuePair沒有延伸org.apache.http.NameValuePair,並且縮短的類型名稱NameValuePair在您的文件中評估爲org.omg...,但在其代碼中評估爲。

+0

我編輯我的答案現在... // @ user2526311它現在固定。 – hexafraction

+0

如何在運行程序時返回「null:null」,還是應該在新問題中發佈? – user2526311

+0

@ user2526311不要在開始時將其設置爲空,將其設置爲虛擬或空字符串。 – hexafraction

11

嘗試導入的

java.util.List; 

代替

java.awt.List; 
+5

@hexafraction我沒有看到你的答案,提出後我意識到你已經確定了它。我通常會回答寫作模式,讓彈出窗口有一個新的答案。你贏得了時間。 –

-1

我得到了同樣的錯誤,但是當我做了如下,它解決了這個問題。
而不是寫這樣的:

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

使用下面一個:

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

你們都使用了錯誤的導入。你需要'java.util.List'中'List'的接口。 – hofmeister

相關問題