How can I do in javascript a program that seems simple in Java? [closed]

0

I am very new to programming, I have achieved it relatively easily (thanks to multiple and clear examples on the internet) read the response of dtweet and save it in a variable.

This is the code I have:

package leerrespuestadedweet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LeerRespuestaDeDweet {
    public static void main(String[] args) {
        try {
            String lineaDeDweet, mensajeDeDweet = "";
            URL verDweet = new URL("https://dweet.io/get/latest/dweet/for/my-thing-name");
            try (BufferedReader entradaDweet = new BufferedReader(new InputStreamReader(verDweet.openStream()))) {
                while ((lineaDeDweet = entradaDweet.readLine()) != null){mensajeDeDweet += lineaDeDweet;}
                entradaDweet.close();
System.out.println(mensajeDeDweet);
            } catch (IOException ex) {
Logger.getLogger(LeerRespuestaDeDweet.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (MalformedURLException ex) {
Logger.getLogger(LeerRespuestaDeDweet.class.getName()).log(Level.SEVERE, null, ex);
        } 
    } 
} 

I want to do the same within a web page and I thought it would be possible with JavaScript (I know it has nothing to do with Java). Probably it is simple or it must be done directly in HTML ...

On the internet I can not find anything. Any example? Any link that you know and can help me?

Thank you for your patience.

    
asked by FBU 05.03.2017 в 18:18
source

2 answers

2

A small example with the library JQuery using ajax.

$.ajax({
  url: "https://dweet.io/get/latest/dweet/for/my-thing-name",
  method: "GET"
}).done(function(response){
  $('.pa').html(response.this);
  console.log(response);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="pa">Hello World!</p>
    
answered by 05.03.2017 в 18:40
1

Well, in the end, following the Guz indication, using AJAX and following the example of link , I've quickly found the way to get the same as with the JAVA.

<script>
    var respuesta = "";
    function loadDoc() {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            if (this.readyState === 4 && this.status === 200) {
            respuesta = this.responseText;
            document.getElementById("demo").innerHTML = respuesta;
            }
        };
    xhttp.open("GET", "https://dweet.io/get/latest/dweet/for/my-thing-name", true);
    xhttp.send();
}
</script>

I respond to myself in case it serves some other clumsy like me. Thanks to everyone.

    
answered by 05.03.2017 в 20:03