android - cannot send data to database of web server -
when directly go link of php browser, creates empty record in database. however, when send data using app, fails.
java:
protected string doinbackground(string... params) { string reg_url = "http://www.minigameserver.square7.ch/register.php"; string user_name = params[0]; string user_ac = params[1]; string user_pw = params[2]; try { url url = new url(reg_url); httpurlconnection httpurlconnection = (httpurlconnection) url.openconnection(); httpurlconnection.setrequestmethod("post"); httpurlconnection.setdooutput(true); outputstream os = httpurlconnection.getoutputstream(); bufferedwriter bufferedwriter = new bufferedwriter(new outputstreamwriter(os,"utf-8")); string data = urlencoder.encode("user_name","utf-8")+"="+urlencoder.encode(user_name,"utf-8")+"&"+ urlencoder.encode("user_ac","utf-8")+"="+urlencoder.encode(user_ac,"utf-8")+"&"+ urlencoder.encode("user_pw","utf-8")+"="+urlencoder.encode(user_pw,"utf-8"); bufferedwriter.write(data); bufferedwriter.flush(); bufferedwriter.close(); os.close(); httpurlconnection.disconnect(); return "done!"; } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return "fail!"; }
when run app, massage "done!" shown.
so, there no exception happening. but, why there no new record?
i saw have tired register username of teset. mind tell did can trace post.
when try code provided arshak. following error comes out. e/surface: getslotfrombufferlocked: unknown buffer: 0x7f7d9eaad530
register.php:
<?php require "db_connect.php"; $user_name = $_post["user_name"]; $user_ac = $_post["user_ac"]; $user_pw = $_post["user_pw"]; $user_pw_enc = md5($user_pw); $sql = "insert user(user_name, user_ac, user_pw) values ('$user_name', '$user_ac', '$user_pw_enc')"; $sql2 = "select * user user_name = '$user_name'"; $result2 = $conn->query($sql2); $sql3 = "select * user user_ac = '$user_ac'"; $result3 = $conn->query($sql3); if($result2->num_rows == 0 && $result3->num_rows == 0){ if ($conn->query($sql) === true) echo "your account created successfully!\nyou can login now!"; else echo "error: " . $sql . "<br>" . $conn->error; } else { if($result2->num_rows > 0) echo "the user name used!<br>"; if($result3->num_rows > 0) echo "the user account used!<br>"; } ?>
i have updated code, see if data gets inserted:
protected string doinbackground(string... params) { string reg_url = "http://www.minigameserver.square7.ch/register.php"; string user_name = params[0]; string user_ac = params[1]; string user_pw = params[2]; try { url url = new url(reg_url); httpurlconnection httpurlconnection = (httpurlconnection) url.openconnection(); httpurlconnection.setrequestmethod("post"); httpurlconnection.setdooutput(true); httpurlconnection.setdoinput(true); httpurlconnection.connect(); dataoutputstream os = new dataoutputstream(httpurlconnection.getoutputstream()); string data = urlencoder.encode("user_name","utf-8")+"="+urlencoder.encode(user_name,"utf-8")+"&"+ urlencoder.encode("user_ac","utf-8")+"="+urlencoder.encode(user_ac,"utf-8")+"&"+ urlencoder.encode("user_pw","utf-8")+"="+ urlencoder.encode(user_pw,"utf-8"); os .writebytes(data); os .flush(); os.close(); int responsecode=httpurlconnection.getresponsecode(); string messagefromserver; if (responsecode==200) { messagefromserver=frominputstream(httpurlconnection.getinputstream()); log.v("registrationmessage ",messagefromserver); httpurlconnection.disconnect(); return messagefromserver; } httpurlconnection.disconnect(); } catch (ioexception e) { e.printstacktrace(); } return "fail!"; } //add method inside asynctask public string frominputstream(inputstream stream) throws ioexception { inputstreamreader streamreader = new inputstreamreader(stream); bufferedreader bufferedreader = new bufferedreader(streamreader); stringbuilder builder = new stringbuilder(); string tempstring = ""; while ((tempstring = bufferedreader.readline()) != null) { builder.append(tempstring); } return builder.tostring(); }
Comments
Post a Comment