Sunday, 15 February 2015

json - Android - JsonException on httppost -



json - Android - JsonException on httppost -

i'm trying connect app db.

i create 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 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 `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 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; seek { 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")); // json object using post method json = jparser.makehttprequest(url_survey, "post", parametridainviare); seek { 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 grab block e.printstacktrace(); updatesav = false; } } if(checkedbutton == r.id.mediumchoise){ //do parametridainviare.add(new basicnamevaluepair("vote", "2")); parametridainviare.add(new basicnamevaluepair("museum_id", "1")); parametridainviare.add(new basicnamevaluepair("game_id", "1")); // json object using post method json = jparser.makehttprequest(url_survey, "post", parametridainviare); seek { 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 grab block e.printstacktrace(); updatesav = false; } } if(checkedbutton == r.id.highchoise){ //do parametridainviare.add(new basicnamevaluepair("vote", "3")); parametridainviare.add(new basicnamevaluepair("museum_id", "1")); parametridainviare.add(new basicnamevaluepair("game_id", "1")); // json object using post method json = jparser.makehttprequest(url_survey, "post", parametridainviare); seek { 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 grab block e.printstacktrace(); updatesav = false; } } } grab (exception e) { // todo auto-generated grab block // da implementare } homecoming null; } protected void onpostexecute(string file_url) { // dismiss dialog after getting info pdialog.dismiss(); }

when sent info have error:

11-03 13:20:55.918: e/json parser(11238): error parsing info org.json.jsonexception: value <br of type java.lang.string cannot converted jsonobject

i have not many experience php , don't know if there error in code. can help me?

thanks

change echo $data

to echo json_encode($data);

add semicolon @ end of row

$vote = $_post['vote'] ;

android json

No comments:

Post a Comment