JSON 데이터를 읽는 Android와 동일합니다
PHP 파일 :
<? phpclass test {// log path const log_path = "e : /phpserver/apache/logs//error.log"; // const pages = 50으로 표시된 행 수; public static function main () {헤더 ( "content-type : text/html; charset = utf-8"); if (! empty ($ _ get [ 'action'])) {if (! method_exists ( 'test', $ _ get [ 'action'])) {echo "404"; } else {self :: $ _ get [ 'action'] (); } 출구; }} public static function showapachelogs () {$ test = new test (); $ result = $ test-> readLogs (self :: log_path, self :: pages); $ json = array (); for ($ i = 0; $ i <count ($ result); $ i ++) {$ line = $ result [$ i]; // 처리가 처리되면 JSON 구문 분석이 $ line = str_replace ( "/r/n", "", $ line)에 실패합니다. $ result [$ i] = 배열 ( "num"=> $ i+1, "msg"=> urlencode ($ line)); } $ str = stripSlashes (urldecode (json_encode ($ result))); echo $ str; } / *** 읽기 로그* / 개인 기능 readLogs ($ filepath, $ num = 20) {$ fp = fopen ($ filepath, "r"); $ pos = -2; $ eof = ""; $ head = false; // 총 행 수가 NUM보다 작을 때 첫 번째 행에 도달하는지 $ line = array (); while ($ num> 0) {while ($ eof! = "/n") {if (fseek ($ fp, $ pos, seek_end) == 0) {// fseek가 0을 성공적으로 반환하고 -1 $ eof = fgetc ($ fp)를 반환하지 않습니다. $ pos-; } else {// 첫 번째 줄과 첫 번째 줄에 도달하면 $ pos를 설정하면 fseek ($ fp, 0, seek_set); $ head = true; // 파일의 헤드에 도달하면 스위치가 깨집니다. }} array_unshift ($ line, fgets ($ fp)); if ($ head) {break; } // 파일의 헤더에 도달 한 후 첫 번째 줄을 읽은 다음 전체 루프 $ eof = ""; $ num-; } fclose ($ fp); 반환 array_reverse ($ line); }} test :: main ();Java 파일 :
import java.io.inputStream; import java.net.httpurlConnection; import java.net.url import java.net.url; import org.json.json.json.json.json.json.json.json.jsonarray; import org.json.jsonobject; public class readlogs {public static void main (String [] args) 예외 {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); // outputs renate inputStream input = conn.getInputStream (); int reslen = 0; 바이트 [] res = 새로운 바이트 [1024]; StringBuilder sb = new StringBuilder (); while ((reslen = input.read (res))! = -1) {sb.append (new String (res, 0, reslen)); } 문자열 jsonsted = sb.tostring (); // String은 JSON jsonArray JSONARRAY = NEW JSONARRAY (JSONST)로 변환됩니다. for (int i = 0; i <jsonarray.length (); i ++) {jsonobject jsonobject = new jsonobject (jsonarray.getstring (i)); 문자열 msg = (string) jsonobject.get ( "msg"); int num = (int) jsonobject.get ( "num"); System.out.println (num+":"+msg); }}}Java 읽기 PHP 인터페이스 데이터를 구현하는 위의 방법은 내가 공유 한 모든 컨텐츠입니다. 나는 당신이 당신에게 참조를 줄 수 있기를 바랍니다. 그리고 당신이 wulin.com을 더 지원할 수 있기를 바랍니다.