Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. public void onClick(View v) {
  2. inputSub = subIn.getText().toString();
  3.  
  4. String url = "https://some_url";
  5.  
  6. String inputJson = null;
  7. try {
  8. inputJson = "{ "asset": {"id":" ..........}
  9.  
  10. }catch (JSONException e){
  11. e.printStackTrace();
  12. }
  13. new CreateIncident(url, inputJson).execute();
  14. }
  15.  
  16. private class CreateIncident extends AsyncTask<Void, Void, Void> {
  17. private final String TAG = "HttpClient";
  18.  
  19. final String user ="some_user";
  20. final String password ="some_pwd";
  21.  
  22. String serUrl;
  23. String inputJson ="";
  24.  
  25. public CreateIncident(String serUrl, String inputJson) {
  26. this.serUrl = serUrl;
  27. this.inputJson = inputJson;
  28. }
  29.  
  30. @Override
  31. protected void onPreExecute() {
  32. super.onPreExecute();
  33.  
  34. // Showing progress dialog
  35. pDialog = new ProgressDialog(CreateIncidentActivity.this);
  36. pDialog.setMessage("Please wait...");
  37. pDialog.setCancelable(false);
  38. pDialog.show();
  39. }
  40.  
  41. @Override
  42. protected Void doInBackground(Void... params) {
  43.  
  44. try {
  45.  
  46. String authString = user + ":" + password;
  47.  
  48. byte[] authBytes = authString.getBytes();
  49. String authStringEnc = Base64.encodeToString(authBytes, Base64.DEFAULT);
  50.  
  51. DefaultHttpClient httpClient = new DefaultHttpClient();
  52. HttpPost postRequest = new HttpPost(serUrl);
  53.  
  54. postRequest.setHeader("Authorization", "Basic " + authStringEnc);
  55. postRequest.setHeader("Accept", "application/json");
  56. postRequest.setHeader("Content-type", "application/json");
  57.  
  58. StringEntity input = new StringEntity(inputJson);
  59.  
  60. input.setContentType("application/json");
  61. postRequest.setEntity(input);
  62.  
  63. HttpResponse response = httpClient.execute(postRequest);
  64.  
  65. StringBuilder sb = new StringBuilder();
  66. try {
  67. BufferedReader reader =
  68. new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 65728);
  69. String line = null;
  70.  
  71. while ((line = reader.readLine()) != null) {
  72. sb.append(line);
  73. }
  74. }
  75. catch (IOException e) { e.printStackTrace(); }
  76. catch (Exception e) { e.printStackTrace(); }
  77.  
  78.  
  79. System.out.println("finalResult " + sb.toString());
  80. System.out.println(response.getStatusLine().getReasonPhrase());
  81.  
  82. if (response.getStatusLine().getStatusCode() != 201) {
  83. throw new RuntimeException("Failed : HTTP error code : "
  84. + response.getStatusLine().getStatusCode());
  85. }
  86.  
  87. BufferedReader br = new BufferedReader(
  88. new InputStreamReader((response.getEntity().getContent())));
  89.  
  90.  
  91. httpClient.getConnectionManager().shutdown();
  92.  
  93. } catch (MalformedURLException e) {
  94.  
  95. e.printStackTrace();
  96.  
  97. } catch (IOException e) {
  98. e.printStackTrace();
  99.  
  100. }
  101.  
  102. @Override
  103. protected void onPostExecute(Void result) {
  104. super.onPostExecute(result);
  105. // Dismiss the progress dialog
  106. if (pDialog.isShowing())
  107. pDialog.dismiss();
  108.  
  109. }
  110. }
  111.  
  112. useLibrary 'org.apache.http.legacy'
  113.  
  114. apply plugin: 'com.android.application'
  115.  
  116. android {
  117. useLibrary 'org.apache.http.legacy'
  118. compileSdkVersion 26
  119. buildToolsVersion "26.0.0"
  120. defaultConfig {
  121. applicationId "com.example.mnaum.getgpscoordinates"
  122. minSdkVersion 15
  123. targetSdkVersion 26
  124. versionCode 1
  125. versionName "1.0"
  126. testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement