I'm having trouble listing all the JsonArray
data in a JSF , always only the last value, since when I use System.out.println(variavel)
I can list everything without problems.
JSF page that needs to receive the data
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>DashBoard</h:head><br/><br/>
<h:body>
<h:form >
<h:outputFormat value="#{ethermine.workers.worker}"/>
<h:commandButton value="Iniciar" action="#{ethermine.executar}"/>
</h:form>
</h:body>
</html>
Class responsible for converting JsonArray to object:
import javafx.concurrent.Worker;
import json.org.JSONArray;
import json.org.JSONObject;
import javax.enterprise.inject.Model;
@Model
public class Ethermine {
private final Workers workers = new Workers();
private Conexao conexao = new Conexao();
public void executar()
{
convertJsonForObject(conexao.getJson());
}
private void convertJsonForObject(StringBuilder json) {
JSONObject obj;
obj = new JSONObject(json.toString());
JSONArray jArray = obj.getJSONArray("data");
for(int i = 0; i< jArray.length(); i++){
JSONObject obj_array_data = jArray.getJSONObject(i);
System.out.println(obj_array_data.get("worker"));
workers.setWorker(String.valueOf(obj_array_data.get("worker")));
}
}
public Workers getWorkers() {
return workers;
}
}
Class responsible for connecting and returning JSON:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
class Conexao {
private StringBuilder resultado = new StringBuilder();
StringBuilder getJson(){
String address = "https://api.ethermine.org/miner/0940f5fAEF2bba7e1e6288E4bc4E9c75ee334b97/workers";
System.setProperty("http.agent","chrome");
try {
URL url = new URL(address);
try {
URLConnection conne = url.openConnection();
InputStream in = conne.getInputStream();
InputStreamReader inReader = new InputStreamReader(in);
BufferedReader out = new BufferedReader(inReader);
String result;
while((result = out.readLine()) !=null){
this.resultado = resultado.append(result);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
return resultado;
}
}
Class containing getters and setters:
public class Workers {
private Boolean status;
private String worker;
private double reportedHashrate;
private double currentHashrate;
private Integer validShare;
private Integer staleShares;
private Double averageHashrate;
public void setStatus(Boolean status){
this.status = status;
}
public Boolean getStatus() {
return status;
}
public String getWorker() {
return worker;
}
public void setWorker(String worker) {
this.worker = worker;
}
public double getReportedHashrate() {
return reportedHashrate;
}
public void setReportedHashrate(double reportedHashrate) {
this.reportedHashrate = reportedHashrate;
}
public double getCurrentHashrate() {
return currentHashrate;
}
public void setCurrentHashrate(double currentHashrate) {
this.currentHashrate = currentHashrate;
}
public Integer getValidShare() {
return validShare;
}
public void setValidShare(Integer validShare) {
this.validShare = validShare;
}
public Integer getStaleShares() {
return staleShares;
}
public void setStaleShares(Integer staleShares) {
this.staleShares = staleShares;
}
public Double getAverageHashrate() {
return averageHashrate;
}
public void setAverageHashrate(Double averageHashrate) {
this.averageHashrate = averageHashrate;
}
}
When you run, it stays:
DashBoard
Kappauni3
But in fact, it has to appear:
Kappauni1
Kappauni2
Kappauni3
Same as output from System.out.println()