cari
Rumahphp教程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>

Kenyataan
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

AI Hentai Generator

AI Hentai Generator

Menjana ai hentai secara percuma.

Alat panas

SublimeText3 versi Inggeris

SublimeText3 versi Inggeris

Disyorkan: Versi Win, menyokong gesaan kod!

Penyesuai Pelayan SAP NetWeaver untuk Eclipse

Penyesuai Pelayan SAP NetWeaver untuk Eclipse

Integrasikan Eclipse dengan pelayan aplikasi SAP NetWeaver.

Versi Mac WebStorm

Versi Mac WebStorm

Alat pembangunan JavaScript yang berguna

SublimeText3 Linux versi baharu

SublimeText3 Linux versi baharu

SublimeText3 Linux versi terkini

MinGW - GNU Minimalis untuk Windows

MinGW - GNU Minimalis untuk Windows

Projek ini dalam proses untuk dipindahkan ke osdn.net/projects/mingw, anda boleh terus mengikuti kami di sana. MinGW: Port Windows asli bagi GNU Compiler Collection (GCC), perpustakaan import yang boleh diedarkan secara bebas dan fail pengepala untuk membina aplikasi Windows asli termasuk sambungan kepada masa jalan MSVC untuk menyokong fungsi C99. Semua perisian MinGW boleh dijalankan pada platform Windows 64-bit.