我有幾個PHP sripts保存在我的/ var/www /目錄中。我可以從我的瀏覽器運行它們,但Java無法看到它們。PHP腳本不是由Java找到,但在瀏覽器中工作
下面是瀏覽器輸出:
而且從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);
?>
有人知道爲什麼會發生這種情況嗎?
似乎你缺少調用腳本的'.php'。 – NilsH
你可能有一個拼寫錯誤,瀏覽器中的PHP文件顯示'get_print_profile_ids',你正在調用'get_profile_ids' ... – akluth
我有所有這些代碼在不同的機器上工作,我不認爲.php是必需的。 .. – TomSelleck