Last active
February 14, 2022 05:18
-
-
Save duivesteyn/958aacbe7510c9313f553988206cc59e to your computer and use it in GitHub Desktop.
Super Simple Android HTTPS GET Request Example using Volley
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// duivesteyn // Super Simple Android HTTPS GET Request Example using Volley. | |
// There was so much confusing android content online, I wanted to post a simple Example of a HTTPS GET Request with here. | |
// Uses Google Volley. | |
// | |
// (and yes I'm new to java. This is the first java code I ever wrote) | |
// | |
// 2022-02-14 | |
// | |
... | |
import android.widget.Toast; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import com.android.volley.Request; | |
import com.android.volley.RequestQueue; | |
import com.android.volley.Response; | |
import com.android.volley.VolleyError; | |
import com.android.volley.toolbox.StringRequest; | |
import com.android.volley.toolbox.JsonObjectRequest; | |
import com.android.volley.toolbox.Volley; | |
public class className { | |
String totalCasesWorld; | |
String totalDeathsWorld; | |
String totalRecoveredWorld; | |
private void httpsGetExample() { | |
//Very Simple Android Volley Example - Send HTTPS GET Request | |
//Based on https://www.section.io/engineering-education/making-api-requests-using-volley-android/ | |
Log.i("api","BMD Simple HTTPS GET Example"); | |
RequestQueue requestQueue = Volley.newRequestQueue(this); | |
String myUrl = "https://corona.lmao.ninja/v2/all"; | |
StringRequest myRequest = new StringRequest(Request.Method.GET, myUrl, | |
response -> { | |
try{ | |
//Create a JSON object containing information from the API. | |
JSONObject myJsonObject = new JSONObject(response); | |
totalCasesWorld = (myJsonObject.getString("cases")); | |
totalRecoveredWorld = (myJsonObject.getString("recovered")); | |
totalDeathsWorld = (myJsonObject.getString("deaths")); | |
Log.i("api","totalCasesWorld " + totalCasesWorld); | |
Log.i("api","totalRecoveredWorld " + totalRecoveredWorld); | |
Log.i("api","totalDeathsWorld " + totalDeathsWorld); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
}, | |
volleyError -> Toast.makeText(this, volleyError.getMessage(), Toast.LENGTH_SHORT).show() | |
); | |
requestQueue.add(myRequest); | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment