ListView com imagem de URL via JSON Dalphi XE 7

29/04/2015

0

Estou com um sistema usando JSON criado por php. Consigo exibir os dados certinho, só que não estou conseguindo exibir uma imagem via URL.

Tenho a imagem URL: http://ovaral.com.br/webservices/images/estabelecimentos/escovaria.jpg

[img:descricao=logo da empresa]http://ovaral.com.br/webservices/images/estabelecimentos/escovaria.jpg[/img]

JSON:

"estabelecimentos":[{"estabelecimento_id":"2","estabelecimento_razao":"Escovaria Domiciliar","estabelecimento_fantasia":"Escovaria","estabelecimento_logo":"\/images\/estabelecimentos\/escovaria.jpg","estabelecimento_cidade":"Petrolina"},{"estabelecimento_id":"1","estabelecimento_razao":"Nova Beleza","estabelecimento_fantasia":"Nova Beleza","estabelecimento_logo":"\/images\/estabelecimentos\/nova_beleza.jpg","estabelecimento_cidade":"Petrolina"}]
Paulo Andrade

Paulo Andrade

Responder

Posts

04/08/2015

Lucas Azevedo

Achou uma solução amigo, tbm procuro a mesma coisa?
Responder

04/08/2015

Paulo Andrade

Consegui amigo. Fiz o seguinte:

procedure TfBuscaEstabelecimentos.PreparaJSON(strJSON: String);
var
  LJsonObj,AJsonSubObj : TJSONObject;
  AJsonPair, AJsonSubPair: TJSONPair;
  AJsonArray: TJSONArray;
  J, I, nC: Integer;
  StringJSON,imgURL: String;
  LItem: TListBoxItem;
  TM: TMemoryStream;
  HTTP : TIdHttp;
  imgItem: TImage;
  lTexto: TLabel;
begin
  tmpListaEstabelecimento.Limpar;
  lstPorCidade.BeginUpdate;
  lstPorCidade.Items.Clear;
  lstInformacoes.Items.Clear;
  LJsonObj := TJsonObject.Create;
  StringJSON := '{'+strJSON;
  StringJSON := StringJSON+'}';

  LJsonObj := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(StringJSON), 0) as TJSONObject;
  AJsonPair := LJsonObj.Get(0);
  AJsonArray := (AJsonPair.JsonValue as TJSONArray);
  for I := 0 to AJsonArray.Size - 1 do
  begin
    LItem := TListBoxItem.Create(lstPorCidade);
    tmpEstabelecimento := TEstabelecimentoPronto.Create;
    // Popular ListView
    AJsonSubObj := TJSONObject(AJsonArray.Get(I));
    for J := 0 to AJsonSubObj.Size - 1 do
    begin
      AJsonSubPair := AJsonSubObj.Get(J);
      {
        Aqui tenho Uma classe para pegar os fields e o astring (Dado do Objeto)
        utilizando o AJsonSubPair.JsonString.Value e o AJsonSubPair.JsonValue.Value
      }
      if (AJsonSubPair.JsonString.Value = 'estabelecimento_razao') then
      begin
        tmpEstabelecimento.Razao := AJsonSubPair.JsonValue.Value;
        lTexto := TLabel.Create(lstPorCidade);
        lTexto.Name := 'lRazao'+IntToStr(j)+'n'+IntToStr(i);
        lTexto.Text := AJsonSubPair.JsonValue.Value;
        lTexto.Parent := LItem;
        lTexto.Position.X := 130;
        lTexto.Height := 72;
      end;
      if (AJsonSubPair.JsonString.Value = 'estabelecimento_id') then
      begin
        tmpEstabelecimento.Id := StrToInt(AJsonSubPair.JsonValue.Value);
        LItem.Tag := StrToInt(AJsonSubPair.JsonValue.Value);
      end;
      if (AJsonSubPair.JsonString.Value = 'estabelecimento_logo') then
      begin
        tmpEstabelecimento.Logo := AJsonSubPair.JsonValue.Value;
        imgURL := AJsonSubPair.JsonValue.Value;
        nC := Length(Trim(imgURL));
        if (nC > 4) then
        begin
          try
            TM := TMemoryStream.Create;
            TM.Clear;
            try
              imgHttp.Get(imgURL,TM);
            except
              on E: EIdHTTPProtocolException do
                if E.Message <> 'HTTP/1.1 404 Not Found' then
                  ShowMessage(E.Message);
            end;
            {LItem.ItemData.Bitmap.LoadFromStream(TM);
            LItem.ItemData.Bitmap.Resize(120,72);  }
            imgItem := TImage.Create(lstPorCidade);
            imgItem.Name := 'img'+IntToStr(J)+'n'+IntToStr(I);
            imgItem.Parent := LItem;
            imgItem.Height := 72;
            imgItem.Width  := 120;
            imgItem.Position.X := 5;
            imgItem.Bitmap.LoadFromStream(TM);
            LItem.Height := 75;
          finally
            TM.Free;
          end;
        end;
      end;
    end;
    tmpListaEstabelecimento.Adicionar(tmpEstabelecimento);
    lstPorCidade.AddObject(LItem); // Adiciona o LItem a minha ListBox
  end;
  lstPorCidade.EndUpdate;
end;


Qualquer dúvida só perguntar :-)
Responder

21/05/2016

Paulo Andrade

Achou uma solução amigo, tbm procuro a mesma coisa?


Conseguiu com meu exemplo?
Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar