尽管AsyncTask,NetworkOnMainThreadException

时间:2014-02-22 18:15:10

标签: java android android-asynctask

我是android新手并开发一个测试应用程序来命中服务器并获得json响应。 这是我的主要活动 -

public class MainPageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome_layout);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);
    MenuItem searchItem = menu.findItem(R.id.search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

    SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            new DownloadWebpageTask().doInBackground("http://49.139.144.194:8080/ServerForMyAndroid/MusicServlet");
            return false;
        }
    };
    searchView.setOnQueryTextListener(queryTextListener);
    return super.onCreateOptionsMenu(menu);
}

}

AsyncTask类是 -

public class DownloadWebpageTask extends AsyncTask<String, Void, String> {
@Override
protected void onPostExecute(String result) {
    ResultMainVO resultVO = new Gson().fromJson(result, ResultMainVO.class);
    System.out.println("test" + resultVO);
}

@Override
protected String doInBackground(String... urls) {
    InputStream is = null;
    StringBuilder builder = new StringBuilder();

    try {
        URL url = new URL(urls[0]);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestProperty("Accept", "*/*");
        conn.setDoInput(true);
        conn.connect();
        is = conn.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return builder.toString();
}

}

我还在AndroidManifest.xml文件中添加了权限 -     

当我在模拟器上运行应用程序时,我得到以下异常 -

02-22 21:49:41.989:E / AndroidRuntime(734):致命异常:主要 02-22 21:49:41.989:E / AndroidRuntime(734):android.os.NetworkOnMainThreadException 02-22 21:49:41.989:E / AndroidRuntime(734):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)

1 个答案:

答案 0 :(得分:7)

更改此

new DownloadWebpageTask().doInBackground("http://49.139.144.194:8080/ServerForMyAndroid/MusicServlet");

new DownloadWebpageTask().execute("http://49.139.144.194:8080/ServerForMyAndroid/MusicServlet");