我只是尝试使用php将数据从android代码插入到mysql数据库,但代码没有做任何事情。
register.php:
<?php
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['n']))
{
$name = $_POST['n'];
$address = $_POST['add'];
//$age = $_POST['age'];
$phone = $_POST['phone'];
$email = $_POST['e'];
$bloodgroup = $_POST['bloodgroup'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("Insert into bloodtb(Name,Address,Phone,Email,Bloodgroup) values
('$name','$address','$phone','$email','$bloodgroup')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
register.java:
public class register extends Activity
{
EditText name,address,bg,ph,email,age;
Button register;
private ProgressDialog pDialog;
int flag=0;
JSONParser jsonParser = new JSONParser();
private static String url = "http://192.168.43.41/phpfiles/register.php";
private static final String TAG_SUCCESS = "success";
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
name=(EditText)findViewById(R.id.etname);
// age=(EditText)findViewById(R.id.etage);
address=(EditText)findViewById(R.id.etadd);
bg=(EditText)findViewById(R.id.etbg);
email=(EditText)findViewById(R.id.etemail);
ph=(EditText)findViewById(R.id.etphone);
register=(Button)findViewById(R.id.btnreg);
register.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view){
new Thread(new Runnable() {
@Override
public void run() {
new loginAccess().execute(); }
});
}
});
}
class loginAccess extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(register.this);
pDialog.setMessage("Registering..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... arg0) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
String n=name.getText().toString();
//String a=age.getText().toString();
String add=address.getText().toString();
String e=email.getText().toString();
String phone=ph.getText().toString();
String bloodgroup=bg.getText().toString();
params.add(new BasicNameValuePair("n", n));
// params.add(new BasicNameValuePair("age", a));
params.add(new BasicNameValuePair("add",add));
params.add(new BasicNameValuePair("phone",phone));
params.add(new BasicNameValuePair("e", e));
params.add(new BasicNameValuePair("bloodgroup",bloodgroup));
JSONObject json = jsonParser.makeHttpRequest(url,"POST", params);
Log.d("Create Response", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1)
{
flag=0;
//Intent i = new Intent(getApplicationContext(),search.class);
/*i.putExtra("mobile_number",number);
i.putExtra("password",pwd);*/
//startActivity(i);
finish();
}
else
{
// failed to Sign in
flag=1;
}
} catch (JSONException ex) {
ex.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
if(flag==1)
Toast.makeText(register.this,"Please Enter Correct informations", Toast.LENGTH_LONG).show();
}
}
}
日志cat中没有错误,但仍未将任何内容插入数据库..请帮助。
答案 0 :(得分:0)
您没有启动该线程,因此您的代码根本不会执行
new Thread(new Runnable()
{
@Override
public void run()
{
new loginAccess().execute();
}
}).start();
AsyncTask也在不同的线程上执行,所以即使在代码下面也应该执行任务
new loginAccess().execute();