I have been trying to get a successful JSON call from a web API with no success the past few days. I have tried multiple APIs with no success, so I don't think it is the API itself but how I am calling it with HttpURLConnection.
Here is a pruned version of my code:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView textView = (TextView) findViewById(R.id.textView);
    String urlString = "http://ip.jsontest.com/";
    String jsonString = null;
    HttpURLConnection connection = null;
    BufferedReader reader = null;
    try {
        URL url = new URL(urlString);
        connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        InputStream stream = connection.getInputStream();
        Scanner scan = new Scanner(stream).useDelimiter("\\A");
        jsonString = scan.next();
        scan.close();
        connection.disconnect();
    } catch (Exception e) { 
        e.printStackTrace();
    } 
    textView.setText(jsonString);
}}
I have followed and used multiple tutorials and guides trying to get this to work with no avail. I have also tried using a BufferedReader and a StringBuilder to pull the data to no avail.
EDIT:
I have had made it into a separate class as well in the past to no success:
public class NetworkConnect   {
/**
 * Execute the given URI, and return the data from that URI.
 *
 * @param uri the universal resource indicator for a set of data.
 * @return the set of data provided by the uri
 */
    private Exception exception;
    HttpURLConnection connection = null;
    BufferedReader reader = null;
    protected String doInBackground(String urlString) {
        try {
            URL url = new URL(urlString);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line+"\n");
                Log.d("Response: ", "> " + line);   //here u ll get whole response...... :-)
            }
            return buffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
 
    