我想在libgdx中使用volley或okhttp从web加载一些数据。
如何在libgdx中使用网络框架,如volley或okhttp,而不是libgdx networking class?
最佳答案
尝试将此compile 'com.squareup.okhttp:okhttp:2.3.0'
添加到项目build.gradle
文件中,例如
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();
}
}