-1

So I'm trying to upload some data to MySQL. I believe that I am effectively passing the value of email in LoginActivity.java to PartySetup.java, but the value is apparently getting lost somewhere along the way. I've tried using sharedPreferences with the same result. Does anyone have any ideas why this is happening? Thanks!

LoginActivity.java:

public class LoginActivity extends AppCompatActivity{

public static ArrayList<String> emailSaver = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstancedState){
    super.onCreate(savedInstancedState);
    setContentView(R.layout.activity_login);

    final EditText tempEmail = (EditText) findViewById(R.id.email);
    final EditText tempPassword = (EditText) findViewById(R.id.password);
    final Button loginButton = (Button) findViewById(R.id.btnLogin);
    final Button toRegisterScreen = (Button) findViewById(R.id.btnLinkToRegisterScreen);

    if (SaveSharedPreference.getUserName(LoginActivity.this).length() != 0){
        // If the user's already been logged in, skip the login screen
        Intent intent = new Intent(LoginActivity.this, OneTimeWelcome.class);
        startActivity(intent);
        finish();
    }

    toRegisterScreen.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
            LoginActivity.this.startActivity(registerIntent);
        }
    });

    loginButton.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            final String email = tempEmail.getText().toString();
            emailSaver.add(0, email);
            final String password = tempPassword.getText().toString();

            //Response received from the server
            Response.Listener<String> responseListener = new Response.Listener<String>(){
                @Override
                public void onResponse(String response){
                    try{
                        Log.i("TAG", response);
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success = jsonResponse.getBoolean("success");

                        if(success){
                            String name = jsonResponse.getString("name");

                            Intent intent = new Intent(LoginActivity.this, OneTimeWelcome.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            intent.putExtra("name", name);
                            intent.putExtra("email", email);
                            LoginActivity.this.startActivity(intent);
                            finish();
                        }
                        else{
                            AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                            builder.setMessage("Login Failed")
                                    .setNegativeButton("Retry", null)
                                    .create()
                                    .show();
                        }
                    }
                    catch(JSONException e){
                        e.printStackTrace();
                    }
                    SaveSharedPreference.setUserName(LoginActivity.this, email);
                }
            };

            LoginRequest loginRequest = new LoginRequest(email, password, responseListener);
            RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
            queue.add(loginRequest);
        }
    });
}
}

PartySetup.java:

public class PartySetup extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private GoogleApiClient mGoogleApiClient;
private Location location;
private TextView tempLatitude;
private TextView tempLongitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_party_setup);

    final EditText tempPartyName = (EditText) findViewById(R.id.party_name);
    final EditText tempHostName = (EditText) findViewById(R.id.host_name);
    final Button startParty = (Button) findViewById(R.id.create_party);
    final CheckBox locationButton = (CheckBox) findViewById(R.id.set_location);
    tempLatitude = (TextView) findViewById(R.id.latitude_text);
    tempLongitude = (TextView) findViewById(R.id.longitude_text);

    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    mGoogleApiClient.connect();

        startParty.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if (locationButton.isChecked() && tempPartyName != null){
                    final String partyName = tempPartyName.getText().toString();
                    final String hostName;
                    if (tempHostName == null){
                        hostName = "";
                    }
                    else hostName = tempHostName.getText().toString();
                    final String latitude = tempLatitude.getText().toString();
                    final String longitude = tempLongitude.getText().toString();
                    final String publicEmail = LoginActivity.emailSaver.get(0);

                    Response.Listener<String> responseListener = new Response.Listener<String>(){
                        @Override
                        public void onResponse(String response){
                            try{
                                Log.i("TAG", response);
                                JSONObject jsonResponse = new JSONObject(response);
                                boolean success = jsonResponse.getBoolean("success");
                                if(success){
                                    Intent intent = new Intent(PartySetup.this, HomePage.class);
                                    startActivity(intent);
                                }
                                else{
                                    AlertDialog.Builder builder = new AlertDialog.Builder(PartySetup.this);
                                    builder.setMessage("Invalid Party Nickname")
                                            .setNegativeButton("Try Again", null)
                                            .create()
                                            .show();
                                }
                            } catch (JSONException e){
                                e.printStackTrace();
                            }
                        }
                    };

                    CreatePartyRequest createPartyRequest = new CreatePartyRequest(partyName, hostName, latitude, longitude, publicEmail, responseListener);
                    RequestQueue queue = Volley.newRequestQueue(PartySetup.this);
                    queue.add(createPartyRequest);
                }
                else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(PartySetup.this);
                    builder.setMessage("Please check the location box")
                            .setNegativeButton("Try Again", null)
                            .create()
                            .show();
                }
            }
        });
    }

protected void onStart() {
    mGoogleApiClient.connect();
    super.onStart();
}

protected void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    try{
        location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    }
    catch (SecurityException e){
        e.printStackTrace();
    }
    if (location != null){
        tempLatitude.setText(String.valueOf(location.getLatitude()));
        tempLongitude.setText(String.valueOf(location.getLongitude()));
    }
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}
}

CreatePartyRequest.java:

public class CreatePartyRequest extends StringRequest {
private static final String CREATE_PARTY_REQUEST_URL = "http://10.0.2.2:8080/android_login_api/create_party_request.php";
private Map<String, String> params;

public CreatePartyRequest(String partyName, String hostName, String latitude, String longitude, String email, Response.Listener<String> listener){
    super(Method.POST, CREATE_PARTY_REQUEST_URL, listener, null);
    params = new HashMap<>();
    params.put("partyName", partyName);
    params.put("hostName", hostName);
    params.put("latitude", latitude);
    params.put("longitude", longitude);
    params.put("user", email);
}

@Override
public Map<String, String> getParams(){
    return params;
}
}

create_party_request.php:

<?php

$con = mysqli_connect("127.0.0.1" , "(my username)" , "(my password)" , "android_api");

$partyName = $_POST["partyName"];
$hostName = $_POST["hostName"];
$latitude = $_POST["latitude"];
$longitude = $_POST["longitude"];
$publicEmail = $_POST["publicEmail"];

global $con, $partyName, $hostName, $latitude, $longitude;
$statement = mysqli_prepare($con, "INSERT INTO parties (party_name, host_name, latitude, longitude, email) VALUES (?, ?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "sssss", $partyName, $hostName, $latitude, $longitude, $publicEmail);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);

$response = array();
$response["success"] = true;

echo json_encode($response);
?>

As a side note, the fields in my sql table are party_name, host_name, latitude, longitude, user.

Here are the errors I'm getting. The log was a bit cut off, but as the title says, there is an undefined index error on the variable publicEmail on line 9 of the php:

errors

1 Answers1

0

Use params.put("publicEmail", email); instead of params.put("user", email); in CreatePartyRequest.java

Rishav Sengupta
  • 241
  • 2
  • 4