Query in webservice and populated a grid with data

0

The question regarding "duplicate" is about consuming webservice in delphi, and in this case I have already consumed, but now I need to play the data that delphi takes from the webservice and put on a grid, so a very different question .

I have a webservice REST made in Java that does a query in a table Bank, first I threw the data in a MEMO to test the webservice, however, now I need to play this query data on a grid, I researched google and not I found nothing that could help me, so I decided to ask here in the forum.

I'm basically making a simple app to test the operation of delphi + WS REST, I'm trying to create a WS that does an insert, update, delete, and select in that database table, first I'm just testing the GETs.

WS:

Obs.1: The get method in question is public String getBanco(@PathParam("nome") String nome) and the rest are just tests. Obs.2: I think it is not necessary to put the class with the methods that performs the search in the bank, if so, I will edit the post.

package ws;

import CTR.BancoCTR;
import com.google.gson.Gson;
import dao.BancoDAO;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;

/**
 * REST Web Service
 *
 * @author Vitor
 */
@Path("BancoWs")
public class BancoWs {

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of BancoWs
     */
    public BancoWs() {
    }

    /**
     * Retrieves representation of an instance of ws.BancoWs
     * @return an instance of java.lang.String
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getJson() {
        return "meu primeiro webservice restfull";
    }
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("Banco/get/{nome}")
    public String getBanco(@PathParam("nome") String nome){
        List<BancoCTR> lista = new ArrayList<BancoCTR>();
        BancoDAO banco = new BancoDAO();

        if (nome.equals("null")){
            lista = banco.listBanco(0, nome);
        }else{
            lista = banco.listBanco(1, nome);
        }

        Gson gson = new Gson();
        return gson.toJson(lista);
    }
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("Banco/list")
    public String listBancos(){

        List<BancoCTR> lista = new ArrayList<BancoCTR>();

        BancoCTR banco = new BancoCTR();
        banco.setBcoCodigo(1);
        banco.setBcoNome("Santander");

        lista.add(banco);

        banco.setBcoCodigo(2);
        banco.setBcoNome("Banco do Brasil");

        lista.add(banco);

        Gson gson = new Gson();
        return gson.toJson(lista);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("Banco/listBd")
    public String listBdBancos(){

        List<BancoCTR> lista = new ArrayList<BancoCTR>();

        BancoDAO dao = new BancoDAO();
        lista = dao.listBanco(0, "");

        Gson gson = new Gson();
        return gson.toJson(lista);
    }

    /**
     * PUT method for updating or creating an instance of BancoWs
     * @param content representation for the resource
     */
    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    public void putJson(String content) {
    }
}

In Delphi:

procedure TForm1.btnPesquisarClick(Sender: TObject);
var
 filtro: String;

 http: TIdHTTP;
 AStrResponse: TStringStream;
 oSSL: TIdSSLIOHandlerSocketOpenSSL;
 FURL: String;
begin

  if edt3.Text <> '' then
    filtro := edt3.Text
  else
    filtro := 'null';

  http := TIdHTTP.Create(Nil);
  oSSL := TIdSSLIOHandlerSocketOpenSSL.Create(Nil);

    oSSL.SSLOptions.Method := sslvSSLv23;
    oSSL.SSLOptions.Mode := sslmUnassigned;
    oSSL.SSLOptions.VerifyMode := [];
    oSSL.SSLOptions.VerifyDepth := 0;
    oSSL.Host := '';

    http.IOHandler := oSSL;
    http.HandleRedirects := True;
    http.Request.ContentType := 'application/json';

    FURL := 'http://localhost:8080/bancoWS/webresources/BancoWs/Banco/get/'
      + filtro;

    AStrResponse := TStringStream.Create;
    http.Get(FURL, AStrResponse);

    mmo1.Clear;
    mmo1.Lines.Add(AStrResponse.DataString);

    FreeAndNil(AStrResponse);
    http.Disconnect;

    FreeAndNil(oSSL);
    FreeAndNil(http);
end;
    
asked by anonymous 02.02.2018 / 12:22

1 answer

0

For this purpose I use the library delphi-rest-client-api , with it it is possible load a JSON into a dataset.

Follow the example of the creator:

var
  vDataSet: TClientDataSet;
begin
  vDataSet := TClientDataSet.Create(nil);
  try
    TDataSetUtils.CreateField(vDataSet, ftInteger, 'id');
    TDataSetUtils.CreateField(vDataSet, ftString, 'name', 100);
    TDataSetUtils.CreateField(vDataSet, ftString, 'email', 100);
    vDataSet.CreateDataSet;

    RestClient.Resource(CONTEXT_PATH + 'persons')
              .Accept(RestUtils.MediaType_Json)
              .GetAsDataSet(vDataSet);
  finally
    vDataSet.Free;
  end;
    
02.02.2018 / 15:33