Membaca data json dengan delphi 7

Berikut kode untuk membaca data json dengan delphi 7.

uses
 IdHTTP, IdException, IdStack, uLkJSON;

procedure TForm1.Button1Click(Sender: TObject);
var
 sJSON: String;
 js, Items, Item: TlkJSONBase;
 I, J: Integer;
 IdHTTP: TIdHTTP;
 resp: TMemoryStream;
begin
 IdHTTP := TIdHTTP.Create(Self);
 resp := TMemoryStream.Create;
 try
 try
 IdHTTP.Get('http://localhost/experiments/json/index.txt', resp);
 resp.Position := 0;
 memo1.Lines.LoadFromStream(resp);

SetLength(sJSON, resp.Size);
 resp.Position := 0;
 resp.Write(sJSON[1], Length(sJSON));

js := TlkJSON.ParseText(sJSON);
 for I := 0 to Pred(js.Count) do
 begin
 Items := js.Child[I].Field['items'];
 for J := 0 to Pred(Items.Count) do begin
 Item := Items.Child[J];
 Memo2.Lines.Add(VarToStr(Item.Field['region_id'].Value));
 Memo2.Lines.Add(VarToStr(Item.Field['average_cover'].Value));
 Memo2.Lines.Add(VarToStr(Item.Field['average_growth'].Value));
 end;
 end;

except
 // this exception class covers the HTTP protocol errors; you may read the
 // response code using ErrorCode property of the exception object, or the
 // same you can read from the ResponseCode property of the TIdHTTP object
 on E: EIdHTTPProtocolException do
 ShowMessage('Indy raised a protocol error!' + sLineBreak +
 'HTTP status code: ' + IntToStr(E.ReplyErrorCode) + sLineBreak +
 'Error message' + E.Message);
 // this exception class covers the cases when the server side closes the
 // connection with a client in a "peaceful" way
 on E: EIdConnClosedGracefully do
 ShowMessage('Indy reports, that connection was closed gracefully!');
 // this exception class covers all the low level socket exceptions
 on E: EIdSocketError do
 ShowMessage('Indy raised a socket error!' + sLineBreak +
 'Error code: ' + IntToStr(E.LastError) + sLineBreak +
 'Error message' + E.Message);
 // this exception class covers all exceptions thrown by Indy library
 on E: EIdException do
 ShowMessage('Indy raised an exception!' + sLineBreak +
 'Exception class: ' + E.ClassName + sLineBreak +
 'Error message: ' + E.Message);
 // this exception class is a base Delphi exception class and covers here
 // all exceptions different from those listed above
 on E: Exception do
 ShowMessage('A non-Indy related exception has been raised!');
 end;
 finally
 IdHTTP.Free;
 resp.Free;
 end;
end;


Untuk membaca json, kode diatas menggunakan library tambahan yaitu lkjson.  Wassalam.

Advertisements

Contoh kode delphi untuk mendapatkan respon alamat situs

Contoh kode delphi untuk mendapatkan respon alamat situs adalah sebagai berikut.

uses
 IdHTTP, IdException, IdStack;

procedure TForm1.Button1Click(Sender: TObject);
var
 IdHTTP: TIdHTTP;
 resp: TMemoryStream;
begin
 IdHTTP := TIdHTTP.Create(Self);
 resp := TMemoryStream.Create;
 try
 try
 IdHTTP.Get('http://www.example.com', resp);
 resp.Position := 0;
 memo1.Lines.LoadFromStream(resp);
 except
 // this exception class covers the HTTP protocol errors; you may read the
 // response code using ErrorCode property of the exception object, or the
 // same you can read from the ResponseCode property of the TIdHTTP object
 on E: EIdHTTPProtocolException do
 ShowMessage('Indy raised a protocol error!' + sLineBreak +
 'HTTP status code: ' + IntToStr(E.ReplyErrorCode) + sLineBreak +
 'Error message' + E.Message);
 // this exception class covers the cases when the server side closes the
 // connection with a client in a "peaceful" way
 on E: EIdConnClosedGracefully do
 ShowMessage('Indy reports, that connection was closed gracefully!');
 // this exception class covers all the low level socket exceptions
 on E: EIdSocketError do
 ShowMessage('Indy raised a socket error!' + sLineBreak +
 'Error code: ' + IntToStr(E.LastError) + sLineBreak +
 'Error message' + E.Message);
 // this exception class covers all exceptions thrown by Indy library
 on E: EIdException do
 ShowMessage('Indy raised an exception!' + sLineBreak +
 'Exception class: ' + E.ClassName + sLineBreak +
 'Error message: ' + E.Message);
 // this exception class is a base Delphi exception class and covers here
 // all exceptions different from those listed above
 on E: Exception do
 ShowMessage('A non-Indy related exception has been raised!');
 end;
 finally
 IdHTTP.Free;
 resp.Free;
 end;
