List 类型不是通用的;它不能用参数 [HTTPClient] 参数化 [英] The type List is not generic; it cannot be parameterized with arguments [HTTPClient]

查看:50
本文介绍了List 类型不是通用的;它不能用参数 [HTTPClient] 参数化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import 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.HttpResponse;导入 org.apache.http.client.HttpClient;导入 org.apache.http.client.entity.UrlEncodedFormEntity;导入 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:\\Users\\username\\Desktop\\image.jpg", "clientID"));}public static String Imgur (String imageDir, String clientID) {//创建需要的字符串字符串地址 = "https://api.imgur.com/3/image";//创建HTTPClient并发布HttpClient 客户端 = 新的 DefaultHttpClient();HttpPost post = new HttpPost(address);//创建base64图像BufferedImage 图像 = null;File file = new File(imageDir);尝试 {//读取图像图像 = ImageIO.read(file);ByteArrayOutputStream byteArray = new ByteArrayOutputStream();ImageIO.write(image, "png", byteArray);byte[] byteImage = byteArray.toByteArray();String dataImage = new Base64().encodeAsString(byteImage);//添加标题post.addHeader("Authorization", "Client-ID" + clientID);//添加图片列表nameValuePairs = new ArrayList(1);nameValuePairs.add(new BasicNameValuePair("image", dataImage));post.setEntity(new UrlEncodedFormEntity(nameValuePairs));//执行HttpResponse 响应 = client.execute(post);//读取响应BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));字符串全部 = 空;//循环响应while (rd.readLine() != null) {all = all + ":" + rd.readLine();}全部归还;}捕获(例外 e){返回错误:"+ e.toString();}}}

所以我有那个代码,我从 上传到 Imgur 得到它v3 使用 Java https 错误,我在第 50 行收到一个错误,列表"告诉我

<块引用>

List 类型不是通用的;它不能用参数参数化

我该怎么做才能解决这个问题?

我正在使用 http://hc.apache.org/httpclient-3.x//a> 并且想要使用他们的 v3 API 将图像上传到 imgur.

更改导入后,我现在收到这些错误.

这解决了这个问题,但又给了我两个错误.

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

<块引用>

类型List中的add(NameValuePair)方法不适用于参数(BasicNameValuePair)

post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

<块引用>

构造函数 UrlEncodedFormEntity(List) 未定义

解决方案

您的导入有一个细微的错误:

<块引用>

import java.awt.List;

应该是:

import java.util.List;

问题在于awt 和Java 的util 包都提供了一个名为List 的类.前者是显示元素,后者是与集合一起使用的泛型类型.此外,java.util.ArrayList 扩展了 java.util.Listnot java.awt.List 所以如果不是为了泛型,它仍然是一个问题.

(解决 OP 提出的进一步问题)作为对您评论的回答,似乎存在花药 微妙的导入问题.

import org.omg.DynamicAny.NameValuePair;

应该是

import org.apache.http.NameValuePair

nameValuePairs 现在使用正确的泛型类型参数,即 new UrlEncodedFormEntity 的泛型参数,即 List,变得有效,因为您的 NameValuePair 现在与他们的 NameValuePair 相同.之前,org.omg.DynamicAny.NameValuePair 没有扩展 org.apache.http.NameValuePair 并且缩短的类型名称 NameValuePair 评估为 org.omg... 在你的文件中,但 org.apache... 在他们的代码中.

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 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.

这篇关于List 类型不是通用的;它不能用参数 [HTTPClient] 参数化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