dsgawmla208057
2016-07-10 02:38
浏览 55

Android - 向SQL添加数据无所作为

So I'm writing an app that's going to be uploading "parties" into a MySQL table. Everything compiles and runs smoothly with no errors, but when I check the table on phpMyAdmin, nothing has been added. Does anyone have any idea of why this is happening?

PartySetup.java:

package example.com.musicapptest;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;

import org.json.JSONException;
import org.json.JSONObject;

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

                    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, 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:

package example.com.musicapptest;

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Carter Klein on 7/9/2016.
 */
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, 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);
}

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

create_party_request.php:

<?php

  $con = mysqli_connect("127.0.0.1" , "(not real username)" , "(not real password - this part works 100% sure)" , "android_api");

//Check connection
if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();

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

function registerUser(){
  global $con, $partyName, $hostName, $latitude, $longitude;
  $statement = mysqli_prepare($con, "INSERT INTO parties (partyName, hostName, latitude, longitude) VALUES (?, ?, ?, ?)");
  mysqli_stmt_bind_param($statement, "ssss", $partyName, $hostName, $latitude, $longitude);
  mysqli_stmt_execute($statement);
  mysqli_stmt_close($statement);
}

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

echo json_encode($response);
?>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongwuzun4630 2016-07-10 03:21
    已采纳

    you haven't called registerUser in your php

    <?php
    
    $con = mysqli_connect("127.0.0.1" , "(not real username)" , "(not real                                                          password - this part works 100% sure)" , "android_api");
    
    //Check connection
    if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();
    
    $partyName = $_POST["partyName"];
    $hostName = $_POST["hostName"];
    $latitude = $_POST["latitude"];
    $longitude = $_POST["longitude"];
    
    function registerUser(){
      global $con, $partyName, $hostName, $latitude, $longitude;
      $statement = mysqli_prepare($con, "INSERT INTO parties (partyName,        hostName, latitude, longitude) VALUES (?, ?, ?, ?)");
    mysqli_stmt_bind_param($statement, "ssss", $partyName, $hostName, $latitude, $longitude);
    mysqli_stmt_execute($statement);
    mysqli_stmt_close($statement);
     }
    registerUser();
    $response = array();
    $response["success"] = true;
    
    echo json_encode($response);
    ?>
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题