Search code examples
androidtimeoutswaggerapache-commons-httpclientswagger-2.0

How to set connection and socket timeout for default swagger android client


I am using android api generated by default Swagger codegen (in Swagger Editor). The problem is that there is no any option, how to set connection timeout and socket timeout.

Is there any elegant way to set timeout without any change in generated code? Or do I have to implement my own timer, which will terminate server call?

Thanks for any advice!


Solution

  • I don't think the default Android API client has any helper method to set the connection timeout.

    I would suggest you to use the okhttp-gson HTTP library for Java instead (which is compatible with Android). Here is the command to generate Java client with the okhttp-gson library:

    java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
    -i modules/swagger-codegen/src/test/resources/2_0/petstore.json \
    -l java \
    -o /var/tmp/java-okhttp-gson/ \
    -D library=okhttp-gson
    

    and it allows setting the HTTP connection timeout via the setConnectTimeout method in ApiClient