You need to sign in to do that
Don't have an account?
Jimmy Armstrong
REST calls from standalone java application
HI,
Is it possible to use the new REST API from a standalone java application?
The example used on the site refers to a web app.
Also can we use this REST api from a J2ME enabled phone?
Thanks
-Kedar
You can use the REST API from any environment that can make http requests. the sample uses a web app to enable the interactive ouath flow for authentication, but there's no requirement to use that, you can use the soap login, the username/password oauth flow, or any of the other methods to obtain a sessionId to use with the rest api.
All Answers
You can use the REST API from any environment that can make http requests. the sample uses a web app to enable the interactive ouath flow for authentication, but there's no requirement to use that, you can use the soap login, the username/password oauth flow, or any of the other methods to obtain a sessionId to use with the rest api.
Apache's HTTP client is a popular one. Your code might look something like this:
restClient.getParams().setCookiePolicy(CookiePolicy.RFC_2109); restClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
//these should not be literals in your actual program; provided only to illustrate this example
String sid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String url = "https://na7.salesforce.com/services/data/v20.0/sobjects";
GetMethod method = new GetMethod(url);
method.setRequestHeader("Authorization", "OAuth " + sid);
int httpResponseCode = restClient.executeMethod(method);
If it was a PostMethod on the URL for sobjects/account for example, you'd add these rows:
String json = "{ \"name\" : \"myNewAccount\" }";
method.setRequestHeader("Content-Type", "application/json");
method.setRequestBody(json);
You mention Apache's Http client. I've downloaded HttpClient and HttpCore from here, but none of the objects you have in your example (like GetMethod or even HttpClient (although DefaultHttpClient is there)) seem to be in there. Can you comment on which library has these?
I think most mentioned of HttpClient here are related to HttpClient 3.x, whereas you've probably downloaded 4.x