类型列表不是通用的;它不能与参数进行参数[了HTTPClient] [英] The type List is not generic; it cannot be parameterized with arguments [HTTPClient]
问题描述
进口java.awt.List中;
进口java.awt.image.BufferedImage中;
进口java.io.BufferedReader中;
进口java.io.ByteArrayOutputStream中;
进口的java.io.File;
进口java.io.InputStreamReader中;
进口的java.util.ArrayList;进口javax.imageio.ImageIO中;进口org.apache.commons codec.binary.Base64。
进口org.apache.http.Htt presponse;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.entity.UrlEn codedFormEntity;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.message.BasicNameValuePair;
进口org.omg.DynamicAny.NameValuePair;公共类上传{ 公共静态无效的主要(字串[] args){ 的System.out.println(Imgur(C:\\\\ \\\\用户名\\\\ \\\\桌面image.jpg文件,clientID的));
}公共静态字符串Imgur(IMAGEDIR字符串,字符串clientID的){
//创造必要的字符串
字符串的地址=https://api.imgur.com/3/image; //创建了HTTPClient和后
HttpClient的客户端=新DefaultHttpClient();
HttpPost后=新HttpPost(地址); //创建的base64形象
BufferedImage的图像= NULL;
档案文件=新的文件(IMAGEDIR); 尝试{
//读取图像
图像= ImageIO.read(文件);
ByteArrayOutputStream的字节数组=新ByteArrayOutputStream();
ImageIO.write(形象,PNG,字节);
字节[] = byteImage byteArray.toByteArray();
字符串dataImage =新的Base64()带codeAsString(byteImage)。 //添加标题
post.addHeader(授权,客户ID+ clientID的);
//添加图片
清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(1);
nameValuePairs.add(新BasicNameValuePair(形象,dataImage));
post.setEntity(新UrlEn codedFormEntity(namevaluepairs中)); //执行
HTT presponse响应= client.execute(岗位); //读取响应
RD的BufferedReader =新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent()));
字符串的所有= NULL; //通过响应循环
而(rd.readLine()!= NULL){
所有的一切= +:+ rd.readLine();
} 返回所有; }
赶上(例外五){
返回error:+ e.toString();
}
}
}
所以我有code和我是从<一个href=\"http://stackoverflow.com/questions/14710247/uploading-to-imgur-v3-using-java-https-errors\">uploading到Imgur V3使用Java HTTPS错误和我得到50行错误的列表告诉我,
类型列表不是通用的;它不能与自变量进行参数设置。
块引用>我能做些什么来解决这个问题?
我使用 http://hc.apache.org/httpclient-3.x/ 并要上传图片用他们的V3 API来imgur。
编辑:更改导入之后,我现在得到这些错误
。这是解决了,但给我的两个错误。
nameValuePairs.add(新BasicNameValuePair(形象,dataImage));
在类型列表方法Add(的NameValuePair)不适用于参数(BasicNameValuePair)
块引用>和
post.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
构造UrlEn codedFormEntity(列表)未定义
块引用>解决方案您输入有一个微妙的错误:
进口java.awt.List中;
块引用>它应该是:
进口的java.util.List;
问题是,无论
AWT
和Java的util包提供了一个名为类列表
。前者是一个显示单元,后者是集合使用泛型类型。此外,的java.util.ArrayList
扩展的java.util.List
的不的java.awt.List中的
,所以如果它不是为仿制药,它会仍然是一个问题。编辑:(适用于解决OP给出进一步的问题)作为回答您的评论,似乎还有的花药的微妙的进口问题。
进口org.omg.DynamicAny.NameValuePair;
应
进口org.apache.http.NameValuePair
namevaluepairs中
现在使用正确的泛型类型参数,为新UrlEn codedFormEntity
,通用参数,该参数为名单,LT ;?扩展的NameValuePair&GT;
,成为有效的,因为的您的的NameValuePair现在是一样的及其的的NameValuePair。之前,org.omg.DynamicAny.NameValuePair </ code>没有延伸
org.apache.http.NameValuePair </ code>和缩短的类型名称
的NameValuePair </ code>评估,以<code> org.omg ...
在你的文件,但org.apache。 ..
在他们的code。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
解决方案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 calledList
. The former is a display element, the latter is a generic type used with collections. Furthermore,java.util.ArrayList
extendsjava.util.List
, notjava.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 fornew UrlEncodedFormEntity
, which isList<? extends NameValuePair>
, becomes valid, since your NameValuePair is now the same as their NameValuePair. Before,org.omg.DynamicAny.NameValuePair
did not extendorg.apache.http.NameValuePair
and the shortened type nameNameValuePair
evaluated toorg.omg...
in your file, butorg.apache...
in their code.这篇关于类型列表不是通用的;它不能与参数进行参数[了HTTPClient]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!