Guest User

Untitled

a guest
May 22nd, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. // The connection data
  2. private static final String query = "SELECT Name, Id from Account";
  3. private static final String clientId = "Your salesforce consumer Key";
  4. private static final String clientSecret = "Secret Key";
  5. // THis is meaningless in our context
  6. private static final String redirectUri = "Redirect URI";
  7. //private static final String environment = "https://harishgakhar40-dev-ed.my.salesforce.com";
  8. private static final String environment = "https://login.salesforce.com";
  9. private static String tokenUrl = null;
  10. private static final String username = "salesforce Username";
  11. private static final String password = "salesforce password + security token";
  12. private static String accessToken = null;
  13. private static String instanceUrl = null;
  14. private ArrayAdapter<String> listAdapter;
  15.  
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20.  
  21. // Create list adapter
  22. listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
  23. ((ListView) findViewById(R.id.contacts_list)).setAdapter(listAdapter);
  24. if (android.os.Build.VERSION.SDK_INT > 9) {
  25. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  26. StrictMode.setThreadPolicy(policy);
  27. }
  28. // Creating HTTP client
  29. HttpClient httpClient = new DefaultHttpClient();
  30.  
  31. tokenUrl = environment + "/services/oauth2/token?grant_type=password&client_id=" + clientId + "&client_secret=" + clientSecret + "&username=" + username + "&password=" + password;
  32. HttpPost httpPost = new HttpPost(tokenUrl);
  33.  
  34. // Building post parameters
  35. // key and value pair
  36. List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
  37.  
  38. nameValuePair.add(new BasicNameValuePair("Content-Type", "application/json"));
  39. try {
  40. httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
  41. } catch (UnsupportedEncodingException e){
  42. // writing error to Log
  43. e.printStackTrace();
  44. }
  45. JSONTokener tokener;
  46. String accessToken,instanceURL;
  47. JSONArray finalResult;
  48. // Making HTTP Request
  49. try {
  50.  
  51. HttpResponse response = httpClient.execute(httpPost);
  52. int code = response.getStatusLine().getStatusCode();
  53.  
  54. BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
  55. String json = reader.readLine();
  56. JSONObject jObj = new JSONObject(json);
  57. accessToken = jObj.getString("access_token");
  58. instanceURL = jObj.getString("instance_url");
  59. getAccountData(accessToken, instanceURL);
  60.  
  61. } catch (Exception e) {
  62.  
  63. e.getMessage();
  64.  
  65. }
  66. }
  67.  
  68.  
  69. public void getAccountData(String access_token, String instance_url) {
  70.  
  71. DefaultHttpClient client = new DefaultHttpClient();
  72. String url = instance_url + "/services/data/v20.0/query/?q=";
  73. String soqlQuery = "Select Id, Name, BillingStreet, BillingCity, BillingState From Account ";
  74.  
  75. try
  76. {
  77. url += URLEncoder.encode(soqlQuery, "UTF-8");
  78. }
  79. catch(UnsupportedEncodingException e){}
  80.  
  81. HttpGet getRequest = new HttpGet(url);
  82. getRequest.addHeader("Authorization", "OAuth " + access_token);
  83.  
  84. try {
  85. HttpResponse response = client.execute(getRequest);
  86.  
  87. String result = EntityUtils.toString(response.getEntity());
  88.  
  89. JSONObject object = (JSONObject) new JSONTokener(result).nextValue();
  90. JSONArray records = object.getJSONArray("records");
  91.  
  92. listAdapter.clear();
  93. for (int i=0;i<records.length();i++) {
  94.  
  95.  
  96. JSONObject record = (JSONObject) records.get(i);
  97. String accountName = record.getString("Name");
  98. Log.v("accountName---- ","accountName ---- "+accountName);
  99. listAdapter.add(accountName);
  100.  
  101. }
  102.  
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. }
  106. }
Add Comment
Please, Sign In to add comment