如何在Android中使用HttpPost将数据库发送到服务器并检查

时间:2015-07-11 08:28:16

标签: android mysql wampserver

我正在使用此代码(我从教程中复制)将一些信息发送到MySQL中的数据库。在Android应用程序中:

class UploadToDatabase extends AsyncTask<String, String, String> {
    List<NameValuePair> nameValuePairs= new ArrayList<NameValuePair>(1);
    //Metemos valores al database del Server

    @Override
    protected String doInBackground(String... params) {
        nameValuePairs.add(new BasicNameValuePair("_id", "2"));
        nameValuePairs.add(new BasicNameValuePair("COLUMNname", params[0]));
        nameValuePairs.add(new BasicNameValuePair("COLUMNimage", params[1]));
        nameValuePairs.add(new BasicNameValuePair("COLUMNsound", "tercero"));
    try{
        //JSONObject json = jsonParser.makeHttpRequest(url_create_product,
          //      "POST", params);
        HttpClient httpClient= new DefaultHttpClient();

        HttpPost httpPost= new HttpPost("http://192.168.1.35/AndroidFileUpload/UpData.php");
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response= httpClient.execute(httpPost);

        HttpEntity entity=response.getEntity();

        is=entity.getContent();
        //Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_LONG).show();
    } catch(ClientProtocolException e) {
        Log.e("ClientProtocol","Log_Tag");
        e.printStackTrace();
    } catch(IOException e){
        Log.e("Log Tag", "IOException");
        e.printStackTrace();
    }
    catch(Exception es){
        Log.e("Fallo de excep", "IOException");
        es.printStackTrace();
    }
    return null;
    }

在wampserver中我使用这个php代码:

<?
$DBhost = "localhost";
$DBuser = "xxxxx";
$DBpass = "xxxxx";
$DBName = "dbsound";
$table = "dbsound";
$conn = new mysqli_connect($DBhost,$DBuser,$DBpass,$DBName) 

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$_id=$_POST['_id'];
$COLUMNname=$_POST['COLUMNname'];
$COLUMNimage=$_POST['COLUMNimage'];
$COLUMNsound=$_POST['COLUMNsound'];

mysqli_select_db("$DBName") or die("Unable to select database $DBName");

$sqlquery = "INSERT INTO $table ("_id", "COLUMNname", "$COLUMNimage","COLUMNsound") VALUES(NULL,'$COLUMNname','$COLUMNimage','$COLUMNsound')";

$results = mysqli_query($sqlquery);

if ($conn->query($sqlquery) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sqlquery . "<br>" . $conn->error;
}

$conn->close();

?>

在DBuser和DBpass中我把MySQL的数据放在哪里。 我没有任何错误,但它在数据库中没有出现任何内容,因此我怎么知道数据是否已发送?

0 个答案:

没有答案