Skip navigation
Currently Being Moderated

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

Mar 22, 2013 3:23 PM

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

 
Replies

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points