Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. Username username;
  2. username = new Username();
  3. username.username=email;
  4. username.password=password;
  5. StringBuffer response = new StringBuffer();
  6. Gson gson= new Gson();
  7. String user_pass_json = gson.toJson(username);
  8.  
  9.  
  10. HttpURLConnection httpConnection = null;
  11. try{
  12. URL targetUrl = new URL('http://139.59.241.38/api-token-auth');
  13. httpConnection = (HttpURLConnection) targetUrl.openConnection();
  14.  
  15. httpConnection.setDoOutput(true);
  16. httpConnection.setRequestMethod("POST");
  17. httpConnection.setRequestProperty("Content-Type", "application/json");
  18. httpConnection.connect();
  19.  
  20.  
  21. //Enviando Request
  22. OutputStream outputStream = httpConnection.getOutputStream();
  23. outputStream.write(user_pass_json.getBytes());
  24. outputStream.flush();
  25.  
  26. if (httpConnection.getResponseCode() != 200){
  27. return ("Failed : HTTP error code : " + httpConnection.getResponseCode());
  28. }
  29.  
  30. //Recebendo Response
  31. InputStream is = httpConnection.getInputStream();
  32. BufferedReader rd = new BufferedReader(new InputStreamReader(is));
  33.  
  34. String line;
  35. while((line = rd.readLine()) != null) {
  36. response.append(line);
  37. response.append('\r');
  38. }
  39. rd.close();
  40. return response.toString();
  41.  
  42. }catch (MalformedURLException e) {
  43. e.printStackTrace();
  44. return "MalformedURLException";
  45.  
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. return ""+httpConnection.getErrorStream ();
  49. }finally {
  50.  
  51. if(httpConnection != null) {
  52. httpConnection.disconnect();
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement