I'm trying to load JSON from a URL in an Android app (the URL only links to JSON data, there is nothing else).
For now I'm only trying to load the content of the URL into a String.

The problem I have is that I need to handle the Exceptions, but I'm not too familiar with that.

Here is the relevant part of my 'Functions' class:

```
static public String loadURL(String inputURL) throws Exception {

    String fullString = "";
    URL url = new URL(inputURL);
    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
    String line;
    while ((line = reader.readLine()) != null) {
        fullString += line;
    }
    reader.close();

    return fullString;

}

```

This is what I'm currently using (in a different class) for testing purposes:

```
Toast.makeText(Setup.this, Functions.loadURL("http://www.exampleJSON.com"), Toast.LENGTH_LONG).show();

```

I'm planning on using the output to extract JSON data for if-statements, saving in sharedPreferences and displaying.

**EDIT**

Here what I'm using to call the method:

```
public class Setup extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setup);

    final SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE);
    final SharedPreferences.Editor editor = preferences.edit();

    final Spinner spinner = (Spinner) findViewById(R.id.spinner);

    final EditText editText = (EditText) findViewById(R.id.editText);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.setup_region_spinner, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(this);

    //BUTTON PRESS

    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            editor.putString("Server", String.valueOf(spinner.getSelectedItem()));

            if (String.valueOf(spinner.getSelectedItem()).equals("EUW")) {

            }

            else if (String.valueOf(spinner.getSelectedItem()).equals("NA")) {

                try {
                    Toast.makeText(Setup.this, Functions.loadURL("https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/" +
                            editText.getText().toString().replaceAll("\\s", "") +
                            "?api_key=cbc50791-3c4d-45e6-b0c1-8aa204ced475"), Toast.LENGTH_LONG).show();
                }catch(Exception e){
                    e.printStackTrace();
                }

            }

            //ERROR

            else {

                Toast.makeText(Setup.this, "Server selection error", Toast.LENGTH_SHORT).show();

            }

        }
    });
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
}

```

The try-block was just added after reading through the answers. Another one was added in my loadURL method. It seems to work for now. 

I'm now getting errors because I was trying to run this on the main thread. I'll have to read more about that.