6

How I can add Header params in restlet android?

I have following code:

ClientResource clientResource = null;
try {
    clientResource = new ClientResource(jsonRestHelper.getUrl());
    for (Parameter parameter : jsonRestHelper.getParameters()) {
        //here works perfectly, jsonRestHelper is a helper class
        clientResource.addQueryParameter(parameter);
    }
    //here I wanna add headers param example
    //addHeader("Key","Value");
} catch (Exception e) {}
ademar111190
  • 14,215
  • 14
  • 85
  • 114
  • Restlet doesn't have a simple generic method like addHeader("Key", "Value"). Instead, it has a variety of objects and properties attached to the Request and Response objects, which you use to specify what you want. Which specific header or headers do you want to set? By the way, if you have the Restlet in Action eBook, appendix E outlines how various headers map to Restlet objects and properties. – Andy Dennie May 23 '12 at 12:29
  • But how to I'll put a header that is not among the patterns rastlet? – ademar111190 May 23 '12 at 15:49

2 Answers2

4

Do you want to add custom (i.e. non-standard) headers? If so, try this:

import java.util.concurrent.ConcurrentMap;
import org.restlet.data.Form;
import org.restlet.engine.header.Header;
import org.restlet.engine.header.HeaderConstants;
import org.restlet.resource.ClientResource;
import org.restlet.util.Series;


...

clientResource = new ClientResource("http://someurl.com");
ConcurrentMap<String, Object> attrs = clientResource.getRequest().getAttributes();
Series<Header> headers = (Series<Header>) attrs.get(HeaderConstants.ATTRIBUTE_HEADERS);
if (headers == null) {
    headers = new Series<Header>(Header.class);
    Series<Header> prev = (Series<Header>) 
        attrs.putIfAbsent(HeaderConstants.ATTRIBUTE_HEADERS, headers);
    if (prev != null) { headers = prev; }
}
headers.add("myHeaderName", "myHeaderValue"); 
Andy Dennie
  • 6,012
  • 2
  • 32
  • 51
  • Sorry, the request comes from ClientResource.getRequest(). I've updated my code snippet above. – Andy Dennie Jun 04 '12 at 14:05
  • Hmm, hold on a sec, getting a weird error while testing this. – Andy Dennie Jun 04 '12 at 14:51
  • I get a error:06-04 11:37:10.447: W/System.err(430): Communication Error (1001) - Error while processing a connection 06-04 11:37:10.447: W/System.err(430): at org.restlet.resource.ClientResource.doError(ClientResource.java:627) 06-04 11:37:10.447: W/System.err(430): at org.restlet.engine.resource.ClientInvocationHandler.invoke(ClientInvocationHandler.java:236) 06-04 11:37:10.447: W/System.err(430): at $Proxy5.retrieve(Native Method) – ademar111190 Jun 04 '12 at 14:52
  • Some things changed at some point and Restlet now uses Series
    instead of Form for setting headers. I've tested the (now corrected) code above and it's working for me. Sorry for the confusion. BTW, this code was copied from something Thierry Boileau wrote at http://restlet-discuss.1400322.n2.nabble.com/How-to-set-response-header-tp7270489p7270784.html.
    – Andy Dennie Jun 04 '12 at 15:31
  • @AndyDennie can you please share library link for org.restlet.engine.header.Header – Senthil Mg Nov 20 '12 at 13:47
  • @SenthilMg, not sure exactly what you're looking for. General info about the Restlet framework is [here](http://www.restlet.org/). Javadoc for org.restlet.engine.header.Header (for the Android edition of Restlet) is [here](http://www.restlet.org/documentation/2.1/android/engine/org/restlet/engine/header/Header.html). – Andy Dennie Nov 20 '12 at 18:18
3

Use Below code

HttpClient client = new DefaultHttpClient();  
String getURL = "rest url";
HttpGet get = new HttpGet(getURL);
get.setParams(HttpParams parmams)
get.setHeader("Key","valye"); // Request Header
    try {
        HttpResponse responseGet = client.execute(get);
   } 
   catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
  } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
}
Brijesh Thakur
  • 6,768
  • 4
  • 24
  • 29