It's the same as Android, reading json data
PHP file:
<?phpclass Test{ //Log path const LOG_PATH="E:/phpServer/Apache/logs//error.log"; //Number of rows displayed 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; } } public static function showApacheLogs(){ $test=new Test(); $result=$test->readLogs(self::LOG_PATH,self::PAGES); $json=array(); for($i=0;$i<count($result);$i++){ $line=$result[$i]; //Note here, if the processing is processed, json parsing will fail $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 number of rows is less than Num, determine whether it reaches the first row $lines = array(); while($num>0){ while($eof != "/n"){ if(fseek($fp, $pos, SEEK_END)==0){ //fseek successfully returns 0, and fails to return -1 $eof = fgetc($fp); $pos--; }else{ //When it reaches the first line and the first line, setting $pos fails fseek($fp,0,SEEK_SET); $head = true; //When it reaches the head of the file, the switch opens break; } } array_unshift($lines,fgets($fp)); if($head){ break; } //This sentence can only be put after one sentence, because after reaching the header of the file, read the first line and then jump out of the entire loop $eof = ""; $num--; } fclose($fp); return array_reverse($lines); }}Test::main();Java files:
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://localhost/test.php?action=showApacheLogs"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(10000); conn.setRequestMethod("GET"); conn.setDoInput(true); conn.setDoOutput(true); // Output returns the 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 is 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 method of implementing Java reading PHP interface data is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.