列表类型不是通用的;它不能用参数参数化 [HTTPClient]
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();
}
}
}
所以我有那个代码,我从使用Java https错误上传到Imgur v3中得到它,我在第50行得到一个错误,“列表”告诉我
列表类型不是通用的;它不能用参数参数化
我能做些什么来解决这个问题?
我正在使用 http://hc.apache.org/httpclient-3.x/,并希望使用他们的v3 API将图像上传到imgur。
编辑:更改导入后,我现在收到这些错误。
这解决了这个问题,但给我两个错误。
nameValuePairs.add(new BasicNameValuePair("image", dataImage));
类型 List 中的方法 add(NameValuePair) 不适用于参数 (BasicNameValuePair)
和
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
构造函数 UrlEncodedFormEntity(List) 未定义