java 如何制作 org.apache.http.Header 的数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3455099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:56:15  来源:igfitidea点击:

How make an array of org.apache.http.Header?

javaandroidgenerics

提问by user383343

I'm new in Java.

我是 Java 新手。

I'm trying to do

我正在尝试做

import org.apache.http.Header;
Header<NameValuePair> nvps = new HeaderList<NameValuePair>();
//....adding some headers
httppost.setHeaders(nvps);

but said

但说

The type Header is not generic; it cannot be parameterized with arguments <NameValuePair>

how I can do it?

我该怎么做?

回答by user383343

I found the answer

我找到了答案

Header[] headers = {
    new BasicHeader("Content-type", "application/x-www-form-urlencoded")
    ,new BasicHeader("Content-type", "application/x-www-form-urlencoded")
    ,new BasicHeader("Accep", "text/html,text/xml,application/xml")
    ,new BasicHeader("Connection", "keep-alive")
    ,new BasicHeader("keep-alive", "115")
    ,new BasicHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2")
};

回答by sarahTheButterFly

I've never used org.apache.http.* before, so I had a look at the API. From there I can see that Header is an interface and 'org.apache.http.message.BasicHeader' is one of its implementation. so maybe you'd like to use this type instead. Also I couldn't find HeaderList in the package.

我以前从未使用过 org.apache.http.*,所以我查看了 API。从那里我可以看到 Header 是一个接口,'org.apache.http.message.BasicHeader' 是它的实现之一。所以也许你想改用这种类型。另外我在包中找不到 HeaderList 。

回答by manoj

use this way

用这种方式

import org.apache.http.protocol.HTTP;
import org.apache.http.entity.ContentType;
import org.apache.http.message.BasicHeader
....

Header[] headers = {new BasicHeader(HTTP.CONTENT_TYPE, 
         ContentType.APPLICATION_JSON.toString())};
...