Wachisu

Untitled

May 10th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. public class MainActivity extends Activity {
  2.     private final OkHttpClient client = new OkHttpClient();
  3.     private String usernameValue;
  4.     private String passwordValue;
  5.     private String databaseValue;
  6.     @InjectView(R.id.usernameField)     EditText usernameContainer;
  7.     @InjectView(R.id.passwordField)     EditText passwordContainer;
  8.     @InjectView(R.id.databaseField)     EditText databaseContainer;
  9.     @InjectView(R.id.loginButton)       Button   loginButton;
  10.  
  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_main);
  15.         ButterKnife.inject(this);
  16.  
  17.         loginButton.setOnClickListener(new View.OnClickListener() {
  18.             @Override
  19.             public void onClick(View v) {
  20.                 try {
  21.                     runLogin();
  22.                 } catch (Exception e) {
  23.                     e.printStackTrace();
  24.                 }
  25.             }
  26.         });
  27.  
  28.     }
  29.  
  30.  
  31.     public void runLogin() throws Exception {
  32.  
  33.         usernameValue = usernameContainer.getText().toString();
  34.         passwordValue = passwordContainer.getText().toString();
  35.         databaseValue = databaseContainer.getText().toString();
  36.  
  37.         RequestBody formBody = new FormEncodingBuilder()
  38.                 .add("Username", usernameValue)
  39.                 .add("Password", passwordValue)
  40.                 .add("Database", databaseValue)
  41.                 .build();
  42.  
  43.         Request request = new Request.Builder()
  44.                 .url("http://10.0.0.2/newnew/login.php")
  45.                 .post(formBody)
  46.                 .build();
  47.  
  48.         Response response = client.newCall(request).execute();
  49.         if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  50.  
  51.         if(response.isSuccessful())
  52.         {
  53.             Toast.makeText(getApplicationContext(), "Connected successfully",
  54.                     Toast.LENGTH_LONG).show();
  55.         }
  56.  
  57.         System.out.println(response.body().string());
  58.        // Log.i("LOGI", response.body().string());
  59.  
  60.     }
Advertisement
Add Comment
Please, Sign In to add comment