How do I use networking frameworks (volley, okhttp, etc.) in libgdx?

I want to download some data from internet using volley or okhttp in libgdx.
How do I use networking structures like volley or okhttp in libgdx instead of the libgdx networking class ?

+3


source to share


1 answer


Try adding this one compile 'com.squareup.okhttp:okhttp:2.3.0'

to your project build.gradle

, for example

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


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

      



Then you can use okhttp in your main 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();
    }
}

      

+4


source







All Articles