5 Replies Latest reply: Mar 25, 2013 1:40 PM by lsepolis123 RSS

    connection-to-server-unsuccessful-phonegap-android-emulator

    lsepolis123 Community Member

      connection-to-server-unsuccessful-phonegap-android-emulator error during lunch, I think code js is old but phonegap is new from github this wk... any comment? just replaced index.html from a finished book chapter in phonegap 2011-2 (c)...

       

       <script type="text/javascript">
                var filename = "read-write.txt";
                  var filePath = "file:///sdcard/read-write.txt";
                                    var textarea = document.getElementById("textarea");
                  /** Called when phonegap javascript is loaded */
                  function onDeviceReady(){
                      var readButton = document.getElementById("read");
                      var writeButton = document.getElementById("write");
      
                      readButton.addEventListener("click", readFile, false);
                      writeButton.addEventListener("click", saveFile, false);
      
                  }
      
                  function readFile(){
      
                      window.resolveLocalFileSystemURI(    //filename to be read
                      filePath,    //success callback 
                      function(fileEntry){
                                fileEntry.file(
                                                                  function(file){
                                                                            var fileReader = new FileReader();
                                                    fileReader.onloadend = function(evt){
                                                        document.getElementById("textarea").value = evt.target.result;
                                                    };
                                                    fileReader.readAsText(file);
                                                                  },
                                                                  function(error){
                                                                            alert("Got error while reading "+filePath);
                                                                  })
                      },    //error callback
                      function(error){
                          alert(filename + " not present, please add content and click Save first");
                      });
      
                  }
      
                  function saveFile(){
      
                      window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
                                                        alert(fileSystem.name);
                          var sdcardEntry = fileSystem.root;
                                                        sdcardEntry.getFile(
                                                                  filename,
                                                                  {create:true},
                                                                  function(fileEntry){
                                                                            fileEntry.createWriter(
                                                                                      function(fileWriter){
                                                                                                 fileWriter.onwrite = function(evt) {
                                                                                                        alert("Write was successful!");
                                                                                                          document.getElementById("textarea").value="";
                                                                                           };
                                                                                                 fileWriter.write(document.getElementById("textarea").value);
                                                                                      },
                                                                                      function(error){
                                                                                                alert("Failed to get a file writer for "+filename); 
                                                                                      });
      
      
      
                                                                  },
                                                                  function(error){
                                                                            alert("Got error while reading "+filename+" "+error);
                                                                  }
                                                                  ) ;          
      
                      }, function(error){
                          alert("Got Error while gaining access to file system");
                      });
      
      
      
                  }
      
      
      
                  /** Called when browser load this page*/
                  function init(){
                      document.addEventListener("deviceready", onDeviceReady, false);
                  }
              </script>
      
      

      connection-to-server-unsuccessful-phonegap-android-emulator.jpg