Advertisement
Guest User

iabasami

a guest
May 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. public String performPostCall(String requestURL,String filename, String base64) {
  2.  
  3. URL url;
  4. String response = "";
  5. try {
  6. url = new URL(requestURL);
  7.  
  8. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  9. // conn.setReadTimeout(context.getResources().getInteger(
  10. // R.integer.maximum_timeout_to_server));
  11. // conn.setConnectTimeout(context.getResources().getInteger(
  12. // R.integer.maximum_timeout_to_server));
  13. conn.setRequestMethod("POST");
  14. conn.setDoInput(true);
  15. conn.setDoOutput(true);
  16.  
  17. conn.setRequestProperty("Content-Type", "application/json");
  18.  
  19. Log.e(TAG, "11 - url : " + requestURL);
  20.  
  21. /*
  22. * JSON
  23. */
  24.  
  25. JSONObject root = new JSONObject();
  26. //
  27. root.put("filename", filename);
  28. root.put("base64", base64);
  29.  
  30. Log.e(TAG, "12 - root : " + root.toString());
  31.  
  32. String str = root.toString();
  33. byte[] outputBytes = str.getBytes("UTF-8");
  34. OutputStream os = conn.getOutputStream();
  35. os.write(outputBytes);
  36.  
  37. int responseCode = conn.getResponseCode();
  38.  
  39. Log.e(TAG, "13 - responseCode : " + responseCode);
  40.  
  41. if (responseCode == HttpsURLConnection.HTTP_OK) {
  42. Log.e(TAG, "14 - HTTP_OK");
  43.  
  44. String line;
  45. BufferedReader br = new BufferedReader(new InputStreamReader(
  46. conn.getInputStream()));
  47. while ((line = br.readLine()) != null) {
  48. response += line;
  49. }
  50. } else {
  51. Log.e(TAG, "14 - False - HTTP_OK");
  52. response = "";
  53. }
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. }
  57.  
  58. return response;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement