function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jimmy ArmstrongJimmy 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

 

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

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

SuperfellSuperfell

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.

This was selected as the best answer
Jimmy ArmstrongJimmy Armstrong
Thanks
gwestrgwestr

Apache's HTTP client is a popular one.  Your code might look something like this:

 

 

  HttpClient restClient = new HttpClient();

   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);

 

 

BaloodevilBaloodevil

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?

SuperfellSuperfell

I think most mentioned of HttpClient here are related to HttpClient 3.x, whereas you've probably downloaded 4.x