首頁 > php教程 > php手册 > 主體

[PHP] java讀取PHP介面數據

WBOY
發布: 2016-08-04 08:53:15
原創
2074 人瀏覽過

和安卓是一个道理,读取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><<span style="color: #008080;">count</span>(<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();i++<span style="color: #000000;">){
            JSONObject jsonObject</span>=<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>
登入後複製

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板