Android HttpURLConection - Top HTTP GET Examples

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;


Here are HttpURLConnection HTTP GET Examples

Let's look at some examples.

This is an Android PHP MySQL HTTP GET Tutorial with HttpURLConnection and ListView.

And that's it.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:


What do You Think

Previous Post Next Post