检查libGDX中的网络状态

时间:2015-04-22 10:32:48

标签: libgdx android-wifi android-networking

我想在libGDX项目中查看Wifi stat,尝试使用以下代码:

   Gdx.net.sendHttpRequest(HttpRequest httpRequest, HttpResponseListener httpResponseListener);

    HttpResponseListener httpResponseListener = new  httpResponseListener() {
        public void handleHttpResponse (HttpResponse httpResponse) {
            HttpStatus status = httpResponse.getStatus();
            if (status.getStatusCode() >= 200 && status.getStatusCode() < 300) {
                // it was successful
            } else {
                // do something else
            }
        }
    }

我添加了权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

它说HttpResponseListener cannot resolved。 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

// there is a small 'h' here, which is wrong ---v
HttpResponseListener httpResponseListener = new httpResponseListener() {
    public void handleHttpResponse (HttpResponse httpResponse) {
        HttpStatus status = httpResponse.getStatus();
        if (status.getStatusCode() >= 200 && status.getStatusCode() < 300) {
            // it was successful
        } else {
            // do something else
        }
    }
}

// here, you still have the parameter types
Gdx.net.sendHttpRequest(HttpRequest httpRequest, HttpResponseListener httpResponseListener);
// this is correct
Gdx.net.sendHttpRequest(httpRequest, httpResponseListener);

除此之外,这可能不起作用。如果没有互联网连接,您将无法从服务器获得响应。您必须覆盖HttpResponseListener.failed()