Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Username username;
- username = new Username();
- username.username=email;
- username.password=password;
- StringBuffer response = new StringBuffer();
- Gson gson= new Gson();
- String user_pass_json = gson.toJson(username);
- HttpURLConnection httpConnection = null;
- try{
- URL targetUrl = new URL('http://139.59.241.38/api-token-auth');
- httpConnection = (HttpURLConnection) targetUrl.openConnection();
- httpConnection.setDoOutput(true);
- httpConnection.setRequestMethod("POST");
- httpConnection.setRequestProperty("Content-Type", "application/json");
- httpConnection.connect();
- //Enviando Request
- OutputStream outputStream = httpConnection.getOutputStream();
- outputStream.write(user_pass_json.getBytes());
- outputStream.flush();
- if (httpConnection.getResponseCode() != 200){
- return ("Failed : HTTP error code : " + httpConnection.getResponseCode());
- }
- //Recebendo Response
- InputStream is = httpConnection.getInputStream();
- BufferedReader rd = new BufferedReader(new InputStreamReader(is));
- String line;
- while((line = rd.readLine()) != null) {
- response.append(line);
- response.append('\r');
- }
- rd.close();
- return response.toString();
- }catch (MalformedURLException e) {
- e.printStackTrace();
- return "MalformedURLException";
- } catch (IOException e) {
- e.printStackTrace();
- return ""+httpConnection.getErrorStream ();
- }finally {
- if(httpConnection != null) {
- httpConnection.disconnect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement