Reputation: 49
i'm trying to connect my app with my db.
I make this script 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();
?>
and this is my Java code:
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();
}
when i sent the data i have this error:
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
i have not many experience with php and i don't know if there is some error in my code. can someone help me?
Thanks
Upvotes: 1
Views: 107
Reputation: 45490
$row = mysqli_fetch_array($result);
this is an insert nothing to fetch ! not to mention you are mixing
mysqli and mysql extensionecho $data;
is not JSON it will break your app since you
atttempt to parse json data.$Vote = $_POST['Vote'];
if(!$result){
echo json_encode(array('success'=> 0));
}else{
echo json_encode(array('success'=> 1));
}
Upvotes: 1
Reputation: 527
change
echo $data
to echo json_encode($data);
add a semicolon at the end of this row
$Vote = $_POST['Vote'] ;
Upvotes: 1
Reputation: 427
Change this: VALUES ($idMuseum, $idGame, $Vote);");
to VALUES ($idMuseum, $idGame, $Vote)");
echo json_encode($data);
Upvotes: 0
Reputation: 291
You need to change your php response to like this echo Json_encode($data); because it is sending the plane html data that why you are getting JSONEXCEPTION
Upvotes: 0
Reputation: 2866
Your server is mostly passing back HTML to the app which then trying to parse it fails. As the error says the response starts with br. Please check if your getting proper response from your server
Upvotes: 0