Java / Android: Read and parse .txt file from a url












3















Using java in android studio I am trying to read a .txt file and parse it to obtain some data.



the file: https://www.ndbc.noaa.gov/data/latest_obs/latest_obs.txt



I am using he following code to parse the data:



String splited = str.trim().replaceAll(" +", " ").split(" ");

String sDate1= splited[3] + "/" + splited[4] + "/" + splited[5]
+ "/" + splited[6] + "/" + splited[7];

try{

java.util.Date date1 = new
java.text.SimpleDateFormat("yyyy/MM/dd/hh/mm").parse(sDate1);
System.out.println(date1);
}
catch(Exception e){
System.out.println(e.getMessage());
}

String windSpeed = splited[9];
String waveHeight = splited[11];
String airTemperature = splited[17];
String waterTemperature = splited[18];

System.out.println(windSpeed);
System.out.println(waveHeight);
System.out.println(airTemperature);
System.out.println(waterTemperature);

if(windSpeed.toLowerCase().equals("mm")){
// write your code here
}

if(waveHeight.toLowerCase().equals("mm")){
// write your code here
}

if(airTemperature.toLowerCase().equals("mm")){
// write your code here
}

if(waterTemperature.toLowerCase().equals("mm")){
// write your code here
}


The '//write your code here' will just return 'data N/A' since mm refers to missing data.



My problem is I am unsure how to open the file from the url to be read. I would like to open the file every hour, and parse the data below so i can assign it to their buoys in my application.










