android - how to pass date to server using HttpURLConnection? -
myphp script on server side <?php // include db connect class require_once __dir__ . '/db_connect.php'; // connecting db $db = new db_connect(); $tvshowname = (isset($_post['username']) ? $_post['username'] : null); $result=mysql_query("insert test(imagename) values ('$tvshowname')"); $target_path1 = "http://" + "10.0.2.2/tvshow/tvshow_images/"; //$target_path1 = "10.0.2.2/tvshow/tvshow_images/"; //$target_path1 = 'http://10.0.2.2/tvshow/tvshow_images/'; /* add original filename our target path. result "uploads/filename.extension" */ $target_path1 = $target_path1 . basename( $_files['uploaded_file']['name']); if(move_uploaded_file($_files['uploaded_file']['tmp_name'], $target_path1)) { echo "the first file ". basename( $_files['uploaded_file']['name']). " has been uploaded."; } else{ echo "there error uploading file, please try again!"; echo "filename: " . basename( $_files['uploaded_file']['name']); echo "target_path: " .$target_path1; } ?> @suppresswarnings("deprecation") @override protected string doinbackground(string... f_url) { httpurlconnection connection = null; dataoutputstream outputstream = null; string pathtoourfile = uploadfilepath; string urlserver = "http://" + "10.0.2.2" + "/tvshow/upload_tvshows.php?username=" +"niranga"+""; //"&tvshowchannel="+"sirasa tv"+"&tvshowtype=" //+ "dailytelegrama" + "&tvshowdescription="+"nvnnnnnnnnnnn"; string lineend = "\r\n"; string twohyphens = "--"; string boundary = "*****"; int bytesread, bytesavailable, buffersize; byte[] buffer; int maxbuffersize = 1 * 1024 * 1024; try { fileinputstream fileinputstream = new fileinputstream(new file( pathtoourfile)); url url = new url(urlserver); connection = (httpurlconnection) url.openconnection(); // allow inputs & outputs connection.setdoinput(true); connection.setdooutput(true); connection.setusecaches(false); // enable post method connection.setrequestmethod("post"); //connection.setrequestproperty("username","niranga" ); connection.setrequestproperty("connection", "keep-alive"); connection.setrequestproperty("content-type", "multipart/form-data;boundary=" + boundary); outputstream = new dataoutputstream( connection.getoutputstream()); outputstream.writebytes(twohyphens + boundary + lineend); outputstream.writebytes("content-disposition: form-data; name=\"uploaded_file\";filename=\""+ pathtoourfile + "\"" +";username=\""+"niranga"+ lineend); outputstream.writebytes(lineend); bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); buffer = new byte[buffersize]; // read file bytesread = fileinputstream.read(buffer, 0, buffersize); while (bytesread > 0) { (int x = 0; x < 100; x++) { // publishing progress.... // after onprogressupdate called publishprogress("" + x); } outputstream.write(buffer, 0, buffersize); bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); bytesread = fileinputstream.read(buffer, 0, buffersize); } outputstream.writebytes(lineend); outputstream.writebytes(twohyphens + boundary + twohyphens + lineend); // responses server (code , message) int serverresponsecode = connection.getresponsecode(); string serverresponsemessage = connection.getresponsemessage(); //print server response here.. log.e("---------",(serverresponsecode + " ===> " + serverresponsemessage)); fileinputstream.close(); outputstream.flush(); outputstream.close(); } catch (final exception ex) { runonuithread(new runnable() { public void run() { alertdialog alertdialog = new alertdialog.builder( addtvshows.this).create(); // setting dialog title alertdialog.settitle("alert dialog"); log.e("erorrr",ex.tostring()); // setting dialog message alertdialog.setmessage(ex.tostring()); // setting ok button alertdialog.setbutton("upload again", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { // write code here execute after dialog // closed //toast.maketext(getapplicationcontext(), "", // toast.length_short).show(); } }); // showing alert message alertdialog.show(); } }); } return null; } want pass username parameter serverside php script using httpurlconnection.i use below url .but username value not taking server side php script.please me username server side php script.... i want sent username value server side php script tobe inserted database.but inserted null values..
string urlserver = "http://" + "10.0.2.2" + "/tvshow/upload_tvshows.php?username=" +"niranga"+"";
http://developer.android.com/reference/org/apache/http/client/methods/httppost.html
you need make http post request url. should use asyntask
public void postdata() { // create new httpclient , post header httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://www.yoursite.com/script.php"); try { // add data list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("id", "12345")); namevaluepairs.add(new basicnamevaluepair("stringdata", "anddev cool!")); httppost.setentity(new urlencodedformentity(namevaluepairs)); // execute http post request httpresponse response = httpclient.execute(httppost); } catch (clientprotocolexception e) { // todo auto-generated catch block } catch (ioexception e) { // todo auto-generated catch block } }
Comments
Post a Comment