和安卓是一个道理,读取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>

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

mPDF
mPDF ist eine PHP-Bibliothek, die PDF-Dateien aus UTF-8-codiertem HTML generieren kann. Der ursprüngliche Autor, Ian Back, hat mPDF geschrieben, um PDF-Dateien „on the fly“ von seiner Website auszugeben und verschiedene Sprachen zu verarbeiten. Es ist langsamer und erzeugt bei der Verwendung von Unicode-Schriftarten größere Dateien als Originalskripte wie HTML2FPDF, unterstützt aber CSS-Stile usw. und verfügt über viele Verbesserungen. Unterstützt fast alle Sprachen, einschließlich RTL (Arabisch und Hebräisch) und CJK (Chinesisch, Japanisch und Koreanisch). Unterstützt verschachtelte Elemente auf Blockebene (wie P, DIV),

Sicherer Prüfungsbrowser
Safe Exam Browser ist eine sichere Browserumgebung für die sichere Teilnahme an Online-Prüfungen. Diese Software verwandelt jeden Computer in einen sicheren Arbeitsplatz. Es kontrolliert den Zugriff auf alle Dienstprogramme und verhindert, dass Schüler nicht autorisierte Ressourcen nutzen.

MinGW – Minimalistisches GNU für Windows
Dieses Projekt wird derzeit auf osdn.net/projects/mingw migriert. Sie können uns dort weiterhin folgen. MinGW: Eine native Windows-Portierung der GNU Compiler Collection (GCC), frei verteilbare Importbibliotheken und Header-Dateien zum Erstellen nativer Windows-Anwendungen, einschließlich Erweiterungen der MSVC-Laufzeit zur Unterstützung der C99-Funktionalität. Die gesamte MinGW-Software kann auf 64-Bit-Windows-Plattformen ausgeführt werden.

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 Linux neue Version
SublimeText3 Linux neueste Version