share|improve this question



























    3















    Using java in android studio I am trying to read a .txt file and parse it to obtain some data.



    the file: https://www.ndbc.noaa.gov/data/latest_obs/latest_obs.txt



    I am using he following code to parse the data:



    String splited = str.trim().replaceAll(" +", " ").split(" ");

    String sDate1= splited[3] + "/" + splited[4] + "/" + splited[5]
    + "/" + splited[6] + "/" + splited[7];

    try{

    java.util.Date date1 = new
    java.text.SimpleDateFormat("yyyy/MM/dd/hh/mm").parse(sDate1);
    System.out.println(date1);
    }
    catch(Exception e){
    System.out.println(e.getMessage());
    }

    String windSpeed = splited[9];
    String waveHeight = splited[11];
    String airTemperature = splited[17];
    String waterTemperature = splited[18];

    System.out.println(windSpeed);
    System.out.println(waveHeight);
    System.out.println(airTemperature);
    System.out.println(waterTemperature);

    if(windSpeed.toLowerCase().equals("mm")){
    // write your code here
    }

    if(waveHeight.toLowerCase().equals("mm")){
    // write your code here
    }

    if(airTemperature.toLowerCase().equals("mm")){
    // write your code here
    }

    if(waterTemperature.toLowerCase().equals("mm")){
    // write your code here
    }


    The '//write your code here' will just return 'data N/A' since mm refers to missing data.



    My problem is I am unsure how to open the file from the url to be read. I would like to open the file every hour, and parse the data below so i can assign it to their buoys in my application.










    share|improve this question

























      3












      3








      3








      Using java in android studio I am trying to read a .txt file and parse it to obtain some data.



      the file: https://www.ndbc.noaa.gov/data/latest_obs/latest_obs.txt



      I am using he following code to parse the data:



      String splited = str.trim().replaceAll(" +", " ").split(" ");

      String sDate1= splited[3] + "/" + splited[4] + "/" + splited[5]
      + "/" + splited[6] + "/" + splited[7];

      try{

      java.util.Date date1 = new
      java.text.SimpleDateFormat("yyyy/MM/dd/hh/mm").parse(sDate1);
      System.out.println(date1);
      }
      catch(Exception e){
      System.out.println(e.getMessage());
      }

      String windSpeed = splited[9];
      String waveHeight = splited[11];
      String airTemperature = splited[17];
      String waterTemperature = splited[18];

      System.out.println(windSpeed);
      System.out.println(waveHeight);
      System.out.println(airTemperature);
      System.out.println(waterTemperature);

      if(windSpeed.toLowerCase().equals("mm")){
      // write your code here
      }

      if(waveHeight.toLowerCase().equals("mm")){
      // write your code here
      }

      if(airTemperature.toLowerCase().equals("mm")){
      // write your code here
      }

      if(waterTemperature.toLowerCase().equals("mm")){
      // write your code here
      }


      The '//write your code here' will just return 'data N/A' since mm refers to missing data.



      My problem is I am unsure how to open the file from the url to be read. I would like to open the file every hour, and parse the data below so i can assign it to their buoys in my application.










      share|improve this question














      Using java in android studio I am trying to read a .txt file and parse it to obtain some data.



      the file: https://www.ndbc.noaa.gov/data/latest_obs/latest_obs.txt



      I am using he following code to parse the data:



      String splited = str.trim().replaceAll(" +", " ").split(" ");

      String sDate1= splited[3] + "/" + splited[4] + "/" + splited[5]
      + "/" + splited[6] + "/" + splited[7];

      try{

      java.util.Date date1 = new
      java.text.SimpleDateFormat("yyyy/MM/dd/hh/mm").parse(sDate1);
      System.out.println(date1);
      }
      catch(Exception e){
      System.out.println(e.getMessage());
      }

      String windSpeed = splited[9];
      String waveHeight = splited[11];
      String airTemperature = splited[17];
      String waterTemperature = splited[18];

      System.out.println(windSpeed);
      System.out.println(waveHeight);
      System.out.println(airTemperature);
      System.out.println(waterTemperature);

      if(windSpeed.toLowerCase().equals("mm")){
      // write your code here
      }

      if(waveHeight.toLowerCase().equals("mm")){
      // write your code here
      }

      if(airTemperature.toLowerCase().equals("mm")){
      // write your code here
      }

      if(waterTemperature.toLowerCase().equals("mm")){
      // write your code here
      }


      The '//write your code here' will just return 'data N/A' since mm refers to missing data.



      My problem is I am unsure how to open the file from the url to be read. I would like to open the file every hour, and parse the data below so i can assign it to their buoys in my application.







      java android text-parsing






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 22:19









      SeanSean

      194




      194
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Your data file is coming from the Internet, so you'll need to download the file first before parsing it. While it is possible to download and parse the file at the same time, let's keep it simple at first.



          To download the file, there are many ways to do this, but you might start with OkHttp or UrlConnection (see this SO answer for more info). If you want an alternative, check out Retrofit. Retrofit is a wrapper around OkHTTP to make it a little easier to use for experienced developers, but if you're just getting started, I'd recommend sticking with OkHttp for now until you understand what's going on.



          Once the file is downloaded or in memory, you'll probably want to use BufferedReader (as suggested by rileyjsumner) to read and parse one line at a time using the code you posted.



          Because you're asking specifically about Android, you'll need to keep a few things in mind:




          • When reading and writing temporary files, you'll want to use the temporary storage. Check out this documentation for more details: https://developer.android.com/training/data-storage/files

          • you'll need to do the downloading and file I/O on a separate thread. Android is inherently multithreaded and relies on the main thread to only update the UI. Everything else should be done on a different thread. There are several ways to do this. (see this post or the documentation). Once you get more comfortable with this, you might move on to RxJava.






          share|improve this answer

































            0














            You may want to use BufferedReader - javadoc



            BufferedReader reader = new BufferedReader(new FileReader("url.txt"));


            You can then scan through the context of the file.



            while(line = reader.readLine() != null) {
            // some code
            }


            This answer has more information on BufferedReader's as well






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290378%2fjava-android-read-and-parse-txt-file-from-a-url%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              Your data file is coming from the Internet, so you'll need to download the file first before parsing it. While it is possible to download and parse the file at the same time, let's keep it simple at first.



              To download the file, there are many ways to do this, but you might start with OkHttp or UrlConnection (see this SO answer for more info). If you want an alternative, check out Retrofit. Retrofit is a wrapper around OkHTTP to make it a little easier to use for experienced developers, but if you're just getting started, I'd recommend sticking with OkHttp for now until you understand what's going on.



              Once the file is downloaded or in memory, you'll probably want to use BufferedReader (as suggested by rileyjsumner) to read and parse one line at a time using the code you posted.



              Because you're asking specifically about Android, you'll need to keep a few things in mind:




              • When reading and writing temporary files, you'll want to use the temporary storage. Check out this documentation for more details: https://developer.android.com/training/data-storage/files

              • you'll need to do the downloading and file I/O on a separate thread. Android is inherently multithreaded and relies on the main thread to only update the UI. Everything else should be done on a different thread. There are several ways to do this. (see this post or the documentation). Once you get more comfortable with this, you might move on to RxJava.






              share|improve this answer






























                1














                Your data file is coming from the Internet, so you'll need to download the file first before parsing it. While it is possible to download and parse the file at the same time, let's keep it simple at first.



                To download the file, there are many ways to do this, but you might start with OkHttp or UrlConnection (see this SO answer for more info). If you want an alternative, check out Retrofit. Retrofit is a wrapper around OkHTTP to make it a little easier to use for experienced developers, but if you're just getting started, I'd recommend sticking with OkHttp for now until you understand what's going on.



                Once the file is downloaded or in memory, you'll probably want to use BufferedReader (as suggested by rileyjsumner) to read and parse one line at a time using the code you posted.



                Because you're asking specifically about Android, you'll need to keep a few things in mind:




                • When reading and writing temporary files, you'll want to use the temporary storage. Check out this documentation for more details: https://developer.android.com/training/data-storage/files

                • you'll need to do the downloading and file I/O on a separate thread. Android is inherently multithreaded and relies on the main thread to only update the UI. Everything else should be done on a different thread. There are several ways to do this. (see this post or the documentation). Once you get more comfortable with this, you might move on to RxJava.






                share|improve this answer




























                  1












                  1








                  1







                  Your data file is coming from the Internet, so you'll need to download the file first before parsing it. While it is possible to download and parse the file at the same time, let's keep it simple at first.



                  To download the file, there are many ways to do this, but you might start with OkHttp or UrlConnection (see this SO answer for more info). If you want an alternative, check out Retrofit. Retrofit is a wrapper around OkHTTP to make it a little easier to use for experienced developers, but if you're just getting started, I'd recommend sticking with OkHttp for now until you understand what's going on.



                  Once the file is downloaded or in memory, you'll probably want to use BufferedReader (as suggested by rileyjsumner) to read and parse one line at a time using the code you posted.



                  Because you're asking specifically about Android, you'll need to keep a few things in mind:




                  • When reading and writing temporary files, you'll want to use the temporary storage. Check out this documentation for more details: https://developer.android.com/training/data-storage/files

                  • you'll need to do the downloading and file I/O on a separate thread. Android is inherently multithreaded and relies on the main thread to only update the UI. Everything else should be done on a different thread. There are several ways to do this. (see this post or the documentation). Once you get more comfortable with this, you might move on to RxJava.






                  share|improve this answer















                  Your data file is coming from the Internet, so you'll need to download the file first before parsing it. While it is possible to download and parse the file at the same time, let's keep it simple at first.



                  To download the file, there are many ways to do this, but you might start with OkHttp or UrlConnection (see this SO answer for more info). If you want an alternative, check out Retrofit. Retrofit is a wrapper around OkHTTP to make it a little easier to use for experienced developers, but if you're just getting started, I'd recommend sticking with OkHttp for now until you understand what's going on.



                  Once the file is downloaded or in memory, you'll probably want to use BufferedReader (as suggested by rileyjsumner) to read and parse one line at a time using the code you posted.



                  Because you're asking specifically about Android, you'll need to keep a few things in mind:




                  • When reading and writing temporary files, you'll want to use the temporary storage. Check out this documentation for more details: https://developer.android.com/training/data-storage/files

                  • you'll need to do the downloading and file I/O on a separate thread. Android is inherently multithreaded and relies on the main thread to only update the UI. Everything else should be done on a different thread. There are several ways to do this. (see this post or the documentation). Once you get more comfortable with this, you might move on to RxJava.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 13 '18 at 22:38

























                  answered Nov 13 '18 at 22:33









                  Kyle FalconerKyle Falconer

                  5,06623254




                  5,06623254

























                      0














                      You may want to use BufferedReader - javadoc



                      BufferedReader reader = new BufferedReader(new FileReader("url.txt"));


                      You can then scan through the context of the file.



                      while(line = reader.readLine() != null) {
                      // some code
                      }


                      This answer has more information on BufferedReader's as well






                      share|improve this answer




























                        0














                        You may want to use BufferedReader - javadoc



                        BufferedReader reader = new BufferedReader(new FileReader("url.txt"));


                        You can then scan through the context of the file.



                        while(line = reader.readLine() != null) {
                        // some code
                        }


                        This answer has more information on BufferedReader's as well






                        share|improve this answer


























                          0












                          0








                          0







                          You may want to use BufferedReader - javadoc



                          BufferedReader reader = new BufferedReader(new FileReader("url.txt"));


                          You can then scan through the context of the file.



                          while(line = reader.readLine() != null) {
                          // some code
                          }


                          This answer has more information on BufferedReader's as well






                          share|improve this answer













                          You may want to use BufferedReader - javadoc



                          BufferedReader reader = new BufferedReader(new FileReader("url.txt"));


                          You can then scan through the context of the file.



                          while(line = reader.readLine() != null) {
                          // some code
                          }


                          This answer has more information on BufferedReader's as well







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 13 '18 at 22:24









                          rileyjsumnerrileyjsumner

                          1851115




                          1851115






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290378%2fjava-android-read-and-parse-txt-file-from-a-url%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Bressuire

                              Vorschmack

                              Quarantine