我试图用我的数据库连接我的应用程序。Android - JsonException在httppost上
我做这个脚本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的经验并不多,我不知道在我的代码中是否有一些错误。 有人可以帮助我吗?
感谢
我觉得URL的输出不是JSON。其中存在一些警告或错误消息。请在浏览器中检查后确认。 – 2014-11-03 12:33:33
在浏览器我有这个错误:解析错误:语法错误,在mydb中意外T_VARIABLE/survey.php第20行 线20是插入查询 – Matt 2014-11-03 12:35:03
发表您回应 – 2014-11-03 12:35:10