2013-04-16 40 views
0

我有幾個PHP sripts保存在我的/ var/www /目錄中。我可以從我的瀏覽器運行它們,但Java無法看到它們。PHP腳本不是由Java找到,但在瀏覽器中工作

下面是瀏覽器輸出:

enter image description here

而且從Java:

DB: Error executing script: get_profile_ids 
HTTP/1.1 404 Not Found [Date: Tue, 16 Apr 2013 11:52:40 GMT, Server: Apache/2.2.22 (Ubuntu), Vary: Accept-Encoding, Content-Length: 288, Keep-Alive: timeout=5, max=100, Connection: Keep-Alive, Content-Type: text/html; charset=iso-8859-1] 
DB: Result: 

我的Java代碼:

public class ServerTest { 

    public static void main(String [] args) { 

     callPHPScript("get_print_profile_ids", new ArrayList<NameValuePair>()); 

    } 

    public static String callPHPScript(String scriptName, List<NameValuePair> parameters) { 
     HttpClient client = new DefaultHttpClient(); 
     HttpPost post = new HttpPost("http://localhost/" + scriptName); 
     String line = ""; 
     StringBuilder stringBuilder = new StringBuilder(); 
     try { 
      post.setEntity(new UrlEncodedFormEntity(parameters)); 

      HttpResponse response = client.execute(post); 
      if (response.getStatusLine().getStatusCode() != 200) 
      { 
       System.out.println("DB: Error executing script: " + scriptName); 
       System.out.println(response.toString()); 
      } 
      else { 
       BufferedReader rd = new BufferedReader(new InputStreamReader(
        response.getEntity().getContent())); 
       line = ""; 
       while ((line = rd.readLine()) != null) { 
        stringBuilder.append(line); 
       } 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     System.out.println("DB: Result: " + stringBuilder.toString()); 
     return stringBuilder.toString(); 
    } 
} 

我的PHP腳本:

<?php 
include('connect_db.php'); 
include('tools.php'); 

$query = 'SELECT id FROM fingerprint_profiles'; 

$result = mysql_query($query); 
echo($result); 

?> 

有人知道爲什麼會發生這種情況嗎?

+0

似乎你缺少調用腳本的'.php'。 – NilsH

+0

你可能有一個拼寫錯誤,瀏覽器中的PHP文件顯示'get_print_profile_ids',你正在調用'get_profile_ids' ... – akluth

+0

我有所有這些代碼在不同的機器上工作,我不認爲.php是必需的。 .. – TomSelleck

回答

5

你缺少.php爲,變化:
HttpPost post = new HttpPost("http://localhost/" + scriptName);

HttpPost post = new HttpPost("http://localhost/" + scriptName + ".php");

+0

它看起來像是這個問題!我不明白,但我有所有這些代碼在不同的機器上工作,我從來沒有把.php放入.. – TomSelleck

+0

這可能是您的其他機器配置不同,以便在服務器上自動添加.php。 –

1

這裏的提示:

System.out.println("DB: Error executing script: " + scriptName); 

正在記錄

DB: Error executing script: get_profile_ids 

改變您的測試呼叫

callPHPScript("get_profile_ids.php", new ArrayList<NameValuePair>()); 

,你可能會得到一個成功的結果。

1

Java代碼調用get_profile_ids而您的瀏覽器截圖顯示get_profile_ids.php。改變你的Java代碼也可以調用get_profile_ids.php

+0

對不起,這是我的測試代碼中的拼寫錯誤,修復了 – TomSelleck

相關問題