Home » Android » java – How make an array of org.apache.http.Header?

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

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m new in 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?

How to&Answers:

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")
};

Answer:

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.

Answer:

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())};
...