-2

I'm doing web portal application.

By using AsyncTask<> im getting the json values and displaying.

My app runs fine in Android 2.3.3 but when i run the app in android 4.1.2 it throws error. I have gone with google and done changes according to it but still i get error in 4.1.2

I have included the line in manifest also..

<uses-permission android:name="android.permission.INTERNET" />

I have removed the runOnUiThread part also from onPostExecute(String file_url) after that also it throws error.

Any help would be appreciated.

My code

class LoadAllgroups extends AsyncTask<String, String, String> {

         @Override
            protected void onPreExecute() {
                super.onPreExecute();

                showDialog(progress_bar_type);
            }

        protected String doInBackground(String... args) {
                 // here my codes

    }
                protected void onPostExecute(String file_url) {
            pDialog.dismiss();
            gnamelist=new ArrayList<String>(new LinkedHashSet<String>(gnamelist));
            runOnUiThread(new Runnable() {
                public void run() {
                    createExpandableListViewDialog();
                    }
            });
            return;
        }
        }

LogCat

07-29 00:29:47.860: E/AndroidRuntime(947): FATAL EXCEPTION: main
07-29 00:29:47.860: E/AndroidRuntime(947): android.os.NetworkOnMainThreadException
07-29 00:29:47.860: E/AndroidRuntime(947):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-29 00:29:47.860: E/AndroidRuntime(947):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
07-29 00:29:47.860: E/AndroidRuntime(947):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
07-29 00:29:47.860: E/AndroidRuntime(947):  at libcore.io.IoBridge.connect(IoBridge.java:112)
07-29 00:29:47.860: E/AndroidRuntime(947):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-29 00:29:47.860: E/AndroidRuntime(947):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
07-29 00:29:47.860: E/AndroidRuntime(947):  at java.net.Socket.connect(Socket.java:842)
07-29 00:29:47.860: E/AndroidRuntime(947):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
07-29 00:29:47.860: E/AndroidRuntime(947):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
07-29 00:29:47.860: E/AndroidRuntime(947):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-29 00:29:47.860: E/AndroidRuntime(947):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-29 00:29:47.860: E/AndroidRuntime(947):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-29 00:29:47.860: E/AndroidRuntime(947):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-29 00:29:47.860: E/AndroidRuntime(947):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-29 00:29:47.860: E/AndroidRuntime(947):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-29 00:29:47.860: E/AndroidRuntime(947):  at com.example.TEENEINSTIEN.JSONParser.makeHttpRequest(JSONParser.java:62)
07-29 00:29:47.860: E/AndroidRuntime(947):  at com.example.TEENEINSTIEN.FrontActivity$3.onGroupClick(FrontActivity.java:219)
07-29 00:29:47.860: E/AndroidRuntime(947):  at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:544)
07-29 00:29:47.860: E/AndroidRuntime(947):  at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:522)
07-29 00:29:47.860: E/AndroidRuntime(947):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2859)
07-29 00:29:47.860: E/AndroidRuntime(947):  at android.widget.AbsListView$1.run(AbsListView.java:3533)
07-29 00:29:47.860: E/AndroidRuntime(947):  at android.os.Handler.handleCallback(Handler.java:615)
07-29 00:29:47.860: E/AndroidRuntime(947):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-29 00:29:47.860: E/AndroidRuntime(947):  at android.os.Looper.loop(Looper.java:137)
07-29 00:29:47.860: E/AndroidRuntime(947):  at android.app.ActivityThread.main(ActivityThread.java:4745)
07-29 00:29:47.860: E/AndroidRuntime(947):  at java.lang.reflect.Method.invokeNative(Native Method)
07-29 00:29:47.860: E/AndroidRuntime(947):  at java.lang.reflect.Method.invoke(Method.java:511)
07-29 00:29:47.860: E/AndroidRuntime(947):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-29 00:29:47.860: E/AndroidRuntime(947):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-29 00:29:47.860: E/AndroidRuntime(947):  at dalvik.system.NativeStart.main(Native Method)

In Frontactivity.java line 219 is JSONObject json = jsonParser.makeHttpRequest(url_get_groupid, "GET", params); in createExpandableListViewDialog() method.

Make it Simple
  • 1,832
  • 5
  • 32
  • 57
  • 2
    What is on `FrontActivity.java:219`? – Shobhit Puri Jul 29 '13 at 07:47
  • You are not supposed to do any network operation in UI thread. Hence the exception.. – Tarun Jul 29 '13 at 07:56
  • @all Where to put these lines in my code `StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);` – Make it Simple Jul 29 '13 at 08:00
  • 1
    @OneManArmy you need to do a network related operation in `doInbackground` of asynctask. http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html – Raghunandan Jul 29 '13 at 08:04
  • If you do some network related operations in mainthread you will be getting NetworkMainThreadException.You need to perform those operations in background process ie,AsyncTask. You can overcome this by adding after super.onCreate(savedInstanceState); StrictMode.ThreadPolicy stPolicy= new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(stPolicy); – Kalai.G Aug 02 '13 at 06:47

1 Answers1

7

you can put the below code in oncreate. but it wont solve the problem it will only hide it.

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

what you actually need to do is put network related code in your createExpandableListViewDialog();(the http request part) inside a background thread.

shreyas
  • 2,166
  • 3
  • 18
  • 30