So I have a class with the following:
class NetworkRequest extends AsyncTask<String, StringBuilder, String> {
@Override
public String doInBackground(String... urls) {
try
{
URL url = new URL(urls[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream inputStream = url.openStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
StringBuilder stringBuilder = new StringBuilder();
int cp;
while ((cp = bufferedReader.read()) != -1)
{
stringBuilder.append((char) cp);
}
return stringBuilder.toString();
}
catch(Exception ex)
{
return null;
}
}
public void onPostExecute(String string)
{
}
}
And it is being called here:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mediated);
TextView welcome_txt = (TextView) findViewById(R.id.test_view);
new NetworkRequest().execute("https://graph.facebook.com/me");
NetworkRequest networkRequest = new NetworkRequest();
String string = networkRequest.doInBackground();
test_view.setText(string);
}
When this is executed I am returning with "nothing" which explains it is falling in the catch statement of the doInBackground method, I get an exception of:
android.os.NetworkOnMainThreadException
Does anyone understand what I am doing wrong?