Estou com dificuldades em listar todos os dados do JsonArray
em uma JSF, sempre fica somente o ultimo valor, já quando uso System.out.println(variavel)
eu consigo listar tudo sem problemas.
Página JSF que precisa receber os dados
<?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>
Classe responsável pela conversão do JsonArray para objeto:
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;
}
}
Classe responsável pela conexão e ter o retorno do 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;
}
}
Classe que contém os getters e 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;
}
}
Quando executa, fica:
DashBoard
Kappauni3
Só que na verdade, tem que aparecer:
Kappauni1
Kappauni2
Kappauni3
Igual na saída do System.out.println()