问题描述
进口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和我是从uploading到Imgur V3使用Java HTTPS错误和我得到50行错误的列表告诉我,
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));
And
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Your import has a subtle error:
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.
这篇关于类型列表不是通用的;它不能与参数进行参数[了HTTPClient]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!