我是一个新的Android开发人员,我正在使用Android Studio开发,我想通过使用php文件将数据插入MySQL
Java代码是:
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Cus_Register extends AppCompatActivity {
Button submit;
EditText userName, pwd, fName, lName, phone, email;
String uName, pass, F_Name, L_Name, mail;
int mobile;
JSONParser jsonParser;
ProgressDialog progressDialog;
int value;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cus__register);
submit = (Button) findViewById(R.id.subCusRegBtn);
userName = (EditText) findViewById(R.id.userRegEditText);
pwd = (EditText) findViewById(R.id.pwdEditText);
fName = (EditText) findViewById(R.id.firstName_RegEditText);
lName = (EditText) findViewById(R.id.lastName_RegEditText);
phone = (EditText) findViewById(R.id.phone_reg_EditText);
email = (EditText) findViewById(R.id.email_reg_EditText);
jsonParser = new JSONParser();
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uName = userName.getText().toString();
pass = pwd.getText().toString();
F_Name = fName.getText().toString();
L_Name = lName.getText().toString();
Toast.makeText(Cus_Register.this,pass, Toast.LENGTH_SHORT).show();
mobile = Integer.parseInt(phone.getText().toString());
mail = email.getText().toString();
new AddCustomerReg().execute();
}
});
}
class AddCustomerReg extends AsyncTask<String, String, String> {
//Before Connection
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(Cus_Register.this);
progressDialog.setTitle("On Progress...");
progressDialog.show();
}
//After Connection
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (value == 1) {
Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Error is there...", Toast.LENGTH_LONG).show();
}
progressDialog.dismiss();
}
//While the Connection
@Override
protected String doInBackground(String... params) {
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("userName", uName));
list.add(new BasicNameValuePair("Password", pass));
list.add(new BasicNameValuePair("fname", F_Name));
list.add(new BasicNameValuePair("lname", L_Name));
list.add(new BasicNameValuePair("phone", phone+""));
list.add(new BasicNameValuePair("email", mail));
JSONObject jsonObject = jsonParser.makeHttpRequest("http://192.168.100.15/we_deliver/add_cus.php", "POST", list);
try {
if (jsonObject != null && !jsonObject.isNull("Value")) {
System.out.println("Here");
value = jsonObject.getInt("Value");
} else {
System.out.println("Else Here");
value = 0;
}
} catch (Exception ex) {
ex.getStackTrace();
}
return null;
}
}
}
此错误始终显示
06-13 13:26:38.430 10768-28236/zoghbi.abdulrahman.we_deliver E/Buffer Error: Error converting result java.lang.NullPointerException: lock == null
06-13 13:26:38.430 10768-28236/zoghbi.abdulrahman.we_deliver E/JSON Parser: Error parsing data org.json.JSONException: End of input at character 0 of
这是我的PHP文件
<?php
require_once ("db_connect.php");
$response = array();
if(isset($_POST['userName'])){
$user_name = $_POST['userName'];
$pwd = $_POST['Password'];
$fname = $_POST['fname'];
$lname= $_POST['lname'];
$phone= $_POST['phone'];
$email= $_POST['email'];
$db = new DB_Connect();
mysql_query("SET NAMES utf8");
$query = mysqli_query($db,"insert into customer (userName, password,fname,lname,password,phone,email) values
('$user_name', '$pwd', '$fname','$lname',$phone,'$email')");
if($query){
$response['Value'] = 1;
}else{
$response['Value'] = 0;
}
}else{
$response['Value'] = -1;
}
echo json_encode($response);
?>
代码转到jSonObject else并在这里打印Else我提到知道错误在哪里
答案 0 :(得分:0)
只是一个业余问题,你是否尝试直接创建你的JsonParser
JSONParser jsonParser = new JSONParser();
或者可以将其public