我正在制作一个示例应用程序,我可以将数据添加到使用WAMP服务器的在线(localhost)数据库中,我编写了一个HTML表单代码,将数据插入数据库以检查数据是否插入到表中很成功。 所以我为android编写了一个示例代码,使用android插入数据,我没有强制关闭,没有错误,但数据没有插入 这是我的所有代码
Form.Html
<html>
<body>
<form action = "submit.php" method="post">
<fieldset >
Question : <br>
<textarea type="text" name="question" style="height: 100px ;width:800px"> </textarea> <br><br>
Option 1 : <br>
<input type="text" name="option1" style="height: 50px ;width:800px"> <br><br>
Option 2 : <br>
<input type="text" name="option2" style="height: 50px ;width:800px"> <br><br>
Option 3 : <br>
<input type="text" name="option3" style="height: 50px ;width:800px"> <br><br>
Option 4 : <br>
<input type="text" name="option4" style="height: 50px ;width:800px"> <br><br>
Answer :<br>
<input type="text" name="answer" style="height: 50px ;width:800px"><br><br>
<input type="submit" value="Submit" style="height: 40px ;width:800px">
</fieldset>
</form>
</body>
</html>
PHPScript
<?php
//
$response = array();
if(!empty($_POST['question']) && !empty($_POST['option1']) && !empty($_POST['option2']) && !empty($_POST['option3']) && !empty($_POST['option4']) && !empty($_POST['answer'])){
$question = $_POST['question'];
$option1 = $_POST['option1'];
$option2 = $_POST['option2'];
$option3 = $_POST['option3'];
$option4 = $_POST['option4'];
$answer = $_POST['answer'];
// import db connection variables
require_once __DIR__ . '/db_config.php';
// connecting to mysql
$db= new mysqli(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE);
// query for insertion
$insertQuery = "INSERT INTO `questions`(Question, Option1, Option2, Option3, Option4, Answer) VALUES ('$question','$option1','$option2','$option3','$option4','$answer')";
// check
if ($db->query($insertQuery) == TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $insertQuery . "<br>" . $db->error;
}
$db->close();
}else{
// import db connection variables
require_once __DIR__ . '/db_config.php';
// connecting to mysql
$db= new mysqli(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE);
// query for insertion
$insertQuery = "INSERT INTO `questions`(Question, Option1, Option2, Option3, Option4, Answer) VALUES ('error','error','error','error','error','error')";
// check
if ($db->query($insertQuery) == TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $insertQuery . "<br>" . $db->error;
}
$db->close();
}
?>
MainActivity.Java
package library.danaraddi.com.myapplication;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends ActionBarActivity {
private HttpClient httpClient;
private HttpPost httpPost;
private HttpResponse httpResponse;
private List<NameValuePair> nameValuePairsList;
private ProgressDialog pDialog;
// JSON parser class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// importing button
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new SubmitData().execute();
}
});
}
/**
* Async task class to get json by making HTTP call
*/
class SubmitData extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
httpClient = new DefaultHttpClient();
httpPost = new HttpPost("http://192.168.1.2/CPSScripts/submit");
nameValuePairsList = new ArrayList<NameValuePair>();
nameValuePairsList.add(new BasicNameValuePair("question", "question"));
nameValuePairsList.add(new BasicNameValuePair("option1", "option1"));
nameValuePairsList.add(new BasicNameValuePair("option2", "option2"));
nameValuePairsList.add(new BasicNameValuePair("option3", "option3"));
nameValuePairsList.add(new BasicNameValuePair("option4", "option4"));
nameValuePairsList.add(new BasicNameValuePair("answer", "answer"));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairsList));
httpResponse = httpClient.execute(httpPost);
Log.i("Response", httpResponse.toString());
} catch (UnsupportedEncodingException e) {
Toast.makeText(MainActivity.this, "exception" + e.toString(), Toast.LENGTH_SHORT).show();
} catch (ClientProtocolException e) {
Toast.makeText(MainActivity.this, "exception" + e.toString(), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(MainActivity.this, "exception" + e.toString(), Toast.LENGTH_SHORT).show();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
}
}
}
PS:我使用过.php而且没有扩展但是结果相同,并在我的清单中发布了互联网权限
这是我的Log.d
01-24 02:52:02.123 1925-1952/library.danaraddi.com.myapplication I/Response﹕ org.apache.http.message.BasicHttpResponse@b30ee4d0