如何在libgdx中使用网络框架(如Volley,OkHttp等)?

3

我想使用Volley或OkHttp在libgdx中从网站加载一些数据。
如何在libgdx中使用网络框架,比如Volley或OkHttp,而不是使用libgdx的网络类

1个回答

3

例如,尝试在您的项目build.gradle文件中添加compile 'com.squareup.okhttp:okhttp:2.3.0'

project(":core") {
    apply plugin: "java"


    dependencies {
            ...
            compile 'com.squareup.okhttp:okhttp:2.3.0'
        }
    }

然后您可以在核心项目中使用OkHttp,这里是一个示例:

public class OkhttpTest extends ApplicationAdapter {
    OkHttpClient client = new OkHttpClient();

    @Override
    public void create() {
        try {
            System.out.println(run("http://google.com"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    String run(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();

        Response response = client.newCall(request).execute();
        return response.body().string();
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接