end;

Cek status koneksi domain dengan menggunakan script delphi

Kode untuk cek status koneksi domain dengan menggunakan script delphi

uses
 IdHTTP, IdException, IdStack;

procedure TForm1.Button1Click(Sender: TObject);
var
 IdHTTP: TIdHTTP;
begin
 IdHTTP:= TIdHttp.Create(Application);
 try
 IdHTTP.host:=('google.ru');
 IdHTTP.connect;
 if IdHTTP.Connected=false then
 begin
 ShowMessage('Connected false');
 IdHTTP.Disconnect;
 end
 else
 begin
 ShowMessage('Connected true');
 end;
 except
 ShowMessage('Host not found');
 end;
end;

Cek Alamat Domain Dengan Delphi 7

Melakukan pengecekan alamat domain dengan menggunakan bahasa pemrograman delphi 7, berikut adalah cuplikan kodenya.

uses
 IdHTTP, IdException, IdStack;

procedure TForm1.Button1Click(Sender: TObject);
var
 IdHTTP: TIdHTTP;
begin
 IdHTTP := TIdHTTP.Create(Self);
 try
 try
 IdHTTP.Get('http://www.example.com');
 except
 // this exception class covers the HTTP protocol errors; you may read the
 // response code using ErrorCode property of the exception object, or the
 // same you can read from the ResponseCode property of the TIdHTTP object
 on E: EIdHTTPProtocolException do
 ShowMessage('Indy raised a protocol error!' + sLineBreak +
 'HTTP status code: ' + IntToStr(E.ReplyErrorCode) + sLineBreak +
 'Error message' + E.Message);
 // this exception class covers the cases when the server side closes the
 // connection with a client in a "peaceful" way
 on E: EIdConnClosedGracefully do
 ShowMessage('Indy reports, that connection was closed gracefully!');
 // this exception class covers all the low level socket exceptions
 on E: EIdSocketError do
 ShowMessage('Indy raised a socket error!' + sLineBreak +
 'Error code: ' + IntToStr(E.LastError) + sLineBreak +
 'Error message' + E.Message);
 // this exception class covers all exceptions thrown by Indy library
 on E: EIdException do
 ShowMessage('Indy raised an exception!' + sLineBreak +
 'Exception class: ' + E.ClassName + sLineBreak +
 'Error message: ' + E.Message);
 // this exception class is a base Delphi exception class and covers here
 // all exceptions different from those listed above
 on E: Exception do
 ShowMessage('A non-Indy related exception has been raised!');
 end;
 finally
 IdHTTP.Free;
 end;
end;

Sederhana tinggal copas, selamat mempelajari. wassalam.

Adoquery pencarian tanggal awal dan akhir

Berikut adalah contoh kode untuk pencarian di antara dua tanggal, biasanya banyak dipergunakan untuk laporan.

 ADOQuery.Active := false;
 ADOQuery.SQL.Clear;
 ADOQuery.SQL.Add('Select * from penjualan_item_log');
 ADOQuery.SQL.Add('left join penjualan_log on penjualan_log.kode_transaksi = penjualan_item_log.kode_transaksi');
 ADOQuery.SQL.Add('where penjualan_log.tanggal >= FORMAT(:TanggalAwal, "dd-MM-yyyy") and penjualan_log.tanggal <= FORMAT(:TanggalAkhir, "dd-MM-yyyy")');
 ADOQuery.Parameters.ParamByName('TanggalAwal').Value := DateTimePickerTanggalAwal.Date;
 ADOQuery.Parameters.ParamByName('TanggalAkhir').Value := DateTimePickerTanggalAkhir.Date;
 ADOQuery.Active := true;

karena komponen adoquery

pake ADOQuery.Parameters.ParamByName('TanggalAwal').Value

maka menggunakan bantuan

FORMAT(:TanggalAwal, "dd-MM-yyyy"),

yang merupakan fungsi format konversi dari bawaan ms access.

Komponen Raw Printer Delphi 7

Bahasa pemrograman delphi 7 sudah memiliki fasilitas pencetakan laporan yang cukup bagus, akan tetapi belum memiliki komponen untuk melakukan pencetakan secara baris seperti pada kertas struck dengan printer dot matrix. Walaupun demikian delphi 7 tetap dapat melakukan cetak baris atau istilah lainnya adala raw printer, dengan cara pengkodean langsung, itupun masih terdapat kekurangan.

Setelah melakukan pencarian akhirnya menemukan komponen yang sudah cukup bagus dan bisa berjalan digunakan. Nama komponen tersebut adalah RawPrinter. Printer yang sudah di coba di pergunakan adalah printer dari Epson type EPSON LX-300+ /II.

Download komponen rawprinter.
http://www.4shared.com/zip/KIjT4TMbba/RAWPrinter.html