jackcarver12
New Coder
Hello,
I am trying to get JSON data from a webpage into a java program.
I already looked some code up and wrote some JavaScript myself.
[CODE lang="html" title="HTML Code"]<html>
<header>
<link href="standard.css" rel="stylesheet"/>
<script language="javascript" type="text/javascript" src="rechenscript.js"></script>
<title>
Cocktailseite
</title>
</header>
<body>
<!--
DropDown
-->
<nav>
<ul>
<li><a href="#" title="Nach Hause">Home</a></li>
<li class="submenu"><a href="#" title="nix">Regeln</a>
<ul>
<li><a href="#" title="Ihr Inventar">Spielregeln</a></li>
<li><a href="#" title="Unsere Auswahl">Verhaltensregeln</a></li>
</li>
</ul>
</li>
<li><a href="#" title="Ueber uns">Shop</a></li>
<li><a href="#" title="So erreichen Sie uns">Logout</a></li>
</ul>
</nav>
<div class:"Banner">
<img src="Banner.jpg" />
</div>
<h2>Unsere Cocktails </h2>
<form name="liste">
<ul class="Getraenkeliste">
<li class="auflistung">Cuba Libre<input type="checkbox" name="CubaLibre" id="getrank1" value="6.80" onclick="calcPrice()" class="getraenk"> </li>
<li class="auflistung">Sex on the Beach<input type="checkbox" name="SexOnTheBeach" id="getrank1" value="7.20" onclick="calcPrice()" class="getraenk"></li>
<li class="auflistung">Caipirinha<input type="checkbox" name="Caipirinha" id="getrank1" value="7.50" onclick="calcPrice()" class="getraenk"></li>
<li class="auflistung">Mochito<input type="checkbox" name="Mochito" id="getrank1" value="8.00" onclick="calcPrice()" class="getraenk"></li>
<li class="auflistung">Apple Teenie<input type="checkbox" name="AppleTeenie" id="getrank1" value="8.20" onclick="calcPrice()" class="getraenk"></li>
<li class="auflistung">Old Pesces<input type="checkbox" name="OldPesces" id="getrank1" value="9.00" onclick="calcPrice()" class="getraenk"></li>
<li class="auflistung">Tropic Thunder<input type="checkbox" name="TropicThunder" id="getrank1" value="8.40" onclick="calcPrice()" class="getraenk"></li>
</ul>
</form>
<div id="preisfeld">
<p>0.00</p>
</div>
<input type="submit" name="Bestellbutton" value="Bestellen" onclick="bestellung()">
</html>[/CODE]
so this is just a list with checkboxes telling me which box was checked by the used.
This JavaScript file calculates the price.
[CODE lang="javascript" title="Java Script File" highlight="80-84"]var summe=0.0;
var i;
function calcPrice()
{
var betrag=0.0;
var betragf=0.0;
var gecheckteBoxen=[];
var gecheckteBox;
var endsumme=0.0;
for(i=0;i<document.liste.length;i++)
{
if(document.liste.elements.checked)
{
/*rechnung*/
gecheckteBox = document.liste.elements.name;
String(gecheckteBox);
/*namen speichern*/
betrag = document.liste.elements.value;
/*betrag speichern*/
Number(betrag);
Number(summe);
summe=parseFloat(summe) + parseFloat(betrag);
}/*ende if*/
}/*ende for*/
/*Ausgabe*/
var element = document.getElementById("preisfeld");
if(summe===0)
{
element.innerHTML = "0.00"+"€";
}
else {
parseFloat(summe);
summe.toFixed(2);/* warum funktioniert das nicht*/
var summeS=String(summe);
/*Ausgabe */
element.innerHTML = String(summe)+ "0" + "€"; //0 wird angefügt, weil toFixed() nicht funktioniert
endsumme=summe;
summe=0;
betrag=0;
betragf=0;
}
return endsumme;
}
function bestellung()
{
var preisDerBox;
var preisDerBoxen=[];
var bestellung=[];
var endsumme=0.0;
var name;
var k=0;
let object;
var sendeObjekt;
endsumme=calcPrice();
for(i=0;i<document.liste.length;i++)
{
if(document.liste.elements.checked)
{
/*rechnung*/
gecheckteBox = document.liste.elements.name;
bestellung.push(gecheckteBox);
preisDerBox = document.liste.elements.value;
parseFloat(preisDerBox);
preisDerBoxen.push(preisDerBox);
bestellungJSON = {name: bestellung , preis: preisDerBoxen ,gesamtpreis: endsumme};
object = JSON.stringify(bestellungJSON);
sendeObjekt = JSON.parse(object);
// bestellungsListe.push({name: gecheckteBox, preis: preisDerBox})
//JSON erstellen
//let json = JSON.stringify(bestellung);
}/*ende if*/
}/*ende for*/
/* bestellung speichert alle namen */
/*endsumme speichert den Preis */
}
/* maybe....
function sendJSON()
{
var xhr = new XMLHttpRequest();
var url = "url";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function ()
{
if (xhr.readyState === 4 && xhr.status === 200)
{
var json = JSON.parse(xhr.responseText);
console.log(json.email + ", " + json.password);
}
};
var data = JSON.stringify({"email": "[email protected]", "password": "101010"});
xhr.send(data);
}
}
*/
window.addEventListener("load", calcPrice());
[/CODE]
There should be a way for me to take the data in my Java Program:
This is the Code:
[CODE lang="java" title="Java Code to receive JSON" highlight="37-42"]package bk;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonReader {
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}
public static void main(String[] args) throws IOException, JSONException {
JSONObject json = readJsonFromUrl("https://swapi.co/api/people/1/");
System.out.println(json.toString());
System.out.println(json.get("id"));
}
}
[/CODE]
So I am not quite sure if this program will do it. So I looked up for another piece of code which could also do the work:
[CODE lang="java" title="Java Code 2" highlight="16-20"]package Zetcode;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class GetRequestJava11
{
// TODO Auto-generated method stub
public static void main(String[] args) throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient(); //GetRequest
HttpRequest request = HttpRequest.newBuilder() //neuen http Client erzeugen
.uri(URI.create("http://webcode.me"))//über die Factory
.build();
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println(response.body()); //gibt den body der Webseite aus
}
}
[/CODE]
So this is it. This is everything I have right now. I am forward looking for any suggestions.
Thank you in advance! Jack
I am trying to get JSON data from a webpage into a java program.
I already looked some code up and wrote some JavaScript myself.
[CODE lang="html" title="HTML Code"]<html>
<header>
<link href="standard.css" rel="stylesheet"/>
<script language="javascript" type="text/javascript" src="rechenscript.js"></script>
<title>
Cocktailseite
</title>
</header>
<body>
<!--
DropDown
-->
<nav>
<ul>
<li><a href="#" title="Nach Hause">Home</a></li>
<li class="submenu"><a href="#" title="nix">Regeln</a>
<ul>
<li><a href="#" title="Ihr Inventar">Spielregeln</a></li>
<li><a href="#" title="Unsere Auswahl">Verhaltensregeln</a></li>
</li>
</ul>
</li>
<li><a href="#" title="Ueber uns">Shop</a></li>
<li><a href="#" title="So erreichen Sie uns">Logout</a></li>
</ul>
</nav>
<div class:"Banner">
<img src="Banner.jpg" />
</div>
<h2>Unsere Cocktails </h2>
<form name="liste">
<ul class="Getraenkeliste">
<li class="auflistung">Cuba Libre<input type="checkbox" name="CubaLibre" id="getrank1" value="6.80" onclick="calcPrice()" class="getraenk"> </li>
<li class="auflistung">Sex on the Beach<input type="checkbox" name="SexOnTheBeach" id="getrank1" value="7.20" onclick="calcPrice()" class="getraenk"></li>
<li class="auflistung">Caipirinha<input type="checkbox" name="Caipirinha" id="getrank1" value="7.50" onclick="calcPrice()" class="getraenk"></li>
<li class="auflistung">Mochito<input type="checkbox" name="Mochito" id="getrank1" value="8.00" onclick="calcPrice()" class="getraenk"></li>
<li class="auflistung">Apple Teenie<input type="checkbox" name="AppleTeenie" id="getrank1" value="8.20" onclick="calcPrice()" class="getraenk"></li>
<li class="auflistung">Old Pesces<input type="checkbox" name="OldPesces" id="getrank1" value="9.00" onclick="calcPrice()" class="getraenk"></li>
<li class="auflistung">Tropic Thunder<input type="checkbox" name="TropicThunder" id="getrank1" value="8.40" onclick="calcPrice()" class="getraenk"></li>
</ul>
</form>
<div id="preisfeld">
<p>0.00</p>
</div>
<input type="submit" name="Bestellbutton" value="Bestellen" onclick="bestellung()">
</html>[/CODE]
so this is just a list with checkboxes telling me which box was checked by the used.
This JavaScript file calculates the price.
[CODE lang="javascript" title="Java Script File" highlight="80-84"]var summe=0.0;
var i;
function calcPrice()
{
var betrag=0.0;
var betragf=0.0;
var gecheckteBoxen=[];
var gecheckteBox;
var endsumme=0.0;
for(i=0;i<document.liste.length;i++)
{
if(document.liste.elements.checked)
{
/*rechnung*/
gecheckteBox = document.liste.elements.name;
String(gecheckteBox);
/*namen speichern*/
betrag = document.liste.elements.value;
/*betrag speichern*/
Number(betrag);
Number(summe);
summe=parseFloat(summe) + parseFloat(betrag);
}/*ende if*/
}/*ende for*/
/*Ausgabe*/
var element = document.getElementById("preisfeld");
if(summe===0)
{
element.innerHTML = "0.00"+"€";
}
else {
parseFloat(summe);
summe.toFixed(2);/* warum funktioniert das nicht*/
var summeS=String(summe);
/*Ausgabe */
element.innerHTML = String(summe)+ "0" + "€"; //0 wird angefügt, weil toFixed() nicht funktioniert
endsumme=summe;
summe=0;
betrag=0;
betragf=0;
}
return endsumme;
}
function bestellung()
{
var preisDerBox;
var preisDerBoxen=[];
var bestellung=[];
var endsumme=0.0;
var name;
var k=0;
let object;
var sendeObjekt;
endsumme=calcPrice();
for(i=0;i<document.liste.length;i++)
{
if(document.liste.elements.checked)
{
/*rechnung*/
gecheckteBox = document.liste.elements.name;
bestellung.push(gecheckteBox);
preisDerBox = document.liste.elements.value;
parseFloat(preisDerBox);
preisDerBoxen.push(preisDerBox);
bestellungJSON = {name: bestellung , preis: preisDerBoxen ,gesamtpreis: endsumme};
object = JSON.stringify(bestellungJSON);
sendeObjekt = JSON.parse(object);
// bestellungsListe.push({name: gecheckteBox, preis: preisDerBox})
//JSON erstellen
//let json = JSON.stringify(bestellung);
}/*ende if*/
}/*ende for*/
/* bestellung speichert alle namen */
/*endsumme speichert den Preis */
}
/* maybe....
function sendJSON()
{
var xhr = new XMLHttpRequest();
var url = "url";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function ()
{
if (xhr.readyState === 4 && xhr.status === 200)
{
var json = JSON.parse(xhr.responseText);
console.log(json.email + ", " + json.password);
}
};
var data = JSON.stringify({"email": "[email protected]", "password": "101010"});
xhr.send(data);
}
}
*/
window.addEventListener("load", calcPrice());
[/CODE]
There should be a way for me to take the data in my Java Program:
This is the Code:
[CODE lang="java" title="Java Code to receive JSON" highlight="37-42"]package bk;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonReader {
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}
public static void main(String[] args) throws IOException, JSONException {
JSONObject json = readJsonFromUrl("https://swapi.co/api/people/1/");
System.out.println(json.toString());
System.out.println(json.get("id"));
}
}
[/CODE]
So I am not quite sure if this program will do it. So I looked up for another piece of code which could also do the work:
[CODE lang="java" title="Java Code 2" highlight="16-20"]package Zetcode;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class GetRequestJava11
{
// TODO Auto-generated method stub
public static void main(String[] args) throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient(); //GetRequest
HttpRequest request = HttpRequest.newBuilder() //neuen http Client erzeugen
.uri(URI.create("http://webcode.me"))//über die Factory
.build();
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println(response.body()); //gibt den body der Webseite aus
}
}
[/CODE]
So this is it. This is everything I have right now. I am forward looking for any suggestions.
Thank you in advance! Jack