Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //on create
- Button button = (Button) findViewById(R.id.button);
- login = findViewById((R.id.Login));
- password = findViewById(R.id.Password);
- button.setOnClickListener( new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- sendPost();
- try {
- TimeUnit.SECONDS.sleep(2);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- sendPost();
- }
- });
- public void sendPost() {
- Thread thread = new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- URL url = new URL("http://cargoalgps.cba.pl/api/uzytkownik/login.php");
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("POST");
- conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
- conn.setRequestProperty("Accept","application/json");
- conn.setDoOutput(true);
- conn.setDoInput(true);
- JSONObject jsonParam = new JSONObject();
- jsonParam.put("login", login.getText());
- jsonParam.put("haslo", password.getText());
- Log.i("JSON", jsonParam.toString());
- DataOutputStream os = new DataOutputStream(conn.getOutputStream());
- //os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
- os.writeBytes(jsonParam.toString());
- os.flush();
- os.close();
- if(conn.getResponseCode() == 200){
- text.setText("zalogowano");
- }
- else{
- text.setText("bledne dane");
- }
- conn.disconnect();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- thread.start();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement