Hello. I had an app that run well in 2.3. I read that in gingerbread and 
higher I can't run a network operation in main thread, and that I should 
use AsyncTask. I made that, but it still gives me that error. In the Galaxy 
SIII, the app run with error and does not load the data. I tried with the 
emulator using the same OS version, and load the data call from the 
database, but the logcat  still print the error 
"android.os.NetworkOnMainThreadException". 

This is the error:

09-20 07:57:25.452: E/ServicioRest(1506): 
android.os.NetworkOnMainThreadException
09-20 07:57:25.452: E/ServicioRest(1506): at 
android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
09-20 07:57:25.452: E/ServicioRest(1506): at 
java.net.InetAddress.lookupHostByName(InetAddress.java:391)
09-20 07:57:25.452: E/ServicioRest(1506): at 
java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
09-20 07:57:25.452: E/ServicioRest(1506): at 
java.net.InetAddress.getAllByName(InetAddress.java:220)
09-20 07:57:25.452: E/ServicioRest(1506): at 
org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
09-20 07:57:25.452: E/ServicioRest(1506): at 
org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-20 07:57:25.452: E/ServicioRest(1506): at 
org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-20 07:57:25.452: E/ServicioRest(1506): at 
org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-20 07:57:25.452: E/ServicioRest(1506): at 
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-20 07:57:25.452: E/ServicioRest(1506): at 
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-20 07:57:25.452: E/ServicioRest(1506): at 
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-20 07:57:25.452: E/ServicioRest(1506): at 
com.silm.notification.request.OrderRequestParser.listOrderRequest(OrderRequestParser.java:42)
09-20 07:57:25.452: E/ServicioRest(1506): at 
com.silm.notification.request.DataLoader.getOrderRequestArray(DataLoader.java:18)
09-20 07:57:25.452: E/ServicioRest(1506): at 
com.silm.notification.fragments.BottomPanel.onCreateView(BottomPanel.java:57)
09-20 07:57:25.452: E/ServicioRest(1506): at 
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:837)
09-20 07:57:25.452: E/ServicioRest(1506): at 
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1041)
09-20 07:57:25.452: E/ServicioRest(1506): at 
android.support.v4.app.BackStackRecord.run(BackStackRecord.java:616)
09-20 07:57:25.452: E/ServicioRest(1506): at 
android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1359)
09-20 07:57:25.452: E/ServicioRest(1506): at 
android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:411)
09-20 07:57:25.452: E/ServicioRest(1506): at 
android.os.Handler.handleCallback(Handler.java:605)
09-20 07:57:25.452: E/ServicioRest(1506): at 
android.os.Handler.dispatchMessage(Handler.java:92)
09-20 07:57:25.452: E/ServicioRest(1506): at 
android.os.Looper.loop(Looper.java:137)
09-20 07:57:25.452: E/ServicioRest(1506): at 
android.app.ActivityThread.main(ActivityThread.java:4424)
09-20 07:57:25.452: E/ServicioRest(1506): at 
java.lang.reflect.Method.invokeNative(Native Method)
09-20 07:57:25.452: E/ServicioRest(1506): at 
java.lang.reflect.Method.invoke(Method.java:511)
09-20 07:57:25.452: E/ServicioRest(1506): at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-20 07:57:25.452: E/ServicioRest(1506): at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-20 07:57:25.452: E/ServicioRest(1506): at 
dalvik.system.NativeStart.main(Native Method)


I also read that changing the StrictMode policy, it could be done. Using 
this code:

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

But is better to use AsyncTask. But I am using AsyncTask! Hopefully I am 
using it incorrect so you could tell me how to use it well, and get the 
problem solve.
I tried also with the StrictMode but it seems like the android version I 
use (2.2) does not include that library. And if AsyncTask solve the 
problem, I should better use AsyncTask. I had problem passing the data I 
load from database in the asyncTaks class to the activity class of my main 
thread, so I made a private asynctask class in my main UI thread, and later 
get a way to pass the data. now this is how that class is:

public class InboxHandler extends FragmentActivity implements
OnLineSelectedListener {
static final int GET_CODE = 0;
private TextView textView3, textView2;
private Button button1, button2, button3;
private LinearLayout tab1, tab2, tab3;
private CheckBox cbVerPorCategoria;
private ActionBar actionBar;
private TextView txtTitle;
private Settings settings;
FragmentTransaction fragmentTransaction;
BottomPanel bottomPanel;
public List<OrderRequest> orderRequestArray;
private OrderRequestParser orderRequestParser;
public static int number;
int clickCounter = 0;
private boolean doubleBackToExitPressedOnce = false;
private static Context context;
DataLoader dataLoader;
private MyAsynckTask myAsynckTask;
public InboxHandler() {
}
public List<OrderRequest> getOrderRequestArray() {
return orderRequestArray;
}
public void setOrderRequestArray(List<OrderRequest> orderRequestArray) {
this.orderRequestArray = orderRequestArray;
}
public static Context getContext() {
return context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inbox);
context = this.getApplicationContext();
settings = Settings.getInstance();
settings.setPrefs(getSharedPreferences(settings.getPREFERENCES(),
Activity.MODE_PRIVATE));
settings.setPrefsEditor(settings.getPrefs().edit());
settings.getPrefsEditor().putBoolean("isLoggedIn", true);
settings.isVistaSimple = true;
actionBar = (ActionBar) findViewById(R.id.actionbar);
initComponents();
prepareActionBar();
}
@SuppressWarnings("unchecked")
private void initComponents() {
try {
MyAsynckTask myAsynckTask = new MyAsynckTask(this);
myAsynckTask.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
private class MyAsynckTask extends AsyncTask {
private String stringMensaje;
public List<OrderRequest> orderRequestArray;
private OrderRequestParser orderRequestParser;
private Request request;
private Settings settings;
private InboxHandler inboxHandler;
public MyAsynckTask(InboxHandler inboxHandler) {
this.inboxHandler = inboxHandler;
}
@Override
protected Object doInBackground(Object... params) {
List<OrderRequest> array = this.getOrderRequests();
settings = Settings.getInstance();
if (array != null) {
settings.setOrderRequestArray(array);
orderRequestArray = array;
Log.e("orderRequestArray",
"Me trajo el orderRequestArray en doInBackground!");
} else {
Log.e("orderRequestArray",
"No me trajo el orderRequestArray en doInBackground!");
}
return array;
}
@Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);
List<OrderRequest> array = (List<OrderRequest>) result;
this.setOrderRequestArray(array);
try {
Log.e("getOrderRequestArray", ""
+ this.getOrderRequestArray().get(0).getDocNumber());
} catch (Exception e) {
e.printStackTrace();
}
if (orderRequestArray != null) {
inboxHandler.setOrderRequestArray(orderRequestArray);
Log.e("orderRequestArray", "is not null en onPostExecute!");
} else {
Log.e("orderRequestArray", "is null en onPostExecute!");
}
if (inboxHandler.getOrderRequestArray() != null) {
Log.e("inboxHandler.getOrderRequestArray()",
"is not null en onPostExecute!");
} else {
Log.e("orderRequestArray", "is null en onPostExecute!");
}
bottomPanel = new BottomPanel();
Bundle bundle = new Bundle();
bundle.putSerializable("arrayPedidos",
(Serializable) orderRequestArray);
bottomPanel.setArguments(bundle);
NotificationFragment notificationFragment = new NotificationFragment(
orderRequestArray, actionBar);
Bundle bundleNotificationFragment = new Bundle();
bundleNotificationFragment.putString("title", "Inbox");
notificationFragment.setArguments(bundleNotificationFragment);
fragmentTransaction = getSupportFragmentManager()
.beginTransaction();
fragmentTransaction.add(R.id.frame_list_container,
notificationFragment);
fragmentTransaction.add(R.id.frame_bottom_panel, bottomPanel);
fragmentTransaction.commit();
}
public List<OrderRequest> getOrderRequests() {
orderRequestParser = new OrderRequestParser();
orderRequestArray = new ArrayList<OrderRequest>();
List<OrderRequest> orderRequestArray = orderRequestParser
.listOrderRequest();
Settings.getInstance().setOrderRequestArray(orderRequestArray);
if (orderRequestArray != null) {
Log.e("orderRequestArray",
"Me trajo el orderRequestArray en getOrderRequestArray!");
} else {
Log.e("orderRequestArray",
"No me trajo el orderRequestArray en getOrderRequestArray!");
}
return orderRequestArray;
}
public void setOrderRequestArray(List<OrderRequest> orderRequestArray) {
this.orderRequestArray = orderRequestArray;
}
public List<OrderRequest> getOrderRequestArray() {
return orderRequestArray;
}
}






-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to