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

时间:2016-07-10 02:38:07

标签: java php android mysql

所以我正在撰写一个即将上传的应用#34;派对"进入MySQL表。一切都编译并运行顺利,没有错误,但当我检查phpMyAdmin上的表时,没有添加任何内容。有没有人知道为什么会这样?

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 个答案:

答案 0 :(得分:1)

你还没有在你的php中调用registerUser

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