I have been provided several curl commands that to use in android.
The curl commands are structured like this:
curl "https://www.website.com/api/a/page"
-H "Authorization: Bearer API_KEY"
-H "Content-Type: application/json"
-X GET
I can not figure out how to make use of this in java in Android.
If it could be turned into something like:
stringURL = "https://www.website.com/api/a/page&appid=API_KEY";
URL url = new URL(stringURL);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
any help would be appreciated
CodePudding user response:
For the sake of code reusability, and for conforming to the preferred Android patterns (e.g., MVVM), it is generally accepted practice to use an HTTP library such as Retrofit. If your case is simple enough, you should be able to map your cURL parameters to the Retrofit classes and methods easily enough.
Other options include Apache HTTPComponents Client, OkHttpClient, AsyncHttpClient, JettyHttpClient, and so on.
If you wish, you can always use the core Java HttpUrlConnection
as you've included in the example. However, it is good practice to wrap these functions/methods into appropriate classes, interfaces, callbacks, or repositories, as required. (The available libraries manage this overhead for you.)
If I've not addressed some specific concerns, let me know so that I can build on this answer further.
P.S.: Not including any code here, since the link to any of the docs should be a good starting point.
CodePudding user response:
- Add the curl commands to postman via Import > Paste raw
- Export postman collection
- Install this plugin PostmanToRetrofit2
- Right click > Generate > Retrofit2Generator > Paste your postman collection > OK