所以我有一个课程如下:
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)
{
}
}
在这里被称为:
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);
}
当执行此操作时,我将返回&#34;没有&#34;这解释了它落在doInBackground方法的catch语句中,我得到了一个例外:
android.os.NetworkOnMainThreadException
有谁知道我做错了什么?