Itu sama dengan android, membaca data json
File PHP:
<? phpclass test {// log path const log_path = "e: /phpserver/apache/logs//error.log"; // jumlah baris yang ditampilkan const halaman = 50; fungsi statis publik () {header ("tipe konten: teks/html; charset = utf-8"); if (! kosong ($ _ dapatkan ['action'])) {if (! Method_exists ('test', $ _ dapatkan ['action'])) {echo "404"; } else {self :: $ _ get ['action'] (); } KELUAR; }} fungsi statis public showapachelogs () {$ test = test baru (); $ result = $ test-> readlogs (self :: log_path, self :: pages); $ json = array (); untuk ($ i = 0; $ i <count ($ hasil); $ i ++) {$ line = $ hasil [$ i]; // Catatan di sini, jika pemrosesan diproses, parsing JSON akan gagal $ line = str_replace ("/r/n", "", $ line); $ result [$ i] = array ("num" => $ i+1, "msg" => urlencode ($ line)); } $ str = stripSlashes (urldecode (json_encode ($ result))); Echo $ str; } / *** Baca Log* / Fungsi Pribadi Readlogs ($ FilePath, $ num = 20) {$ fp = fopen ($ filepath, "r"); $ pos = -2; $ eof = ""; $ head = false; // Ketika jumlah total baris kurang dari NUM, tentukan apakah itu mencapai baris pertama $ lines = array (); while ($ num> 0) {while ($ eof! = "/n") {if (fseek ($ fp, $ pos, seek_end) == 0) {// fseek berhasil mengembalikan 0, dan gagal mengembalikan -1 $ eof = fgetc ($ fp); $ pos--; } else {// Ketika mencapai baris pertama dan baris pertama, pengaturan $ POS gagal fseek ($ fp, 0, seekt_set); $ head = true; // Ketika mencapai kepala file, sakelar membuka istirahat; }} array_unshift ($ lines, fgets ($ fp)); if ($ head) {break; } // Kalimat ini hanya dapat diajukan setelah satu kalimat, karena setelah mencapai header file, baca baris pertama dan kemudian melompat keluar dari seluruh loop $ eof = ""; $ num--; } fclose ($ fp); return array_reverse ($ lines); }} Test :: main ();File Java:
import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import org.json.JSONArray;import org.json.JSONObject;public class ReadLogs { public static void main(String[] args) throws Exception { URL url = new Url ("http: //localhost/test.php? Action = showapachelogs"); HttpurlConnection conn = (httpurlconnection) url.openconnection (); Conn.SetConnectTimeout (10000); Conn.setRequestMethod ("Get"); Conn.setDoInput (true); Conn.SetDoOutput (true); // output Mengembalikan hasil inputStream input = conn.getInputStream (); int reslen = 0; byte [] res = byte baru [1024]; StringBuilder SB = StringBuilder baru (); while ((reslen = input.read (res))! =-1) {sb.append (string baru (res, 0, reslen)); } String jsonstr = sb.toString (); // String dikonversi ke json jsonarray jsonarray = new jsonarray (jsonstr); untuk (int i = 0; i <jsonarray.length (); i ++) {jsonObject jsonObject = new jsonObject (jsonarray.getString (i)); String msg = (string) jsonobject.get ("msg"); int num = (int) jsonobject.get ("num"); System.out.println (num+":"+msg); }}}Metode di atas menerapkan data antarmuka Java membaca PHP adalah semua konten yang telah saya bagikan dengan Anda. Saya harap Anda dapat memberi Anda referensi dan saya harap Anda dapat mendukung wulin.com lebih lanjut.