title: GSON help/Connecting to a web service (Android Studio)
body: <p>I am new to Android application building. I am trying to figure out how to send a response to a web service and validate the data. Here is my code:</p>
  
  <hr>
  
  <p>LoginUser.java</p>
  
  <pre><code>import org.json.JSONException;
  import org.json.JSONObject;
  
  import android.app.ProgressDialog;
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.view.View;
  import android.widget.EditText;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.loopj.android.http.AsyncHttpClient;
  import com.loopj.android.http.AsyncHttpResponseHandler;
  import com.loopj.android.http.RequestParams;
  
  
  
  import cz.msebera.android.httpclient.Header;
  
  public class LoginUser extends AppCompatActivity
  {
  // Progress Dialog Object
  ProgressDialog prgDialog;
  // Error Msg TextView Object
  TextView errorMsg;
  // Email Edit View Object
  EditText emailET;
  // Passwprd Edit View Object
  EditText pwdET;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.content_main);
      // Find Error Msg Text View control by ID
      errorMsg = (TextView) findViewById(R.id.login_error);
      // Find Email Edit View control by ID
      emailET = (EditText) findViewById(R.id.edtemail);
      // Find Password Edit View control by ID
      pwdET = (EditText) findViewById(R.id.edtpass);
      // Instantiate Progress Dialog object
      prgDialog = new ProgressDialog(this);
      // Set Progress Dialog Text
      prgDialog.setMessage("Please wait...");
      // Set Cancelable as False
      prgDialog.setCancelable(false);
  }
  
  
  
  
  /**
   * Method gets triggered when Login button is clicked
   *
   * @param view
   */
  public void loginUser(View view) {
      // Get Email Edit View Value
      String email = emailET.getText().toString();
      // Get Password Edit View Value
      String password = pwdET.getText().toString();
  
      // Instantiate Http Request Param Object
      RequestParams params = new RequestParams();
  
      // When Email Edit View and Password Edit View have values other than Null
      if (Utility.isNotNull(email) &amp;&amp; Utility.isNotNull(password)) {
          // When Email entered is Valid
          if (Utility.validate(email)) {
              // Put Http parameter username with value of Email Edit         View control
              params.put("Username", email);
              // Put Http parameter password with value of Password Edit Value control
              params.put("Pass", password);
              // Invoke RESTful Web Service with Http parameters
              Login(params);
          }
          // When Email is invalid
          else {
              Toast.makeText(getApplicationContext(), "Please enter valid email address with no spaces!", Toast.LENGTH_LONG).show();
          }
      } else {
          Toast.makeText(getApplicationContext(), "Please fill the form, don't leave any field blank!", Toast.LENGTH_LONG).show();
      }
  
  
  }
  
  
  
  /**
   * Method that performs RESTful webservice invocations
   *
   * @param params
   */
  public void Login(RequestParams params){
      // Show Progress Dialog
      prgDialog.show();
  
      // Make RESTful webservice call using AsyncHttpClient object
      final AsyncHttpClient client = new AsyncHttpClient();
      client.get("MY URL IS HERE", params, new AsyncHttpResponseHandler() {
  
  
  
          // When the response returned by REST has Http response code '200'
          @Override
          public void onSuccess(int statusCode, Header[] headers, byte[] responseBody)
          {
  
              // Hide Progress Dialog
              prgDialog.hide();
              try {
  
                  JSONObject obj = new JSONObject(new String(responseBody));
  
                  // When the JSON response has status boolean value assigned with true
                  if (obj.getBoolean("status")) {
                      Toast.makeText(getApplicationContext(), "You are successfully logged in!", Toast.LENGTH_LONG).show();
                      // Navigate to Home screen
                      navigatetoMyLocationActivity();
                  }
                  // Else display error message
                  else {
                      errorMsg.setText(obj.getString("error_msg"));
                      Toast.makeText(getApplicationContext(),     obj.getString("error_msg"), Toast.LENGTH_LONG).show();
                  }
              } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  Toast.makeText(getApplicationContext(), "Error Occurred [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
                  e.printStackTrace();
  
              }
  
          }
  
  
          // When the response returned by REST has Http response code other than '200'
          @Override
          public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
              // Hide Progress Dialog
              prgDialog.hide();
              // When Http response code is '404'
              if (statusCode == 404) {
                  Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
              }
              // When Http response code is '500'
              else if (statusCode == 500) {
                  Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
              }
              // When Http response code other than 404, 500
              else {
                  Toast.makeText(getApplicationContext(), "Unexpected Error occurred! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
              }
  
          }
      });
  }
  
  
  
  
  /**
   * Method which navigates from Login Activity to Home Activity
   */
  public void navigatetoMyLocationActivity(){
      Intent homeIntent = new Intent(getApplicationContext(),MyLocation.class);
      homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      startActivity(homeIntent);
    }
  }
  </code></pre>
  
  <hr>
  
  <p>I also created a DTO but I am not sure if it is necessary or not but here is:</p>
  
  <p>LoginUserDTO.java</p>
  
  <pre><code>public class LoginUserDTO
  {
  private String UserName;
  private String Pass;
  
  
  public LoginUserDTO(String UserName, String Pass)
   {
    this.UserName = UserName;
    this.Pass = Pass;
  
   }
  
  
  public String getUsername()
  {
      return UserName;
   }
  
  public void setUserName(String UserName)
  {
      this.UserName = UserName;
   }
  
  
  
  public String getPass()
   {
      return Pass;
    }
  
  public void setPass(String Pass)
    {
      this.Pass = Pass;
    }
  }
  </code></pre>
  
  <hr>
  
  <p>I also have the internet permission in my AndroidManifest.xml</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  </code></pre>
  
  <hr>
  
  <p>If someone could at least point me in the right direction I would greatly appreciate it! Thanks in advance!</p>