2010-08-11 15 views
9

Sono nuovo in Java.Come creare un array di org.apache.http.Header?

che sto cercando di fare

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

ma ha detto

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

come posso farlo?

risposta

3

Non ho mai usato org.apache.http. * Prima, quindi ho dato un'occhiata all'API. Da lì posso vedere che Header è un'interfaccia e 'org.apache.http.message.BasicHeader' è una delle sue implementazioni. quindi forse ti piacerebbe usare questo tipo, invece. Inoltre non sono riuscito a trovare HeaderList nel pacchetto.

43

ho trovato la risposta

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

Segna come corretta? – Sunkas

0

uso questo modo

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