我想使用libGDX
检查网络连接,并尝试使用以下代码。
HttpRequestBuilder requestBuilder = new HttpRequestBuilder();
Net.HttpRequest httpRequest = requestBuilder.newRequest().method(Net.HttpMethods.GET).url("https://api.vk.com/").build();
Net.HttpResponseListener httpResponseListener = new httpResponseListener() {
public void handleHttpResponse (Net.HttpResponse httpResponse) {
HttpStatus status = httpResponse.getStatus();
if (status.getStatusCode() >= 200 && status.getStatusCode() < 300) {
// it was successful
} else {
// do something else
}
}
}
Gdx.net.sendHttpRequest(httpRequest, httpResponseListener);
它有几个错误,例如Gdx.net
现在没有sendHttpRequest()
功能。我该如何更正此代码?
答案 0 :(得分:1)
Gdx.net
有sendHttpRequest()
方法 - 您似乎使用的不是HttpRequest
实例(为什么Net...
以大写字母开头?)
关于如何使用Gdx.net
的一个适当的最小示例是 - 我还在必要时添加了评论
public class HttpManager implements HttpResponseListener
{
public HttpState state;
private String result;
private byte[] byteResult;
HttpRequest request;
public HttpManager()
{
request = new HttpRequest();
request.setMethod(Net.HttpMethods.GET); //or POST
request.setContent(""); //you can put here some PUT/GET content
request.setUrl(url);
Gdx.net.sendHttpRequest(request, this);
}
@Override
public void handleHttpResponse(HttpResponse httpResponse)
{
if( httpResponse.getStatus().getStatusCode() != 200 )
{
//ERROR
float errorCode = httpResponse.getStatus().getStatusCode();
}
else
{
byte[] byteResult = httpResponse.getResult(); //you can also get result as String by using httpResponse.getResultAsString();
}
}
@Override
public void failed(Throwable t)
{
// TODO Auto-generated method stub
}
@Override
public void cancelled()
{
// TODO Auto-generated method stub
}
}