Android HttpURLConection HTTP GET Examples

User Management System
Learn Kotlin, Retrofit, MVVM and MySQL using this all-in-one app. It is designed to be beginner friendly.

Android HttpURLConnection HTTP GET Tutorial.

In this piece we explore some HTTP GET examples. Basicaly how to perform HTTP GET using the HttpURLConnection class.


Let's start.

Quick HTTP GET Examples

Let's start by looking at some quick HTTP GET examples.

1. How to perform HTTP GET and return a JSONObject

The first step is to perform a HTTP GET when provided the target URL and a Context object.

    public static JSONObject getJsonObject(String url, Context context) throws IOException, JSONException{
        String line = null;
        InputStream is = getInputStream(url, null, null, context);
        BufferedReader br = new BufferedReader(new;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null){
            sb.append(line + 'n');


        return new JSONObject(sb.toString());


2. How to perform a HTTP GET and return a BufferedInputStream

Let's say we have a getUserAgentString() method as follows:

    public static String  getUserAgentString(Context context){
        SharedPreferences sPref = PreferenceManager.getDefaultSharedPreferences(context);
        String myid = sPref.getString("myId", "NoInfo");
        String myscr = sPref.getInt("scW", 0)+"x"+sPref.getInt("scH", 0);
        String verString = null;
        try {
            verString = context.getPackageManager().getPackageInfo(context.getPackageName(),0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
        String partnerid = "";

        return "aptoideAppsBackup-" + verString+";"+ Constants.TERMINAL_INFO+";"+myscr+";id:"+myid+";"+sPref.getString(Constants.LOGIN_USER_LOGIN, "")+";"+partnerid;

Then here's we get the and return the BufferedInputStream.

    public static BufferedInputStream getInputStream(String url, String username, String password, Context context) throws IOException{

        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();

        if(username != null){
            String userPassword = username + ":" + password;
            String encoding = Base64.encodeToString(userPassword.getBytes(), Base64.NO_WRAP);
            connection.setRequestProperty("Authorization", "Basic " + encoding);
        connection.setRequestProperty("User-Agent", getUserAgentString(context));
        System.out.println("Using user-agent: " + (getUserAgentString(context)));
        Log.d("TAG", username + " " + password);
        BufferedInputStream bis = new BufferedInputStream(connection.getInputStream(), 8 * 1024);

        return bis;



Android MySQL Retrofit2 Multipart CRUD,Search,Pagination rating

When I was a 2nd year Software Engineering student, I buillt a now defunct online tool called Camposha(from Campus Share) using my then favorite language C#(ASP.NET) to compete OLX in my country(Kenya). The idea was to target campus students in Kenya. I got a few hundred signups but competing OLX proved too daunting. I decided to focus on my studies, learning other languages like Java,Python,Kotlin etc while meanwhile publishing tutorials at my YouTube Channel ProgrammingWizards TV which led to this site( Say hello or post me a suggestion: . Follow me below; Github , and on my channel: ProgrammingWizards TV

We will be happy to hear your thoughts

Leave a reply

+ thirty = 36

Reset Password
Compare items
  • Total (0)
Shopping cart