[英]libgdx's http request method doesn't work on the web application
我正在測試libgdx庫的net類,以便從傳遞給setUrl方法的頁面獲取json。 這部分代碼來自一個名為ApplicationScreen的類,它實現了libgdx Screen界面。 那是代碼:
@Override
public void show() {
String requestContent = null;
httpRequest = new HttpRequest(Net.HttpMethods.GET);
httpRequest.setUrl("http://localhost/projects/SqlParser/index.php");
httpRequest.setHeader("Content-Type", "text/plain");
httpRequest.setContent(requestContent);
Gdx.net.sendHttpRequest(httpRequest, new HttpResponseListener() {
public void handleHttpResponse(HttpResponse httpResponse) {
final int statusCode = httpResponse.getStatus().getStatusCode();
System.out.println("HTTP Request status: " + statusCode);
System.out.println("Content:");
System.out.println(httpResponse.getResultAsString());
}
public void failed(Throwable t) {
System.out.println("HTTP request failed!");
}
});
}
當我運行桌面版本時,我在控制台窗口“HTTP請求狀態:200”上收到此消息,然后是http請求的內容,因此代碼執行了我期望它應該執行的操作。 當我運行html版本時,我在控制台“HTTP請求狀態:0”上收到此消息。 有什么建議嗎?
你在android清單中設置了權限嗎?
還需要檢查您在傳遞url時使用的是您正在使用的計算機的ip而不是localhost。
希望這有一些幫助......即使它早就應該了。
這可能為時已晚,但就我而言,事實證明這是違反同源政策的行為。
我使用不安全版本的Google Chrome解決了這個問題,使用以下選項啟動瀏覽器: - allow-file-access-from-files --disable-web-security
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.