Search code examples
androidlibgdxandroid-volleyokhttp

How to use network frameworks (volley , okhttp , etc) in libgdx?


I want to load some data from web using volley or okhttp in libgdx.
How to use network frameworks like volley or okhttp in libgdx instead of libgdx networking class ?


Solution

  • Try to add this compile 'com.squareup.okhttp:okhttp:2.3.0' to your project build.gradle file for example

    project(":core") {
        apply plugin: "java"
    
    
        dependencies {
                ...
                compile 'com.squareup.okhttp:okhttp:2.3.0'
            }
        }
    

    Then you can use okhttp in your core project here is an example :

    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();
        }
    }