无法在android studio中将数据插入数据库

时间:2018-05-03 07:17:30

标签: java php android mysql

让我更好地解释一下。我做了一个有3个字段的应用程序。我想将这三个字段插入数据库。我已成功插入数据'吐司中的消息,但值未插入数据库中。即使我没有任何错误..谢谢! 我的php文件:

    <?php

//Define your host here.
$hostname = "localhost";
//Define your database username here.
$username = "root";
//Define your database password here.
$password = "root";
//Define your database name here.
$dbname = "SCPL";

 $con = mysqli_connect($hostname,$username,$password,$dbname);

 $name      = $_POST['name'];
 $email     = $_POST['email'];
 $website   = $_POST['website'];

 $Sql_Query = "insert into scpl (name,email,website) values ('$name','$email','$website')";

 if(mysqli_query($con,$Sql_Query)){

 echo 'Data Inserted Successfully';

 }
 else{

 echo 'Try Again';

 }
 mysqli_close($con);
?>

这是我的mainactivity.java类:

public class MainActivity extends Activity {

EditText editTextName, editTextEmail, editTextWebsite;

String GetName, GetEmail, GetWebsite;

Button buttonSubmit ;

String DataParseUrl = "http://192.168.2.6/androids/insert.php";
//String HttpURL = "http://192.168.2.26/Android_php/gps_tracker/insert.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editTextName = (EditText)findViewById(R.id.editText1);
    editTextEmail = (EditText)findViewById(R.id.editText2);
    editTextWebsite = (EditText)findViewById(R.id.editText3);
    buttonSubmit = (Button)findViewById(R.id.button1);

    buttonSubmit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            GetDataFromEditText();

            SendDataToServer(GetName, GetEmail, GetWebsite);

        }
    });
}

public void GetDataFromEditText(){

    GetName = editTextName.getText().toString();
    GetEmail = editTextEmail.getText().toString();
    GetWebsite = editTextWebsite.getText().toString();

}

public void SendDataToServer(final String name, final String email, final  String website){
    class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {

            String QuickName = name ;
            String QuickEmail = email ;
            String QuickWebsite = website;

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            nameValuePairs.add(new BasicNameValuePair("name", QuickName));
            nameValuePairs.add(new BasicNameValuePair("email", QuickEmail));
            nameValuePairs.add(new BasicNameValuePair("website", QuickWebsite));

            try {
                HttpClient httpClient = new DefaultHttpClient();

                HttpPost httpPost = new HttpPost(DataParseUrl);

                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = httpClient.execute(httpPost);

                HttpEntity entity = response.getEntity();

            } catch (ClientProtocolException e) {

            } catch (IOException e) {

            }
            return "";
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            Toast.makeText(MainActivity.this, "Data Submit Successfully", Toast.LENGTH_LONG).show();

        }
    }
    SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
    sendPostReqAsyncTask.execute(name, email,website);
}

}

2 个答案:

答案 0 :(得分:1)

我没有发现任何错误/错误,请尝试将您的URL / IP地址更改为: 使用10.0.2.2作为默认AVD,使用10.0.3.2作为genymotion。

答案 1 :(得分:0)

尝试使用$_REQUEST代替$_POST并在onPostExecute函数中显示成功消息之前先检查服务器的响应,如@MJM建议的那样。

当我将数据作为POST请求发送时,我的API和Android应用程序出现了类似问题,获取提交数据的唯一方法是使用$_REQUEST