Search code examples
androidhttpurlconnectionokhttp

Using OkHttp with existing HttpUrlConnection code?


I've just started working on an (existing) Android project that uses HttpUrlConnection to communicate with a RESTful web service. Today I realized that one of the REST calls I need to make uses the PATCH method which HttpUrlConnection apparently doesn't support. While I was searching for a workaround, I ran across the OkHttp client which apparently does support the PATCH method. On the website for OkHttp it says:

"You can try out OkHttp without rewriting your network code. The okhttp-urlconnection module implements the familiar java.net.HttpURLConnection API.."

I downloaded the jar file (as well as the Okio dependency) and added them to my project. Unfortunately, I can't seem to find any information on their website (I looked through the wiki and javadocs) about how I can "try out OkHttp without rewriting" all my existing HttpUrlConnection code. Am I just misunderstanding what they mean by that statement or am I missing something obvious?


Solution

  • Take a look at OkUrlFactory, which comes in the okhttp-urlconnection module.