2016-07-12 142 views
-1

所以我試圖上傳一些數據到MySQL。我相信我有效地將LoginActivity.java中的電子郵件的價值傳遞給了PartySetup.java,但是這個值顯然在這個過程中丟失了。我試着用相同的結果使用sharedPreferences。有沒有人有任何想法,爲什麼發生這種情況?謝謝!Android - 獲取未定義的索引JSONException,但我不知道爲什麼

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); 
?> 

請注意,我的sql表中的字段是party_name,host_name,緯度,經度,用戶。

這是我收到的錯誤。該日誌已有點切斷,但正如標題所說,對變量publicEmail一個未定義的索引錯誤在PHP的第9行:

errors

+0

哪一行你得到錯誤? – Sush

+0

對不起 - 錯誤是在線9 –

+0

發佈錯誤日誌 – Sush

回答

0

使用params.put(「publicEmail」,電子郵件); 而不是params.put(「user」,email);在CreatePartyRequest.java

相關問題