Unable to overwrite File contents in php from android -
i trying send variable android app php site. able send if use file_append.
my aim overwrite previous data.if remove file_append, doesn't return anything. example if post dog,cat,bird, php site shows dogcatbird. want bird. cat should overidden dog , forth. removing file_append seems not work. how rectify this? . . . . . . . . . .
mainactivity.java file:
public class mainactivity extends activity { button sendbutton; edittext msgtextfield; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // load layout setcontentview(r.layout.activity_main); if (android.os.build.version.sdk_int > 9) { strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); } // make message text field object msgtextfield = (edittext) findviewbyid(r.id.msgtextfield); // make send button object sendbutton = (button) findviewbyid(r.id.sendbutton); } // function gets called when click button public void send(view v) { // message message text box string msg = msgtextfield.gettext().tostring(); // make sure fields not empty if (msg.length()>0) { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("https://delectable-passbook.000webhostapp.com/sms/test1.php"); try { list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("message", msg)); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpclient.execute(httppost); msgtextfield.settext(""); // clear text box } catch (clientprotocolexception e) { // todo auto-generated catch block } catch (ioexception e) { // todo auto-generated catch block } } else { // display message if text fields empty toast.maketext(getbasecontext(),"all field required",toast.length_short).show(); } } }
php file code :
<?php // "message" variable post request // data coming android app $message=$_post["message"]; // specify file save contents of variable message $filename="androidmessages.html"; // write (append) data file file_put_contents($filename,$message,file_append); // load contents of file variable $androidmessages=file_get_contents($filename); // display contents of variable (which has contents of file) echo $androidmessages; ?>
Comments
Post a Comment