thelittlewozniak

Untitled

Dec 26th, 2018
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. button.setOnClickListener(new View.OnClickListener() {
  2.             @RequiresApi(api = Build.VERSION_CODES.N)
  3.             public void onClick(View v) {
  4.                 URL url= null;
  5.                 try {
  6.                     url = new URL("http://androidweb.azurewebsites.net/utilisateur/login");
  7.                 } catch (MalformedURLException e) {
  8.                     e.printStackTrace();
  9.                 }
  10.                 HttpURLConnection connection=null;
  11.                 int responseCode=0;
  12.                 try {
  13.                     //JSONObject json=jsonParser.makeHttpRequest();
  14.                     connection = (HttpURLConnection) url.openConnection();
  15.                     connection.setRequestMethod("POST");
  16.                     TextView email=findViewById(R.id.email);
  17.                     TextView password=findViewById(R.id.password);
  18.                     connection.addRequestProperty("pseudo", email.getText().toString());
  19.                     connection.addRequestProperty("motDePasse", password.getText().toString());
  20.                     connection.setRequestProperty("Content-Type", "text/plain");
  21.                     connection.setRequestProperty("charset", "utf-8");
  22.                     connection.setConnectTimeout(10000); //Apres 10 sec renvoie un timeout
  23.                     connection.connect(); //Effectue la connection
  24.                     responseCode=connection.getResponseCode();
  25.                 } catch (IOException e) {
  26.                     e.printStackTrace();
  27.                 }
  28.                 if(responseCode==200){
  29.                     try {
  30.                         InputStream stream = connection.getInputStream();
  31.                         BufferedReader rd = new BufferedReader(new InputStreamReader(stream, Charset.forName("UTF-8")));
  32.                         JsonReader reader=new JsonReader(rd);
  33.                     } catch (IOException e) {
  34.                         e.printStackTrace();
  35.                     }
  36.                 }
  37.             }
  38.         });
Advertisement
Add Comment
Please, Sign In to add comment