搜尋
首頁php教程php手册[PHP] java讀取PHP介面數據

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

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前By尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
1 個月前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。