And Android is a reason to read JSON data
PHP File:
<?php class test{//log path const log_path= "E:\phpServer\Apache\logs\\error.log";
The number of rows displayed is const PAGES=50;
public static function main () {header ("content-type:text/html;charset=utf-8");
if (!empty ($_get[' action ')) {if (!method_exists (' Test ', $_get[' action ')) {echo "404";
}else{self::$_get[' action '] ();
} exit;
The public static function Showapachelogs () {$test =new test ();
$result = $test->readlogs (self::log_path,self::P ages);
$json =array ();
For ($i =0 $i <count ($result); $i + +) {$line = $result [$i];
Note here if the processing will parse the JSON failure $line =str_replace ("\ r \ n", "", $line);
$result [$i]=array ("num" => $i +1, "msg" =>urlencode ($line));
$str =stripslashes (UrlDecode (Json_encode ($result)));
Echo $str;
/** * Read Log/Private function readlogs ($filePath, $num =20) {$fp = fopen ($filePath, "R");
$pos =-2;
$eof = ""; $head = false; When the total row count is less than NUM, the first line is judged $lInes = Array ();
while ($num >0) {while ($eof!= "\ n") {if (Fseek ($fp, $pos, seek_end) ==0) {//fseek successfully returns 0, failure returns-1
$eof = fgetc ($FP);
$pos--;
}else{//When the first row is reached, the beginning of the line sets the $pos failure fseek ($FP, 0,seek_set); $head = true;
The file head is reached, the switch opens the break;
} array_unshift ($lines, Fgets ($fp)); if ($head) {break;}
This sentence, can only put on a sentence, because to the head of the file, the first line read out and then jump out of the whole cycle $eof = "";
$num--;
} fclose ($FP);
Return Array_reverse ($lines); } test::main ();
java file:
Import Java.io.InputStream;
Import java.net.HttpURLConnection;
Import Java.net.URL;
Import Org.json.JSONArray;
Import Org.json.JSONObject; public class Readlogs {public static void main (string[] args) throws Exception {URL url = new URL ("Http://localhos
T/test.php?action=showapachelogs ");
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
Conn.setconnecttimeout (10000);
Conn.setrequestmethod ("get");
Conn.setdoinput (TRUE);
Conn.setdooutput (TRUE);
Output return result InputStream input = Conn.getinputstream ();
int reslen = 0;
byte[] res = new byte[1024];
StringBuilder sb=new StringBuilder ();
while ((Reslen=input.read (res))!=-1) {sb.append (New String (res, 0, Reslen));
} String jsonstr=sb.tostring ();
String converted to JSON Jsonarray jsonarray=new Jsonarray (JSONSTR);
for (int i=0;i<jsonarray.length (); i++) {jsonobject jsonobject=new jsonobject (jsonarray.getstring (i)); String msg= (String) jsonobjecT.get ("msg");
int num= (int) jsonobject.get ("num");
System.out.println (num+ ":" +msg); }
}
}
The above Java reading PHP interface data implementation method is small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.