Guest User

Untitled

a guest
Oct 20th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. package com.example.android.d1;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5.  
  6. import java.io.IOException;
  7. import org.apache.http.HttpException;
  8. import org.apache.http.HttpHost;
  9. import org.apache.http.auth.AuthScope;
  10. import org.apache.http.auth.UsernamePasswordCredentials;
  11. import org.apache.http.client.CredentialsProvider;
  12. import org.apache.http.client.methods.CloseableHttpResponse;
  13. import org.apache.http.client.methods.HttpGet;
  14. import org.apache.http.impl.client.BasicCredentialsProvider;
  15. import org.apache.http.impl.client.CloseableHttpClient;
  16. import org.apache.http.impl.client.HttpClients;
  17. import org.apache.http.util.EntityUtils;
  18.  
  19. public class MainActivity extends AppCompatActivity {
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24.  
  25. String username = "xxxx";
  26. String password = "xxxxxxxx";
  27. String hostname = "xxxxx.service-now.com";
  28.  
  29. CredentialsProvider provider = new BasicCredentialsProvider();
  30. UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
  31. provider.setCredentials(new AuthScope(new HttpHost(hostname)), credentials);
  32. CloseableHttpClient httpclient = HttpClients.custom()
  33. .setDefaultCredentialsProvider(provider)
  34. .build();
  35.  
  36. String query = "https://xxxxx.service-now.com/api/now/table/incident?sysparm_limit=1";
  37.  
  38. try {
  39. HttpGet httpget = new HttpGet(query);
  40. httpget.setHeader("Accept", "application/json");
  41. System.out.println("Executing request " + httpget.getRequestLine());
  42. CloseableHttpResponse response = httpclient.execute(httpget);
  43. try {
  44. System.out.println("----------------------------------------");
  45. System.out.println(response.getStatusLine());
  46. String responseBody = EntityUtils.toString(response.getEntity());
  47. System.out.println(responseBody);
  48. }
  49. finally {
  50. response.close();
  51. }
  52. } finally {
  53. httpclient.close();
  54. }
  55. }
  56. }
  57.  
  58. apply plugin: 'com.android.application'
  59.  
  60. android {
  61. compileSdkVersion 28
  62. defaultConfig {
  63. applicationId "com.example.android.d1"
  64. minSdkVersion 15
  65. targetSdkVersion 28
  66. versionCode 1
  67. versionName "1.0"
  68. testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  69. }
  70. buildTypes {
  71. release {
  72. minifyEnabled false
  73. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  74. }
  75. }
  76. packagingOptions {
  77. exclude 'META-INF/DEPENDENCIES'
  78. }
  79. }
  80.  
  81. dependencies {
  82. implementation fileTree(include: ['*.jar'], dir: 'libs')
  83. implementation 'com.android.support:appcompat-v7:28.0.0'
  84. implementation 'com.android.support.constraint:constraint-layout:1.1.3'
  85. testImplementation 'junit:junit:4.12'
  86. androidTestImplementation 'com.android.support.test:runner:1.0.2'
  87. androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
  88. implementation files('libs/commons-cli-1.2.jar')
  89. implementation files('libs/commons-codec-1.10.jar')
  90. implementation files('libs/commons-logging-1.2.jar')
  91. implementation files('libs/fluent-hc-4.5.6.jar')
  92. implementation files('libs/httpclient-4.5.6.jar')
  93. implementation files('libs/httpclient-cache-4.5.6.jar')
  94. implementation files('libs/httpclient-win-4.5.6.jar')
  95. implementation files('libs/httpcore-4.4.10.jar')
  96. implementation files('libs/httpcore-ab-4.4.10.jar')
  97. implementation files('libs/httpcore-nio-4.4.10.jar')
  98. implementation files('libs/httpmime-4.5.6.jar')
  99. implementation files('libs/jna-4.4.0.jar')
  100. implementation files('libs/jna-platform-4.4.0.jar')
  101. }
  102.  
  103. try {
  104. HttpGet httpget = new HttpGet(query);
  105. httpget.setHeader("Accept", "application/json");
  106. System.out.println("Executing request " + httpget.getRequestLine());
  107. CloseableHttpResponse response = httpclient.execute(httpget);
  108. try {
  109. System.out.println("----------------------------------------");
  110. System.out.println(response.getStatusLine());
  111. String responseBody = EntityUtils.toString(response.getEntity());
  112. System.out.println(responseBody);
  113. }catch(Exception e){ //Not really necessary
  114. e.printStackTrace(); //Prints the exception in the logs
  115. }
  116. finally {
  117. response.close();
  118. }
  119. }catch(IOException e){
  120. e.printStackTrace();
  121. }
  122. finally {
  123. httpclient.close();
  124. }
  125.  
  126. import java.io.IOException;
Add Comment
Please, Sign In to add comment