php - HttpURLConnection check whether JSON string has been inserted in mysql successfully -
i implemented android code send json string server. getting json string '{"mac": "23:a5:j0:06:c6:e9", "latitude":84.16898451,"longitude":3.16561387,"route": 1}'
output in doinbackground method , getting output output of getresponsecode: the output of getresponsecode: 200
in client side.
i checked php file in localhost , when fresh index.php homepage, bus table being created , data updated if change values in json string. not connect s4 device localhost directly check since using public hotspot.
i have uploaded index.php
file online file manager in htdocs
directory on byethost.com server, table bus being created no record being inserted , updated.
is possible httpurlconnection check whether data has been inserted/updated in bus
table? should add path first parameter of file_get_contents
method else php://input
?
doinbackground
method:
protected void doinbackground(string... params) { // todo auto-generated method stub try { system.out.println("the output of : doinbackground " +params[0]); url myurl = new url("http://username.byethost8.com/index.php"); httpurlconnection conn = (httpurlconnection) myurl .openconnection(); conn.setrequestmethod("post"); conn.setdooutput(true); conn.setconnecttimeout(10000); conn.setreadtimeout(10000); conn.setrequestproperty("content-type", "application/json"); conn.connect(); system.out.println("the output of getresponsecode: "+conn.getresponsecode()); // create data output stream dataoutputstream wr = new dataoutputstream( conn.getoutputstream()); // write output stream string wr.writebytes(params[0]); wr.close(); } catch (ioexception e) { e.printstacktrace(); } return null; }
index.php:
<?php $json = json_decode ( file_get_contents ( 'php://input', true ) ); $con = new mysqli ( "domain.com", "user_name", "password", "database_name" ); // tried string test index.php file , works // $json = '{"mac": "23:a5:j0:06:c6:e9", "latitude":84.16898451,"longitude":3.16561387,"route": 1}'; $data = json_decode ( $json ); $mac = $data->{'mac'}; $latitude = $data->{'latitude'}; $longitude = $data->{'longitude'}; $route = $data->{'route'}; require 'connection.php'; // check whether route's table exist. $results = $con->query ( "show tables 'bus' " ) or die ( mysqli_error () ); if (($results->num_rows) == 1) { //"update myguests set lastname='doe' id=2" $sql = "replace bus(mac, route, latitude, longitude) values( ?, ?, ? , ? )"; $stmt = $con->prepare($sql) or die ( $con->error ); $stmt->bind_param("ssss",$mac,$route, $latitude,$longitude); $stmt->execute(); $stmt->close(); echo "table exist"; } else { $create = "create table bus (id int(11) not null auto_increment primary key, mac varchar(30) not null unique, route int(11) not null, latitude float(10,6) not null , longitude float(10,6) not null, created_at timestamp default current_timestamp)" ; $stmt = $con->prepare($create) or die ( $con->error ); $stmt->execute(); $stmt->close(); echo "table created"; }
some of logcat output:
04-29 22:18:28.541: w/system.err(32348): java.net.protocolexception: cannot write request body after response has been read 04-29 22:18:28.541: w/system.err(32348): @ com.android.okhttp.internal.http.httpurlconnectionimpl.getoutputstream(httpurlconnectionimpl.java:214) 04-29 22:18:28.551: w/system.err(32348): @ com.bustracker.myasynctask.doinbackground(postdata.java:61) 04-29 22:18:28.551: w/system.err(32348): @ com.bustracker.myasynctask.doinbackground(postdata.java:1) 04-29 22:18:28.551: w/system.err(32348): @ android.os.asynctask$2.call(asynctask.java:288) 04-29 22:18:28.551: w/system.err(32348): @ java.util.concurrent.futuretask.run(futuretask.java:237) 04-29 22:18:28.551: w/system.err(32348): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) 04-29 22:18:28.551: w/system.err(32348): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) 04-29 22:18:28.551: w/system.err(32348): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) 04-29 22:18:28.551: w/system.err(32348): @ java.lang.thread.run(thread.java:818) 04-29 22:18:28.561: i/system.out(32348): output of : doinbackground {"mac":"89:gg:d0:06:86:m6","latitude":93.86900553,"longitude":25.66558334,"route":4} 04-29 22:18:28.591: i/system.out(32348): (httplog)-static: issbsettingenabled false 04-29 22:18:28.591: i/system.out(32348): knoxvpnuidstorageknoxvpnsupported api value returned false
you using twice json_decode()
on input.
you have ioexception cannot write request body after response has been read
caused byy statement system.out.println("the output of getresponsecode: "+conn.getresponsecode());
. can not ask response if have noty send yet. or better: cannot write more after statement. put statement after wr.close()
. after read response form conn.getinputstream()
.
Comments
Post a Comment