Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoginActivity extends AppCompatActivity {
- private static String response, username, password, fdid;
- private static HttpClient httpclient;
- EditText fdidTextBox, usernameTextBox, passwordTextBox;
- Button loginButton;
- User user;
- public static final MediaType JSON
- = MediaType.parse("application/json; charset=utf-8");
- OkHttpClient client = new OkHttpClient();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- fdidTextBox = (EditText) findViewById(R.id.fdidEditText);
- usernameTextBox = (EditText) findViewById(R.id.usernameEditText);
- passwordTextBox = (EditText) findViewById(R.id.passwordEditText);
- loginButton = (Button) findViewById(R.id.login_button);
- //only 5 digits for fdid
- int maxLength = 5;
- fdidTextBox.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength) {
- }});
- loginButtonListener();
- }
- private void loginButtonListener() {
- username = usernameTextBox.getText().toString();
- password = passwordTextBox.getText().toString();
- fdid = fdidTextBox.getText().toString();
- loginButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- makePostRequestOnNewThread();
- }
- });
- }
- private void makePostRequestOnNewThread() {
- Thread t = new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- //getUrlContent("http://fireeport.com/api/login");
- postToServer("http://fireeport.com/api/login");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- });
- t.start();
- }
- String getUrlContent(String url) throws IOException {
- Request request = new Request.Builder()
- .url(url)
- .build();
- Response response = client.newCall(request).execute();
- Log.d("result",response.body().string());
- return response.body().string();
- }
- String postToServer(String url) throws IOException {
- RequestBody formBody=new FormBody.Builder().add("username",username).add("password",password).add("fdid",fdid).build();
- Request request = new Request.Builder()
- .url(url).post(formBody)
- .build();
- Response response = client.newCall(request).execute();
- Log.d("result",response.body().string());
- return response.body().string();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement