和安卓是一个道理,读取json数据
PHP文件:
<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Test{ </span><span style="color: #008000;">//</span><span style="color: #008000;">日志路径</span> <span style="color: #0000ff;">const</span> LOG_PATH="E:\phpServer\Apache\logs\\error.log"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;">显示的行数</span> <span style="color: #0000ff;">const</span> PAGES=50<span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> main(){ </span><span style="color: #008080;">header</span>("content-type:text/html;charset=utf-8"<span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$_GET</span>['action'<span style="color: #000000;">])){ </span><span style="color: #0000ff;">if</span>(!<span style="color: #008080;">method_exists</span>('Test',<span style="color: #800080;">$_GET</span>['action'<span style="color: #000000;">])){ </span><span style="color: #0000ff;">echo</span> "404"<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ self</span>::<span style="color: #800080;">$_GET</span>['action'<span style="color: #000000;">](); } </span><span style="color: #0000ff;">exit</span><span style="color: #000000;">; } } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> showApacheLogs(){ </span><span style="color: #800080;">$test</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> Test(); </span><span style="color: #800080;">$result</span>=<span style="color: #800080;">$test</span>->readLogs(self::LOG_PATH,self::<span style="color: #000000;">PAGES); </span><span style="color: #800080;">$json</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span>count(<span style="color: #800080;">$result</span>);<span style="color: #800080;">$i</span>++<span style="color: #000000;">){ </span><span style="color: #800080;">$line</span>=<span style="color: #800080;">$result</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">]; </span><span style="color: #008000;">//</span><span style="color: #008000;">注意这里,如果处理会json解析失败</span> <span style="color: #800080;">$line</span>=<span style="color: #008080;">str_replace</span>("\r\n", "", <span style="color: #800080;">$line</span><span style="color: #000000;">); </span><span style="color: #800080;">$result</span>[<span style="color: #800080;">$i</span>]=<span style="color: #0000ff;">array</span>("num"=><span style="color: #800080;">$i</span>+1,"msg"=><span style="color: #008080;">urlencode</span>(<span style="color: #800080;">$line</span><span style="color: #000000;">)); } </span><span style="color: #800080;">$str</span>=<span style="color: #008080;">stripslashes</span>(<span style="color: #008080;">urldecode</span>(json_encode(<span style="color: #800080;">$result</span><span style="color: #000000;">))); </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$str</span><span style="color: #000000;">; } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 读取日志 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> readLogs(<span style="color: #800080;">$filePath</span>,<span style="color: #800080;">$num</span>=20<span style="color: #000000;">){ </span><span style="color: #800080;">$fp</span> = <span style="color: #008080;">fopen</span>(<span style="color: #800080;">$filePath</span>,"r"<span style="color: #000000;">); </span><span style="color: #800080;">$pos</span> = -2<span style="color: #000000;">; </span><span style="color: #800080;">$eof</span> = ""<span style="color: #000000;">; </span><span style="color: #800080;">$head</span> = <span style="color: #0000ff;">false</span>; <span style="color: #008000;">//</span><span style="color: #008000;">当总行数小于Num时,判断是否到第一行了 </span> <span style="color: #800080;">$lines</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$num</span>>0<span style="color: #000000;">){ </span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$eof</span> != "\n"<span style="color: #000000;">){ </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">fseek</span>(<span style="color: #800080;">$fp</span>, <span style="color: #800080;">$pos</span>, SEEK_END)==0){ <span style="color: #008000;">//</span><span style="color: #008000;">fseek成功返回0,失败返回-1 </span> <span style="color: #800080;">$eof</span> = <span style="color: #008080;">fgetc</span>(<span style="color: #800080;">$fp</span><span style="color: #000000;">); </span><span style="color: #800080;">$pos</span>--<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span>{ <span style="color: #008000;">//</span><span style="color: #008000;">当到达第一行,行首时,设置$pos失败 </span> <span style="color: #008080;">fseek</span>(<span style="color: #800080;">$fp</span>,0,<span style="color: #000000;">SEEK_SET); </span><span style="color: #800080;">$head</span> = <span style="color: #0000ff;">true</span>; <span style="color: #008000;">//</span><span style="color: #008000;">到达文件头部,开关打开 </span> <span style="color: #0000ff;">break</span><span style="color: #000000;">; } } </span><span style="color: #008080;">array_unshift</span>(<span style="color: #800080;">$lines</span>,<span style="color: #008080;">fgets</span>(<span style="color: #800080;">$fp</span><span style="color: #000000;">)); </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$head</span>){ <span style="color: #0000ff;">break</span>; } <span style="color: #008000;">//</span><span style="color: #008000;">这一句,只能放上一句后,因为到文件头后,把第一行读取出来再跳出整个循环 </span> <span style="color: #800080;">$eof</span> = ""<span style="color: #000000;">; </span><span style="color: #800080;">$num</span>--<span style="color: #000000;">; } </span><span style="color: #008080;">fclose</span>(<span style="color: #800080;">$fp</span><span style="color: #000000;">); </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">array_reverse</span>(<span style="color: #800080;">$lines</span><span style="color: #000000;">); } } Test</span>::main();
java文件:
<span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.InputStream; </span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.net.HttpURLConnection; </span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.net.URL; </span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.json.JSONArray; </span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.json.JSONObject; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> ReadLogs { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> main(String[] args) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception { URL url </span>= <span style="color: #0000ff;">new</span> URL("http://localhost/test.php?action=showApacheLogs"<span style="color: #000000;">); HttpURLConnection conn </span>=<span style="color: #000000;"> (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(</span>10000<span style="color: #000000;">); conn.setRequestMethod(</span>"GET"<span style="color: #000000;">); conn.setDoInput(</span><span style="color: #0000ff;">true</span><span style="color: #000000;">); conn.setDoOutput(</span><span style="color: #0000ff;">true</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 输出返回结果</span> InputStream input =<span style="color: #000000;"> conn.getInputStream(); </span><span style="color: #0000ff;">int</span> resLen =0<span style="color: #000000;">; </span><span style="color: #0000ff;">byte</span>[] res = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">byte</span>[1024<span style="color: #000000;">]; StringBuilder sb</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> StringBuilder(); </span><span style="color: #0000ff;">while</span>((resLen=input.read(res))!=-1<span style="color: #000000;">){ sb.append(</span><span style="color: #0000ff;">new</span> String(res, 0<span style="color: #000000;">, resLen)); } String jsonStr</span>=<span style="color: #000000;">sb.toString(); </span><span style="color: #008000;">//</span><span style="color: #008000;">String转换成JSON</span> JSONArray jsonArray=<span style="color: #0000ff;">new</span><span style="color: #000000;"> JSONArray(jsonStr); </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i=0;i<jsonarray.length style="color: #000000;">){ JSONObject jsonObject=<span style="color: #0000ff;">new</span><span style="color: #000000;"> JSONObject(jsonArray.getString(i)); String msg</span>=(String) jsonObject.get("msg"<span style="color: #000000;">); </span><span style="color: #0000ff;">int</span> num=(<span style="color: #0000ff;">int</span>) jsonObject.get("num"<span style="color: #000000;">); System.out.println(num</span>+":"+<span style="color: #000000;">msg); } } }</span></jsonarray.length>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
4 weeks agoByDDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
1 months agoByDDD
Two Point Museum: All Exhibits And Where To Find Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor
