Java 类com.badlogic.gdx.Net.HttpRequest 实例源码

项目:libgdxcn    文件:HttpRequestExample.java   
@Override
public void create () {
    HttpRequest request = new HttpRequest(HttpMethods.GET);
    request.setUrl("http://libgdx.badlogicgames.com/nightlies/dist/AUTHORS");
    Gdx.net.sendHttpRequest(request, new HttpResponseListener() {
        @Override
        public void handleHttpResponse (HttpResponse httpResponse) {
            Gdx.app.log("HttpRequestExample", "response: " + httpResponse.getResultAsString());
        }

        @Override
        public void failed (Throwable t) {
            Gdx.app.error("HttpRequestExample", "something went wrong", t);
        }

        @Override
        public void cancelled () {
            Gdx.app.log("HttpRequestExample", "cancelled");
        }
    });
}
项目:NerdShooter    文件:NetworkHandle.java   
protected void sendRequest(String method, Map<String, String> params, final NetworkListener listener){
    HttpRequest httpPost = new HttpRequest(HttpMethods.POST);
    httpPost.setUrl(website + method + ".php");
    httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
    httpPost.setContent(HttpParametersUtils.convertHttpParameters(params));

    Gdx.net.sendHttpRequest (httpPost, new HttpResponseListener() {
        public void handleHttpResponse(Net.HttpResponse httpResponse) {
            String responce = new String(httpResponse.getResult());
            String dat = decrypt(responce, ENC_KEY);
            listener.finished(dat);
            listener.length(dat.getBytes().length);
        }

        public void failed(Throwable t) {
            listener.failed(t);
        }

        public void cancelled(){

        }
    });
}
项目:gdx-http    文件:DefaultHttpClient.java   
@Override
public void sendHttpRequest(HttpRequest httpRequest, HttpResponseListener httpResponseListener) {

    URI uri = URI.create(httpRequest.getUrl());

    if (cookieMgr != null && !cookieMgr.isEmpty()) {
        String tempCookie = cookieMgr.getHeaderPayload(uri);
        httpRequest.setHeader(HttpClient.HeaderFields.COOKIE, tempCookie);
    }

    if (headerFields.size > 0) {
        for (Entry<String, String> entry : headerFields.entries()) {
            httpRequest.setHeader(entry.key, entry.value);
        }
    }
    // Listener instantiation below...
    Gdx.net.sendHttpRequest(httpRequest, new ResponseWrapper(httpResponseListener, uri));
}
项目:gaiasky    文件:DesktopNetworkChecker.java   
private void urlCheck(final String url, final LinkListener listener) {
    HttpRequest request = new HttpRequest(HttpMethods.GET);
    request.setUrl(url);
    request.setTimeOut(TIMEOUT_MS);
    Gdx.net.sendHttpRequest(request, new HttpResponseListener() {
        @Override
        public void handleHttpResponse(HttpResponse httpResponse) {
            if (httpResponse.getStatus().getStatusCode() == HttpStatus.SC_OK) {
                listener.ok(url);
            } else {
                listener.ko(url);
            }
        }

        @Override
        public void failed(Throwable t) {
            listener.ko(url);
        }

        @Override
        public void cancelled() {
            listener.ko(url);
        }
    });

}
项目:ead    文件:GATracker.java   
/**
 * Sends the given payload to backend
 */
private void ga(String payload) {
    HttpRequest request = new HttpRequest("POST");
    request.setContent("v=1&tid=" + tracker + "&an=eAdventureDesktop&av="
            + version + "&cid=" + cid + "&" + payload);
    Gdx.net.sendHttpRequest(request, new HttpResponseListener() {
        @Override
        public void handleHttpResponse(HttpResponse httpResponse) {

        }

        @Override
        public void failed(Throwable t) {

        }

        @Override
        public void cancelled() {

        }
    });
}
项目:JavityEngine    文件:JavityNetJavaImpl.java   
public void cancelHttpRequest (HttpRequest httpRequest) {
    HttpResponseListener httpResponseListener = getFromListeners(httpRequest);

    if (httpResponseListener != null) {
        httpResponseListener.cancelled();
        removeFromConnectionsAndListeners(httpRequest);
    }
}
项目:JavityEngine    文件:WWW.java   
public void GET(String query) {
        if (work)
            return;

        work = true;

        HttpRequestBuilder requestBuilder = new HttpRequestBuilder();
        HttpRequest request = requestBuilder.newRequest().url(urlString).timeout(timeout).content(query).method(HttpMethods.GET).build();

        final WWW my = this;
        Gdx.net.sendHttpRequest(request, new HttpResponseListener() {

            public void handleHttpResponse(HttpResponse httpResponse) {
                responseBytes = httpResponse.getResult();
//              Gdx.app.log("WWW", "Response: " + responseBytes.length);
//              Gdx.app.log("WWW", "Status: " + httpResponse.getStatus().getStatusCode());
                work = false;
                if (wwwResponseListener != null) {
                    Gdx.app.postRunnable(new Runnable() {
                        public void run() {
                            wwwResponseListener.httpResponseListener(my);
                        }
                    });
                }
            }

            public void failed(Throwable t) {
                Gdx.app.error(WWW.class.getSimpleName(), "failed: " + t);
                work = false;
            }

            public void cancelled() {
                Gdx.app.error(WWW.class.getSimpleName(), "cancelled");
                work = false;
            }
        });
    }
项目:gdx-twitter    文件:TwitterAPI.java   
private void sendRequest(final TwitterRequest twitterRequest, final TwitterResponseListener listener) {

        HttpRequestBuilder requestBuilder = new HttpRequestBuilder();
        HttpRequest httpRequest = requestBuilder.newRequest().method(twitterRequest.getRequestType().name()).url(twitterRequest.getUrl()).build();

        // System.out.println(twitterRequest.build().getHeader());
        twitterRequest.build();

        httpRequest.setHeader("Authorization", twitterRequest.getHeader());
        httpRequest.setContent(twitterRequest.getData());
        httpRequest.setTimeOut(config.TWITTER_REQUEST_TIMEOUT);
        Gdx.net.sendHttpRequest(httpRequest, new HttpResponseListener() {

            @Override
            public void handleHttpResponse(HttpResponse httpResponse) {
                if (httpResponse.getStatus().getStatusCode() == 200) {
                    listener.success(httpResponse.getResultAsString());
                } else {
                    listener.apiError(httpResponse.getStatus(), httpResponse.getResultAsString());
                }
            }

            @Override
            public void failed(Throwable t) {
                listener.httpError(t);

            }

            @Override
            public void cancelled() {
                listener.cancelled();
            }
        });

    }
项目:libgdxcn    文件:NetJavaImpl.java   
public void cancelHttpRequest (HttpRequest httpRequest) {               
    try {
        lock.lock();
        HttpResponseListener httpResponseListener = listeners.get(httpRequest);

        if (httpResponseListener != null) {
            httpResponseListener.cancelled();
            connections.remove(httpRequest);
            listeners.remove(httpRequest);
        }
    } finally {
        lock.unlock();
    }
}
项目:libgdxcn    文件:DownloadTest.java   
@Override
public void create () {
    batch = new SpriteBatch();
    HttpRequest request = new HttpRequest(HttpMethods.GET);
    request.setUrl("https://www.google.at/images/srpr/logo11w.png");
    Gdx.net.sendHttpRequest(request, new HttpResponseListener() {
        @Override
        public void handleHttpResponse (HttpResponse httpResponse) {
            final byte[] bytes = httpResponse.getResult();

            Gdx.app.postRunnable(new Runnable() {
                @Override
                public void run () {
                    Pixmap pixmap = new Pixmap(bytes, 0, bytes.length);
                    texture = new Texture(new PixmapTextureData(pixmap, pixmap.getFormat(), false, false, true));   
                }
            });     
        }

        @Override
        public void failed (Throwable t) {
            t.printStackTrace();
            Gdx.app.log("EmptyDownloadTest", "Failed", t);
        }

        @Override
        public void cancelled () {
            Gdx.app.log("EmptyDownloadTest", "Cancelled");
        }
    });
}
项目:gaiasky    文件:GaiaCatalogWindow.java   
private String[][] getTAPData(String url, boolean hip, final String format, final GaiaDataListener listener) {
    HttpRequest request = new HttpRequest(HttpMethods.GET);
    request.setUrl(url);
    request.setTimeOut(5000);

    Gdx.net.sendHttpRequest(request, new HttpResponseListener() {
        @Override
        public void handleHttpResponse(HttpResponse httpResponse) {
            if (httpResponse.getStatus().getStatusCode() == HttpStatus.SC_OK) {
                // Ok
                listener.ok(isToArray(httpResponse.getResultAsStream(), format), hip);
            } else {
                // Ko with code
                listener.ko(httpResponse.getStatus().toString());
            }

        }

        @Override
        public void failed(Throwable t) {
            // Failed
            listener.ko();
        }

        @Override
        public void cancelled() {
            // Cancelled
            listener.ko();
        }
    });

    return null;
}
项目:ead    文件:MockPlatform.java   
@Override
public <T> T sendHttpRequest(HttpRequest httpRequest, Class<T> type)
        throws IOException {

    Object ret = httpResponses.get(httpRequest.getUrl());
    if (ret == null) {
        ret = httpResponses.get(DEFAULT_RESPONSE);
    }
    return (T) ret;
}
项目:ead    文件:Tracker.java   
/**
 * Loads the client identifier from preferences. If it does not exists,
 * obtains one from bugr.
 */
protected void loadClientId() {
    String clientId = controller.getPreferences().getString(
            Preferences.CLIENT_ID);
    if ("".equals(clientId) || clientId == null) {
        HttpRequest request = new HttpRequest("POST");
        request.setUrl(bugrURL + ACTIVATE_PATH);
        // Obtain an unique id
        Gdx.net.sendHttpRequest(request, new HttpResponseListener() {
            @Override
            public void handleHttpResponse(HttpResponse httpResponse) {
                if (httpResponse.getStatus().getStatusCode() == 200) {
                    cid = httpResponse.getResultAsString();
                    controller.getPreferences().putString(
                            Preferences.CLIENT_ID, cid);
                    // Start session with recently acquired cid
                    startSession();
                } else {
                    Gdx.app.error("Tracker",
                            "Impossible to activate this installation. Server said:\n"
                                    + httpResponse.getResultAsString());
                }
            }

            @Override
            public void failed(Throwable throwable) {
                Gdx.app.error("Tracker",
                        "Impossible to activate this installation.",
                        throwable);
            }

            @Override
            public void cancelled() {

            }
        });
    } else {
        this.cid = clientId;
    }
}
项目:ead    文件:AbstractPlatform.java   
@Override
public <T> T sendHttpGetRequest(String URL, Class<T> type)
        throws IOException {
    HttpRequest httpRequest = Pools.obtain(HttpRequest.class);
    httpRequest.setMethod(Net.HttpMethods.GET);
    httpRequest.setTimeOut(TIMEOUT);
    httpRequest.setUrl(URL);
    try {
        return sendHttpRequest(httpRequest, type);
    } finally {
        Pools.free(httpRequest);
    }
}
项目:ead    文件:MockPlatform.java   
@Override
public <T> T sendHttpRequest(HttpRequest httpRequest, Class<T> type)
        throws IOException {

    Object ret = httpResponses.get(httpRequest.getUrl());
    return (T) ret;
}
项目:JavityEngine    文件:JavityNetJavaImpl.java   
public JavityNetJavaImpl () {
    asyncExecutor = new AsyncExecutor(10);
    connections = new ObjectMap<HttpRequest, HttpURLConnection>();
    listeners = new ObjectMap<HttpRequest, HttpResponseListener>();
}
项目:JavityEngine    文件:JavityNetJavaImpl.java   
synchronized void removeFromConnectionsAndListeners (final HttpRequest httpRequest) {
    connections.remove(httpRequest);
    listeners.remove(httpRequest);
}
项目:JavityEngine    文件:JavityNetJavaImpl.java   
synchronized void putIntoConnectionsAndListeners (final HttpRequest httpRequest,
    final HttpResponseListener httpResponseListener, final HttpURLConnection connection) {
    connections.put(httpRequest, connection);
    listeners.put(httpRequest, httpResponseListener);
}
项目:JavityEngine    文件:JavityNetJavaImpl.java   
synchronized HttpResponseListener getFromListeners (HttpRequest httpRequest) {
    HttpResponseListener httpResponseListener = listeners.get(httpRequest);
    return httpResponseListener;
}
项目:JavityEngine    文件:WWW.java   
public void POST(Object form) {
        if (work)
            return;

        work = true;

        HttpRequestBuilder requestBuilder = new HttpRequestBuilder();
        HttpRequest request = requestBuilder.newRequest().url(urlString).timeout(timeout).jsonContent(form).method(HttpMethods.POST)
                .build();

        final WWW my = this;
        Gdx.net.sendHttpRequest(request, new HttpResponseListener() {

            public void handleHttpResponse(final HttpResponse httpResponse) {
                responseBytes = httpResponse.getResult();

//              Gdx.app.log(WWW.class.getSimpleName(), "response bytes: " + responseBytes.length + " response string: "
//                      + httpResponse.getResultAsString() + " Code: " + httpResponse.getStatus().getStatusCode());

                statusCode = httpResponse.getStatus().getStatusCode();
                work = false;
                if (wwwResponseListener != null) {
                    Gdx.app.postRunnable(new Runnable() {
                        public void run() {
                            wwwResponseListener.httpResponseListener(my);
                        }
                    });
                }
            }

            public void failed(Throwable t) {
                Gdx.app.error(WWW.class.getSimpleName(), "failed: " + t);
                work = false;
            }

            public void cancelled() {
                Gdx.app.error(WWW.class.getSimpleName(), "cancelled");
                work = false;
            }
        });
    }
项目:gdx-backend-jglfw    文件:JglfwNet.java   
public void sendHttpRequest (HttpRequest httpRequest, HttpResponseListener httpResponseListener) {
    netJavaImpl.sendHttpRequest(httpRequest, httpResponseListener);
}
项目:gdx-backend-jglfw    文件:JglfwNet.java   
@Override
public void cancelHttpRequest (HttpRequest httpRequest) {
    netJavaImpl.cancelHttpRequest(httpRequest);
}
项目:RottenCave    文件:ScoreRestClient.java   
public void getScoreById(HttpResponseListener responseListener, int id) {
    String url = URL_BASE + "get/" + id;
    HttpRequest request = requestBuilder.newRequest().method(HttpMethods.GET).url(url).build();
    Gdx.net.sendHttpRequest(request, responseListener);
}
项目:RottenCave    文件:ScoreRestClient.java   
public void getScoresList(HttpResponseListener responseListener) {
    String url = URL_BASE + "list";
    HttpRequest request = requestBuilder.newRequest().method(HttpMethods.GET).url(url).build();
    Gdx.net.sendHttpRequest(request, responseListener);
}
项目:RottenCave    文件:ScoreRestClient.java   
public void pushRemoteScore(HttpResponseListener responseListener, RemoteScore score) {
    String url = URL_BASE + "create";
    Json json = new Json(OutputType.json);
    HttpRequest request = requestBuilder.newRequest().method(HttpMethods.POST).url(url).build();
    request.setHeader("Content-Type", "application/json");
    request.setContent(json.toJson(score, RemoteScore.class));

    Gdx.app.debug("Push remote score ", json.toJson(score));
    Gdx.net.sendHttpRequest(request, responseListener);
}
项目:libgdxcn    文件:NetJavaImpl.java   
public NetJavaImpl () {
    executorService = Executors.newCachedThreadPool();
    connections = new ObjectMap<HttpRequest, HttpURLConnection>();
    listeners = new ObjectMap<HttpRequest, HttpResponseListener>();
    lock = new ReentrantLock();
}
项目:libgdxcn    文件:JglfwNet.java   
public void sendHttpRequest (HttpRequest httpRequest, HttpResponseListener httpResponseListener) {
    netJavaImpl.sendHttpRequest(httpRequest, httpResponseListener);
}
项目:libgdxcn    文件:JglfwNet.java   
@Override
public void cancelHttpRequest (HttpRequest httpRequest) {
    netJavaImpl.cancelHttpRequest(httpRequest);
}
项目:libgdxcn    文件:AndroidNet.java   
@Override
public void sendHttpRequest (HttpRequest httpRequest, final HttpResponseListener httpResponseListener) {
    netJavaImpl.sendHttpRequest(httpRequest, httpResponseListener);
}
项目:libgdxcn    文件:AndroidNet.java   
@Override
public void cancelHttpRequest (HttpRequest httpRequest) {
    netJavaImpl.cancelHttpRequest(httpRequest);
}
项目:BlockJump    文件:GameScreen.java   
public void getJsonList() {
    HttpRequest httpRequest = new HttpRequest(HttpMethods.GET);
    httpRequest.setUrl("http://24.222.27.154/highscores.json");

    Gdx.net.sendHttpRequest(httpRequest, new HttpResponseListener() {

        @Override
        public void handleHttpResponse(HttpResponse httpResponse) {
            result = httpResponse.getResultAsString();
            Gdx.app.postRunnable(new Runnable() {

                @Override
                public void run() {
                    grabHighScores();
                    createHighScores();
                }

            });
        }

        @Override
        public void failed(Throwable t) {
            Gdx.app.postRunnable(new Runnable() {

                @Override
                public void run() {
                    createHighScores();
                    if (highScoreStage != null) {
                        Dialog dialog = new Dialog("Error", new Skin(
                                Gdx.files.internal("data/uiskin.json")));
                        dialog.text("Connection Error");
                        dialog.button("OK");
                        dialog.show(highScoreStage);
                    }
                }

            });
            result = "";
        }

        @Override
        public void cancelled() {

        }

    });

    // long time = System.currentTimeMillis();
    // while(result == null) {
    // if(System.currentTimeMillis() - time > 5000) {
    // break;
    // }
    // }
    //
    // return result;
}
项目:ead    文件:Updater.java   
@Override
public Object call() throws Exception {
    // Try to download update.json. If updateURL is not present, disable
    // the update system
    if (releaseInfo.getUpdateURL() != null) {
        HttpRequest request = new HttpRequest("GET");
        request.setUrl(releaseInfo.getUpdateURL());
        Gdx.app.debug(LOG_TAG,
                "Trying to retrieve update.json from url:"
                        + releaseInfo.getUpdateURL());
        Gdx.net.sendHttpRequest(request, new HttpResponseListener() {
            @Override
            public void handleHttpResponse(HttpResponse httpResponse) {

                if (httpResponse.getStatus().getStatusCode() == 200) {
                    String data = httpResponse.getResultAsString();
                    Gdx.app.debug(LOG_TAG,
                            "Update.json fetched and read: " + data);
                    try {
                        UpdateInfo updateInfo = controller
                                .getApplicationAssets().fromJson(
                                        UpdateInfo.class, data);

                        if (updateInfo != null) {
                            checkUpdateNeeded(updateInfo);
                        }
                    } catch (SerializationException e) {
                        Gdx.app.error(
                                LOG_TAG,
                                "An error occurred while reading update.json from "
                                        + releaseInfo.getUpdateURL()
                                        + ". The update system will be disabled.",
                                e);
                        setDone();
                    }
                }
            }

            @Override
            public void failed(Throwable t) {
                Gdx.app.debug(
                        LOG_TAG,
                        "Error fetching update.json. Updater will be disabled",
                        t);
                setDone();
            }

            @Override
            public void cancelled() {

            }
        });
    } else {
        Gdx.app.debug(LOG_TAG,
                "The update.json url is null. The update system will be disabled.");
        setDone();
    }

    return null;
}
项目:ead    文件:Platform.java   
/**
 * Process the specified {@link HttpRequest} and returns the result.
 * 
 * @param httpRequest
 * @param type
 *            decides what result will be returned, currently supported
 *            values:
 *            <dl>
 *            <dd><strong>String.class</strong> Returns the result stream as
 *            a <em>String</em></dd>
 *            <dd><strong>byte[].class</strong> Returns the result stream as
 *            a <em>byte array</em></dd>
 *            <dd><strong>HttpURLConnection.class</strong> Returns the
 *            <em>connection</em>. Note that you must disconnect when you've
 *            finished via {@link HttpURLConnection#disconnect()}.</dd>
 *            </dl>
 * @return the result depending on {@code type} value.
 * @throws IOException
 *             if something went wrong.
 */
<T> T sendHttpRequest(HttpRequest httpRequest, Class<T> type)
        throws IOException;
项目:gdx-http    文件:HttpClient.java   
void sendHttpRequest(HttpRequest httpRequest, HttpResponseListener httpResponseListener);