-3

I'm getting exception while hitting a url from android app. I have added Internet permission to the app's manifest.

URL url = new URL("www.example.com?query=abcd");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.connect();
        urlConnection.disconnect();

This is stacktrace of exception

syfro
  • 121
  • 1
  • 5
  • Please, at least do some research about this exception ... similar questions are asked 10 times a day, because so many people just don't know how to do internet search(or just can't learn on example) – Selvin Apr 04 '16 at 09:25

2 Answers2

0

Use AsyncTask and call webservice url in doInBackground().if you want detaild information about AsyncTask please go through the below url

http://developer.android.com/reference/android/os/AsyncTask.html

malli
  • 642
  • 2
  • 12
  • 30
0

According to Android developer guide lines NetworkOnMainThreadException is thrown when you perform any network based operation on main thread. So call your webservice in worker thread(eg async task).

For debugging purpose if you want to call webservice on main thread then

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);

But do not use it unless you want just for debugging

Chirag Jain
  • 133
  • 9