Note that there are some explanatory texts on larger screens.

plurals
  1. PONetworkOnMainThreadException even after using asynctask
    primarykey
    data
    text
    <p>I have found few questions in stackOverflow related to this topic but was unable to find a suitable answer.In my app,I intend to do network communications via separate thread.So I used asynctask.But even after using asynctask I found the networkOnMainThreadException.Here is my code :</p> <pre><code>private static final String CATEGORY_ALL_FETCHING_URL = "http://qpon.es/coupon/index.php/welcome/categories/all"; private static final String CATEGORY_IMAGE_URL = "http://qpon.es/images/coupons/"; private static final String TAG_CATEGORY_ID = "id"; private static final String TAG_CATEGORY_NAME = "name"; private static final String TAG_CATEGORY_IMAGE = "img"; private static final String TAG_CATEGORY_CREATE_DATE = "created"; private static final String CATEGORY_INTENT = "category"; private ProgressDialog downloadingStatusIndicator = null; private ArrayList&lt;Category&gt; categoryList = null; private CustomCategoryAdapter customAdapter = null; private class CateoryListDataGenerator extends AsyncTask&lt;String, Void, String&gt; { @Override protected void onPreExecute() { super.onPreExecute(); downloadingStatusIndicator = new ProgressDialog(getActivity()); downloadingStatusIndicator.setTitle(R.string.categoryDownloadTitle); downloadingStatusIndicator.setMessage("Retrieving Categories..."); downloadingStatusIndicator.show(); } @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub String response = ""; try { Log.d("BACK THREAD STARTS", "YES"); categoryList = new ArrayList&lt;Category&gt;(); response = JSONParser .getRemoteResponse(CATEGORY_ALL_FETCHING_URL); // Thread.sleep(2000); } catch (Exception e) { Log.d("BACKGROUD", e.getMessage()); } return response; } @Override protected void onPostExecute(String response) { JSONArray categoriesJSON = null; try { categoriesJSON = new JSONArray(response); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } for (int i = 0; i &lt; categoriesJSON.length(); i++) { Log.d("LOOP BEGINS", "YES"); Category eachCategory = new Category(); JSONObject eachCategoryJSON = null; try { eachCategoryJSON = categoriesJSON.getJSONObject(i); eachCategory.setCategoryName(eachCategoryJSON .getString(TAG_CATEGORY_NAME)); eachCategory.setCategoryCreateDate(eachCategoryJSON .getString(TAG_CATEGORY_CREATE_DATE)); eachCategory.setCategoryImagePath(CATEGORY_IMAGE_URL + eachCategoryJSON.getString(TAG_CATEGORY_IMAGE)); eachCategory.setCategoryID(eachCategoryJSON .getInt(TAG_CATEGORY_ID)); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } categoryList.add(eachCategory); Log.d("CATEGORY INSERTED", categoryList.toString()); } Log.d("CATEGORY LIST", categoryList.toString()); if (categoryList != null || categoryList.size() &gt; 0) { customAdapter.notifyDataSetChanged(); for (int i = 0; i &lt; categoryList.size(); i++) { customAdapter.add(categoryList.get(i)); } downloadingStatusIndicator.dismiss(); customAdapter.notifyDataSetChanged(); } Toast.makeText(getActivity(), response, Toast.LENGTH_SHORT).show(); } } </code></pre> <p>In JSONParder's setRemoteRenponse() method :</p> <pre><code>boolean isURLFetched = false; while(!isURLFetched){ DefaultHttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); try { HttpResponse execute = client.execute(httpGet); content = execute.getEntity().getContent(); isURLFetched = true; } catch (UnsupportedEncodingException e) { Log.d("HERE", "ONE"); e.printStackTrace(); } catch (ClientProtocolException e) { Log.d("HERE", "TWO"); e.printStackTrace(); } catch (IOException e) { Log.d("HERE", "THREE"); e.printStackTrace(); } } try { BufferedReader buffer = new BufferedReader(new InputStreamReader( content)); String s = ""; while ((s = buffer.readLine()) != null) { response += s; } } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } return response; </code></pre> <p>Lastly the asynctask class object is created &amp; executed in the fragment's onActivityCreated() method :</p> <pre><code>CateoryListDataGenerator dataGenerator = new CateoryListDataGenerator(); dataGenerator.execute(new String[] { CATEGORY_ALL_FETCHING_URL }); </code></pre> <p>Am I doing something wrong here? I have included INTERNET permission too.For now,I have changed the thread policy to work this app on android 3.0 or higher but this is no solution.</p> <p>Here is the logcat message:</p> <pre><code>05-24 17:11:36.025: E/AndroidRuntime(708): FATAL EXCEPTION: main 05-24 17:11:36.025: E/AndroidRuntime(708): android.os.NetworkOnMainThreadException 05-24 17:11:36.025: E/AndroidRuntime(708): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 05-24 17:11:36.025: E/AndroidRuntime(708): at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 05-24 17:11:36.025: E/AndroidRuntime(708): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 05-24 17:11:36.025: E/AndroidRuntime(708): at java.net.InetAddress.getAllByName(InetAddress.java:214) 05-24 17:11:36.025: E/AndroidRuntime(708): at libcore.net.http.HttpConnection.&lt;init&gt;(HttpConnection.java:70) 05-24 17:11:36.025: E/AndroidRuntime(708): at libcore.net.http.HttpConnection.&lt;init&gt;(HttpConnection.java:50) 05-24 17:11:36.025: E/AndroidRuntime(708): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341) 05-24 17:11:36.025: E/AndroidRuntime(708): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 05-24 17:11:36.025: E/AndroidRuntime(708): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 05-24 17:11:36.025: E/AndroidRuntime(708): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315) 05-24 17:11:36.025: E/AndroidRuntime(708): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310) 05-24 17:11:36.025: E/AndroidRuntime(708): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289) 05-24 17:11:36.025: E/AndroidRuntime(708): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239) 05-24 17:11:36.025: E/AndroidRuntime(708): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273) 05-24 17:11:36.025: E/AndroidRuntime(708): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168) 05-24 17:11:36.025: E/AndroidRuntime(708): at com.example.couponapp.adapters.CustomCategoryAdapter.getScaledBitmapFromFile(CustomCategoryAdapter.java:83) 05-24 17:11:36.025: E/AndroidRuntime(708): at com.example.couponapp.adapters.CustomCategoryAdapter.getView(CustomCategoryAdapter.java:58) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.widget.AbsListView.obtainView(AbsListView.java:2267) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.widget.ListView.makeAndAddView(ListView.java:1769) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.widget.ListView.fillDown(ListView.java:672) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.widget.ListView.fillFromTop(ListView.java:733) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.widget.ListView.layoutChildren(ListView.java:1608) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.widget.AbsListView.onLayout(AbsListView.java:2102) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.View.layout(View.java:13754) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.ViewGroup.layout(ViewGroup.java:4362) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.View.layout(View.java:13754) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.ViewGroup.layout(ViewGroup.java:4362) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.View.layout(View.java:13754) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.ViewGroup.layout(ViewGroup.java:4362) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.View.layout(View.java:13754) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.ViewGroup.layout(ViewGroup.java:4362) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.widget.LinearLayout.onLayout(LinearLayout.java:1420) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.View.layout(View.java:13754) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.ViewGroup.layout(ViewGroup.java:4362) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.View.layout(View.java:13754) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.ViewGroup.layout(ViewGroup.java:4362) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.Choreographer.doCallbacks(Choreographer.java:555) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.Choreographer.doFrame(Choreographer.java:525) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.os.Handler.handleCallback(Handler.java:615) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.os.Handler.dispatchMessage(Handler.java:92) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.os.Looper.loop(Looper.java:137) 05-24 17:11:36.025: E/AndroidRuntime(708): at android.app.ActivityThread.main(ActivityThread.java:4745) 05-24 17:11:36.025: E/AndroidRuntime(708): at java.lang.reflect.Method.invokeNative(Native Method) 05-24 17:11:36.025: E/AndroidRuntime(708): at java.lang.reflect.Method.invoke(Method.java:511) 05-24 17:11:36.025: E/AndroidRuntime(708): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 05-24 17:11:36.025: E/AndroidRuntime(708): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 05-24 17:11:36.025: E/AndroidRuntime(708): at dalvik.system.NativeStart.main(Native Method) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload