Query the webservice and populated a grid with the data

Asked

Viewed 688 times

0

The question referring to "duplicated", is about consuming the webservice in Delphi, and in this case I already consumed, but now I need to play the data that Delphi takes from the webservice and put in a grid, therefore, a very different question.

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

basically I’m making a simple app to test the functioning of Delphi + WS REST, I’m trying to create a WS that makes an Insert, update, delete and select in this table Database, first I’m testing only the Gets.

WS:

Obs.1: The get method in question is the public String getBanco(@PathParam("nome") String nome) The rest are just tests. obs.2: I think it is not necessary to put the class with the methods that perform the search in the database, if that is, 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) {
    }
}

At 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;
  • Possible duplicate of Consume Webservice in Deli

  • I think not, since then I ask about consuming webservice and not about loading a grid with the data from the webservice, what to do if the classes are the same? kkk

  • has a video here that shows. Maybe you can help vc. https://www.youtube.com/watch?v=HJJSWMTpJvk

  • does not help me because I am already consuming the webservice and loading the data in a MEMO (same in the video) my problem now is instead of loading this data in a MEMO I want to load them in a Dbgrid.

1 answer

0

For this purpose I use the library Delphi-Rest-client-api, with it it is possible to load a JSON to 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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.