android restlet add non-standard header param
Asked Answered
C

2

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) {}
Contour answered 22/5, 2012 at 21:59 Comment(2)
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.Miscount
But how to I'll put a header that is not among the patterns rastlet?Contour
M
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"); 
Miscount answered 23/5, 2012 at 17:30 Comment(6)
Sorry, the request comes from ClientResource.getRequest(). I've updated my code snippet above.Miscount
Hmm, hold on a sec, getting a weird error while testing this.Miscount
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)Contour
Some things changed at some point and Restlet now uses Series<Header> 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 restlet-discuss.1400322.n2.nabble.com/….Miscount
@AndyDennie can you please share library link for org.restlet.engine.header.HeaderGomar
@SenthilMg, not sure exactly what you're looking for. General info about the Restlet framework is here. Javadoc for org.restlet.engine.header.Header (for the Android edition of Restlet) is here.Miscount
P
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();
}
Potoroo answered 4/6, 2012 at 9:13 Comment(2)
The OP asked how to do this using Restlet.Miscount
Sorry for hijacking this older question but is there a way to add a custom header in restlet 1.1 where there client resource obj reference does not exist?Fret

© 2022 - 2024 — McMap. All rights reserved.