Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android restlet add non-standard header param

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) {}
like image 578
ademar111190 Avatar asked May 22 '12 21:05

ademar111190


2 Answers

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"); 
like image 94
Andy Dennie Avatar answered Oct 22 '22 14:10

Andy Dennie


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();
}
like image 3
Brijesh Thakur Avatar answered Oct 22 '22 14:10

Brijesh Thakur