我正在尝试将我的应用与我的数据库连接。
我制作这个脚本php:
<?php
/*ci colleghiamo al database(attenti perchè se lavorate in locale
l'host è 10.0.2.2 e non 127.0.0.1)*/
mysql_connect("localhost","mydb","")
or die("Impossibile connettersi al server MySQL.\n");
//selezioniamo il db a cui ci vogliamo connettere
mysql_select_db("mydb")
or die("Impossibile aprire il database.\n");
//creo la tabella nel database
mysql_query("CREATE TABLE IF NOT EXISTS `mydb`.`survey` (
`Museum_ID` INT NOT NULL,
`Game_ID` INT NOT NULL,
`Vote` INT NOT NULL,
PRIMARY KEY (`Museum_ID`))
ENGINE = InnoDB;");
$idMuseum = $_POST['Museum_ID'];
$idGame = $_POST['Game_ID'];
$Vote = $_POST['Vote']
$result = mysql_query("INSERT INTO `mydb`.`survey` (`Museum_ID`, `Game_ID`, `Vote`) VALUES ($idMuseum, $idGame, $Vote);");
$row = mysqli_fetch_array($result);
$data = $row[0];
if($data){
echo $data;
mysql_close();
?>
这是我的Java代码:
public class MakePost extends AsyncTask<String, Void, Void>{
@Override
protected void onPreExecute() {
super.onPreExecute();
//toggleUI(0);
pDialog = new ProgressDialog(Survey.this);
pDialog.setMessage("Sending data. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected Void doInBackground(String... args) {
List<NameValuePair> parametriDaInviare = new ArrayList<NameValuePair>(2);
JSONObject json = null;
int success = -1;
updatesAv = false;
try {
if(checkedButton == R.id.lowChoise){
//aggiungo alla lista parametri il voto
parametriDaInviare.add(new BasicNameValuePair("Vote", "1"));
parametriDaInviare.add(new BasicNameValuePair("Museum_ID", "1"));
parametriDaInviare.add(new BasicNameValuePair("Game_ID", "1"));
// get JSON Object by using POST method
json = jParser.makeHttpRequest(url_Survey, "POST", parametriDaInviare);
try
{
Log.d("PHP Response", json.toString());
success = json.getInt(TAG_SUCCESS);
if(success == 1)
{
popupWindow.dismiss();
updatesAv =true;
}
else {
updatesAv = false;
}
Log.d("UPDATES CHECK", "RESULT: " + updatesAv);
}catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
updatesAv = false;
}
}
if(checkedButton == R.id.mediumChoise){
//do something
parametriDaInviare.add(new BasicNameValuePair("Vote", "2"));
parametriDaInviare.add(new BasicNameValuePair("Museum_ID", "1"));
parametriDaInviare.add(new BasicNameValuePair("Game_ID", "1"));
// get JSON Object by using POST method
json = jParser.makeHttpRequest(url_Survey, "POST", parametriDaInviare);
try
{
Log.d("PHP Response", json.toString());
success = json.getInt(TAG_SUCCESS);
if(success == 1)
{
popupWindow.dismiss();
updatesAv =true;
}
else {
updatesAv = false;
}
Log.d("UPDATES CHECK", "RESULT: " + updatesAv);
}catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
updatesAv = false;
}
}
if(checkedButton == R.id.highChoise){
//do something
parametriDaInviare.add(new BasicNameValuePair("Vote", "3"));
parametriDaInviare.add(new BasicNameValuePair("Museum_ID", "1"));
parametriDaInviare.add(new BasicNameValuePair("Game_ID", "1")); // get JSON Object by using POST method
json = jParser.makeHttpRequest(url_Survey, "POST", parametriDaInviare);
try
{
Log.d("PHP Response", json.toString());
success = json.getInt(TAG_SUCCESS);
if(success == 1)
{
popupWindow.dismiss();
updatesAv =true;
}
else {
updatesAv = false;
}
Log.d("UPDATES CHECK", "RESULT: " + updatesAv);
}catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
updatesAv = false;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
// da implementare
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all data
pDialog.dismiss();
}
当我发送数据时,我有这个错误:
11-03 13:20:55.918: E/JSON Parser(11238): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
我对PHP的经验不多,我不知道我的代码中是否有错误。 有人能帮我吗?
由于
答案 0 :(得分:1)
变化
echo $ data
要 echo json_encode($ data);
在此行的末尾添加分号
$ Vote = $ _POST ['投票'];
答案 1 :(得分:1)
$row = mysqli_fetch_array($result);
这是一个无需取的插入!更不用说你在混音
mysqli和mysql扩展echo $data;
不是JSON,它会破坏你的应用程序
尝试解析json数据。$Vote = $_POST['Vote'];
if(!$result){
echo json_encode(array('success'=> 0));
}else{
echo json_encode(array('success'=> 1));
}
答案 2 :(得分:0)
您的服务器主要是将HTML传回给应用程序,然后尝试解析它失败。正如错误所述,响应以br开头。请检查您的服务器是否收到了正确的回复
答案 3 :(得分:0)
你需要将你的php响应更改为这样 echo Json_encode($ data); 因为它正在发送飞机html数据,说明你获得JSONEXCEPTION的原因
答案 4 :(得分:0)
更改此内容:VALUES ($idMuseum, $idGame, $Vote);");
到 VALUES ($idMuseum, $idGame, $Vote)");
echo json_encode($data);