Ganesh Hoolageri
Ganesh Hoolageri

Reputation: 155

NetworkOnMainThreadException inspite of AsyncTask

I am new to android and developing a test application to hit the server and get the json response. Here is my main activity-

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);
}

}

And AsyncTask class is-

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();
}

}

I have also added the permissions in AndroidManifest.xml file-

I get the below exception when I run the app on emulator-

02-22 21:49:41.989: E/AndroidRuntime(734): FATAL EXCEPTION: main 02-22 21:49:41.989: E/AndroidRuntime(734): android.os.NetworkOnMainThreadException 02-22 21:49:41.989: E/AndroidRuntime(734): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)

Upvotes: 1

Views: 131

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

Change this

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

to

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

Upvotes: 7

Related Questions