title: Send a HTTP-POST Request Through Android Button
body: <p><em>I have to send an HTTP-Request (through an Android button) to a Java Servlet.</em></p>
  
  <p>I am stuck where I have to put the information (code) and what it must contain in order to send it.</p>
  
  <p><strong>Context:</strong> User Logs in --> Clicks on "Grant Access" --> Sends an HTTP-Request to Java Servlet(for example 192.168.1.1)</p>
  
  <p><strong>Note:</strong> I'm just a beginner in Android.</p>
  
  <p>I know that it has to do with the ID of the button "Grant Access".</p>
  
  <p><strong>AndroidManifest:</strong></p>
  
  <pre><code>  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.mertino11.ourapplication"&gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  
      &lt;application
          android:name=".FireApp"
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:supportsRtl="true"&gt;
  
          &lt;activity
              android:name=".MainActivity"
              android:theme="@style/AppTheme"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN"/&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER"/&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;activity
              android:name=".AccountActivity"
              android:theme="@style/AppTheme" /&gt;
      &lt;/application&gt;
  
      &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>Activity_main_xml:</strong></p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:baselineAligned="false"&gt;
  
      &lt;ImageView
          android:layout_width="match_parent"
          android:layout_height="163dp"
          app:srcCompat="@drawable/snetech"
          android:id="@+id/imageView" /&gt;
  
      &lt;EditText
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:inputType="textEmailAddress"
          android:ems="10"
          android:id="@+id/emailField"
          android:hint="Email"
          android:paddingTop="20dp" /&gt;
  
      &lt;EditText
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:inputType="textPassword"
          android:ems="10"
          android:id="@+id/passwordField"
          android:hint="Password"
          android:fontFamily="sans-serif"
          android:paddingTop="20dp" /&gt;
  
      &lt;Button
          android:text="Login"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/loginBtn"
          android:paddingTop="20dp" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>Activity_account (If user logs successful):</strong></p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/activity_account"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context="com.example.mertino11.ourapplication.AccountActivity"&gt;
  
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="logout"
          android:id="@+id/logout"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"
          android:layout_marginBottom="16dp" /&gt;
  
      &lt;Button
          android:text="Grant Access"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/grantAccess"
          android:layout_marginBottom="70dp"
          android:layout_above="@+id/logout"
          android:layout_centerHorizontal="true" /&gt;
  
      &lt;TextView
          android:text="Main Menu"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/textView"
          android:textStyle="normal|bold"
          android:textSize="30sp"
          android:textAlignment="center"
          android:fontFamily="sans-serif"
          android:textAllCaps="false"
          android:layout_centerVertical="true"
          android:layout_centerHorizontal="true" /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>The Java Classes I use:</strong><br>
  <strong>AccountActivity</strong></p>
  
  <pre><code>    import android.content.Intent;
      import android.os.Bundle;
      import android.support.v7.app.AppCompatActivity;
      import android.view.View;
      import android.widget.Button;
  
      import com.google.firebase.auth.FirebaseAuth;
      public class AccountActivity extends AppCompatActivity {
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_account);
  
          Button logout=(Button) findViewById(R.id.logout);
          logout.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  FirebaseAuth.getInstance().signOut();
                  startActivity(new Intent(AccountActivity.this,MainActivity.class));
                  finish();
              }
          });
  
  
      }
  
  
  }
  </code></pre>
  
  <p><strong>MainActivity:</strong></p>
  
  <pre><code>    import android.content.Intent;
      import android.os.Bundle;
      import android.support.annotation.NonNull;
      import android.support.v7.app.AppCompatActivity;
      import android.text.TextUtils;
      import android.view.View;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.Toast;
  
      import com.google.android.gms.tasks.OnCompleteListener;
      import com.google.android.gms.tasks.Task;
      import com.google.firebase.auth.AuthResult;
      import com.google.firebase.auth.FirebaseAuth;
  
      public class MainActivity extends AppCompatActivity {
  
      private EditText mEmailField;
      private EditText mPasswordField;
  
      private Button mLoginBtn;
  
      private FirebaseAuth mAuth;
  
      private FirebaseAuth.AuthStateListener mAuthListener;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          mAuth = FirebaseAuth.getInstance();
  
          mEmailField = (EditText) findViewById(R.id.emailField);
          mPasswordField = (EditText) findViewById(R.id.passwordField);
  
          mLoginBtn = (Button) findViewById(R.id.loginBtn);
  
          mAuthListener = new FirebaseAuth.AuthStateListener() {
              @Override
              public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  
                  if(firebaseAuth.getCurrentUser() != null) {
  
                      startActivity(new Intent(MainActivity.this, AccountActivity.class));
  
                  }
              }
          };
  
          mLoginBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
  
                  startSignIn();
  
              }
          });
      }
  
      @Override
      protected void onStart() {
          super.onStart();
  
          mAuth.addAuthStateListener(mAuthListener);
      }
  
      private void startSignIn() {
  
          String email = mEmailField.getText().toString();
          String password = mPasswordField.getText().toString();
  
          if(TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {
  
              Toast.makeText(MainActivity.this, "Fields are empty!", Toast.LENGTH_LONG).show();
  
          } else {
              mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener&lt;AuthResult&gt;() {
                  @Override
                  public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
  
                      if(!task.isSuccessful()){
  
                          Toast.makeText(MainActivity.this, "Sign In Problem!", Toast.LENGTH_LONG).show();
                      }
                  }
              });
          }
  
  
  
      }
  }
  </code></pre>
  
  <p><strong>FireApp:</strong></p>
  
  <pre><code>    import android.app.Application;
  
      import com.firebase.client.Firebase;
  
  
      public class FireApp extends Application {
      @Override
      public void  onCreate() {
        super.onCreate();
  
       Firebase.setAndroidContext(this);
      }
      }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/IFr1R.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IFr1R.png" alt="Login Screen"></a>
  <a href="https://i.stack.imgur.com/lh6eu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lh6eu.png" alt="User enters his Main Menu"></a></p>