所以我在Android studio中创建了几个editText字段,每个字段通过php连接到mysql(我使用的是xampp btw)。现在我的问题是,**它在将一个“{1}}从editText视图更改为2 RadioButton(即tbuyer
)之后返回一个”必填字段丢失“** x。
现在为了从这两个字符串中获取字符串,我使用RadioGroup声明了r_tb; r_ntb
条件:
String role=" ";
然后将它传递给将发送到哈希映射的参数:
if (radioGroup.getCheckedRadioButtonId() == r_tb.getId())
{
role = "Trade Buyer";
}
else if (radioGroup.getCheckedRadioButtonId() == r_ntb.getId())
{
role = "Non Trade Buyer";
}
然后调用json的字符串请求,以便我们知道注册是否成功。但正如我所说,它返回**必填字段缺失“
这些是 Buyer_Registration_Activity.java 的代码:
@Override
protected Map<String, String> getParams() throws AuthFailureError { //put all parameters that will be sent to hash map
Map<String, String> params = new HashMap<>();
params.put("cname", cname); //String is converted to final, because we'are using it in a class; refer to line 48-50
params.put("date", date);
params.put("lname", lname);
params.put("fname", fname);
params.put("mi", mi);
params.put("email", email);
params.put("city", city);
params.put("country", country);
params.put("tbuyer", role); // check code; String created from radio button
return params;
}
这些是 DBOperations.php 的代码(Crud操作声明)
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.smdojt.manilafame.R;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class Buyer_Registration_Activity extends AppCompatActivity implements View.OnClickListener{
private EditText editTextCompanyName, editTextDate, editTextLastName, editTextFirstName, editTextMiddleInitial
, editTextCity, editTextCountry, editTextType, editTextEmail;
private Button buttonRegister;
private ProgressDialog progressDialog;
String role = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buyer_registration);
//this.setTitle("Buyer Registration");
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
myToolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
editTextCompanyName = (EditText) findViewById(R.id.editTextCompanyName);
editTextDate = (EditText) findViewById(R.id.editTextDate);
editTextLastName = (EditText) findViewById(R.id.editTextLastName);
editTextFirstName = (EditText) findViewById(R.id.editTextFirstName);
editTextMiddleInitial = (EditText) findViewById(R.id.editTextMiddleInitial);
editTextCity = (EditText) findViewById(R.id.editTextCity);
editTextType = (EditText) findViewById(R.id.editTextType);
editTextCountry = (EditText) findViewById(R.id.editTextCountry);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
buttonRegister = (Button) findViewById(R.id.buttonRegister);
progressDialog = new ProgressDialog(this);
buttonRegister.setOnClickListener(this);
}
private void registerUser(){
final String cname = editTextCompanyName.getText().toString().trim();
final String date = editTextDate.getText().toString().trim();
final String lname = editTextLastName.getText().toString().trim();
final String fname = editTextFirstName.getText().toString().trim();
final String mi = editTextMiddleInitial.getText().toString().trim();
final String email = editTextEmail.getText().toString().trim();
final String city = editTextCity.getText().toString().trim();
final String country = editTextCountry.getText().toString().trim();
final String tbuyer = editTextType.getText().toString().trim();
final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
final RadioButton r_tb = (RadioButton) findViewById(R.id.radio_tb);
final RadioButton r_ntb = (RadioButton) findViewById(R.id.radio_ntb);
if (radioGroup.getCheckedRadioButtonId() == r_tb.getId())
{
role = "Trade Buyer";
}
else if (radioGroup.getCheckedRadioButtonId() == r_ntb.getId())
{
role = "Non Trade Buyer";
}
progressDialog.setMessage("Registering User...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST,
com.example.smdojt.manilafame.sql_demo_2.Constants.URL_REGISTER,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {//If there are no ERROR this method will be executed
progressDialog.dismiss();
//we will get the json object
try {
JSONObject jsonObject = new JSONObject(response); //create json object
Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) { // Else, this method will be executed
progressDialog.hide();
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError { //put all parameters that will be sent to hash map
Map<String, String> params = new HashMap<>();
params.put("cname", cname); //String is converted to final, because we'are using it in a class; refer to line 48-50
params.put("date", date);
params.put("lname", lname);
params.put("fname", fname);
params.put("mi", mi);
params.put("email", email);
params.put("city", city);
params.put("country", country);
params.put("tbuyer", role); // check code; String created from radio button
//params.put("tbuyer", tbuyer);
return params;
}
};
//add stringRequest (Line 55)
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
@Override
public void onClick(View view) {
if (view==buttonRegister)
{
registerUser();
}
}
}
这些是 registerBuyer.php 的代码(其中我的所有错误陈述都来自包括“必需的字段缺失”)
<?php
//manage all php and db operations
class DbOperations
{
private $con;
function __construct()
{
require_once dirname(__FILE__).'/DbConnect.php'; //import dbconnect to dboperations
$db = new DbConnect(); //create db connect object
$this->con = $db->connect();
}
//CRUD Operations below
//Create-----------------------------------------------------------------------------------------------
function registerBuyer($cname, $date, $lname, $fname, $mi, $email, $country, $city, $tbuyer)
{
//$password = md5($password); //encrypt password
$stmt = $this->con->prepare("INSERT INTO `mfmis_buyers`.`buyers` (`id`, `cname`, `date`, `lname`, `fname`, `mi`, `email`, `country`, `city`, `tbuyer`) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?);"); //statement
//find actual parameters /bind param
$stmt->bind_param("sssssssss", $cname, $date, $lname, $fname, $mi, $email, $country, $city, $tbuyer); //bind to query
//execute query below
if ($stmt->execute()) //as soon as this line is called, data will be inserted into DB
{
return true;
}
else
{
return false;
}
}
//-----------------------------------------------------------------------------------------------------
}
?>
答案 0 :(得分:0)
因此,基于role
字符串,它甚至不会推送单个数据,因为首先的连接甚至都不正确。
因此,我上面提到的代码正在运行,唯一阻碍的是我的 Constants.java 。确保已设置本地或在线连接并与xampp服务器保持一致。
public class Constants {
public static final String ROOT_URL = "http://192.168.15.186/MFMIS/register/";
public static final String URL_REGISTER = ROOT_URL+"registerBuyer.php";
}
确保您的String ROOT_URL
和URL_REGISTER
或任何其他本地和在线连接正确无误。否则,如果问题仍然存在,那么您的活动或xampp方面就会出现问题。