Consume Webservice in Deli

Asked

Viewed 1,849 times

0

I created a REST webservice using JAVA, but I need to make a client in Delphi consume this webservice, I tried to make several video lessons I found by google, but none was useful to me.

I created a simple webservice to test this communication with Delphi and WS REST, the idea of WS is to do an Insert, update, delete and select banks, WS Works right with the tests I did, but, to consume in Delphi that is not right, first I’m testing only GET.

WS:

Note: I used netbeans to create WS.

Note: The GET method in question is @Path("Bank/get/{name}", the other GET methods were just tests to see how WS works.

Obs3: I think it is not necessary to put the classes with the methods of Insert, select and etc, if it is, I 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 
 */
@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) {
    }
}

Client:

the screen is very simple:

inserir a descrição da imagem aqui

the search I wanted to do in a dbgrid, but an example I found used memo, so I tried to use it to see if it worked and then try for dbgrid, but when I click the search button, it cleans the memo but does not bring any information and is blank, I changed the code several times, but, unsuccessfully, the current code was half, because I was making an example and I realized in the middle that it did not serve (I think) kkkkk.

Note: I am using Delphi XE8.

procedure TForm1.btnPesquisarClick(Sender: TObject);
var
 filtro: String;
 mymarshal: TJSONUnMarshal;
begin
  rstrqst1.Resource := '/Banco/get/{nome}';
  rstrqst1.Method := rmGet;

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

  rstrqst1.Params.AddUrlSegment('nome', filtro);
  rstrqst1.Execute;
  mymarshal := TJSONUnMarshal.Create;




  mmo1.Clear;
  mmo1.Lines.Add(rstrspns1.);
end;

unfortunately so far I haven’t found any more examples I can follow, I found some components like idHttp or Tfdstanstoragejsonlink, but, I was in doubt if they served, so I decided to ask here.

1 answer

1


I use Idftp widely in my applications, it has served in everything today I needed in Rest. Below is a dry example of code I use:

var
  http           : TIdHTTP;  
  AStrResponse   : TStringStream;
  oSSL           : TIdSSLIOHandlerSocketOpenSSL;  
  FURL           : String;
begin
  Result:= FALSE;

  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://a_sua_url.com/alguma_coisa'

    AStrResponse := TStringStream.Create;

    http.Get(FURL, AStrResponse);
    Response.Text := AStrResponse.DataString;

    FreeAndNil(AStrResponse);

    http.Disconnect;

  FreeAndNil(oSSL);
  FreeAndNil(http);

end;

Browser other questions tagged

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