-3
  public class DoLogin extends AsyncTask<String,String,String>
       {
    ResultSet rs2;
    String z = "";
    Boolean isSuccess = false;
    TextView DateOfBooking,Product,CustomerName,
                         Quantity,Destination,DealerName,Remarks,DueDate;

    ArrayList DateOfBooking1 = new ArrayList();
                     ArrayList Product1 = new ArrayList();
                    ArrayList CustomerName1 = new ArrayList();
                    ArrayList Quantity1 = new ArrayList();
                    ArrayList Destination1 = new ArrayList();
                    ArrayList DealerName1  = new ArrayList();
                     ArrayList Remarks1  = new ArrayList();
                ArrayList DueDate1 = new ArrayList();


               @Override
                 protected void onPreExecute() {

                      }

                    @Override
                 protected void onPostExecute(String r) {
        Toast.makeText(OrderRequest.this, r, Toast.LENGTH_SHORT).show();
        if(isSuccess) {

            try {
                addHeaders();

                do{
                    s1 = rs2.getString(1);
                    DateOfBooking1.add(s1);
                    s2 = rs2.getString(2);
                    CustomerName1.add(s2);
                    s3 = rs2.getString(3);
                    Destination1.add(s3);
                    s4 = rs2.getString(4);
                    DealerName1.add(s4);
                    s5 = rs2.getString(5);
                    Product1.add(s5);
                    s6 = rs2.getString(6);
                    Quantity1.add(s6);
                      s7 =rs2.getString(7);
                    Remarks1.add(s7);
                    s8 =rs2.getString(8);
                    DueDate1.add(s8);

                }while(rs2.next());

                if (DateOfBooking1.size() != 0) {
                    for (int j = 0; j < DateOfBooking1.size(); j++) {

                        /** Create a TableRow dynamically **/
                        tr = new TableRow(OrderRequest.this);
                        tr.setLayoutParams(new TableRow.LayoutParams(
                                TableRow.LayoutParams.FILL_PARENT,
                                TableRow.LayoutParams.WRAP_CONTENT));


                        /** Creating a TextView to add to the row **/
                        DateOfBooking = new TextView(OrderRequest.this);

                    DateOfBooking.setText(DateOfBooking1.get(j).toString());
                        DateOfBooking.setTextColor(Color.BLACK);
                        DateOfBooking.setTypeface(Typeface.DEFAULT, 
                                       Typeface.ITALIC);
                        DateOfBooking.setLayoutParams(new 
                                         TableRow.LayoutParams
                               (TableRow.LayoutParams.FILL_PARENT,
                               TableRow.LayoutParams.WRAP_CONTENT));
                        DateOfBooking.setPadding(5, 5, 5, 5);
                        DateOfBooking.setId(j);
                        tr.addView(DateOfBooking);  // Adding textView to 
                                                              tablerow.


                        CustomerName = new TextView(OrderRequest.this);

                      CustomerName.setText(CustomerName1.get(j).toString());
                        CustomerName.setTextColor(Color.BLACK);
                        CustomerName.setTypeface(Typeface.DEFAULT, 
                                                        Typeface.ITALIC);
                        CustomerName.setLayoutParams(new 
                                        TableRow.LayoutParams
                             (TableRow.LayoutParams.FILL_PARENT,
                               TableRow.LayoutParams.WRAP_CONTENT));
                        CustomerName.setPadding(5, 5, 5, 5);
                        CustomerName.setId(j);
                        tr.addView(CustomerName);  // Adding textView to 
                                                             tablerow.


                        Destination = new TextView(OrderRequest.this);
                        Destination.setText(Destination1.get(j).toString());
                        Destination.setTextColor(Color.BLACK);
                        Destination.setTypeface(Typeface.DEFAULT, 
                                                      Typeface.ITALIC);
                        Destination.setLayoutParams(new 
                           TableRow.LayoutParams
                           (TableRow.LayoutParams.FILL_PARENT, 
                                 TableRow.LayoutParams.WRAP_CONTENT));
                        Destination.setPadding(5, 5, 5, 5);
                        Destination.setId(j);
                        tr.addView(Destination);  // Adding textView to 
                                                                 tablerow.

                        DealerName = new TextView(OrderRequest.this);
                        DealerName.setText(DealerName1.get(j).toString());
                        DealerName.setTextColor(Color.BLACK);
                        DealerName.setTypeface(Typeface.DEFAULT, 
                                                 Typeface.ITALIC);
                        DealerName.setLayoutParams(new TableRow.LayoutParams
                                  (TableRow.LayoutParams.FILL_PARENT,
                               TableRow.LayoutParams.WRAP_CONTENT));
                        DealerName.setPadding(5, 5, 5, 5);
                        DealerName.setId(j);
                        tr.addView(DealerName);  // Adding textView to 
                                                                   tablerow.

                        Product = new TextView(OrderRequest.this);
                        Product.setText(Product1.get(j).toString());
                        Product.setTextColor(Color.BLACK);
                        Product.setTypeface(Typeface.DEFAULT, 
                                                Typeface.ITALIC);
                        Product.setLayoutParams(new TableRow.LayoutParams
                        (TableRow.LayoutParams.FILL_PARENT, 
                             TableRow.LayoutParams.WRAP_CONTENT));
                        Product.setPadding(5, 5, 5, 5);
                        Product.setId(j);
                        tr.addView(Product);  // Adding textView to 
                                                 tablerow.


                        Quantity = new TextView(OrderRequest.this);
                        Quantity.setText(Quantity1.get(j).toString());
                        Quantity.setTextColor(Color.BLACK);
                        Quantity.setTypeface(Typeface.DEFAULT, 
                                                      Typeface.ITALIC);
                        Quantity.setLayoutParams(new TableRow.LayoutParams
                             (TableRow.LayoutParams.FILL_PARENT,
                 TableRow.LayoutParams.WRAP_CONTENT));
                        Quantity.setPadding(5, 5, 5, 5);
                        Quantity.setId(j);
                        tr.addView(Quantity);  // Adding textView to 
                                                              tablerow.

                        Remarks = new TextView(OrderRequest.this);
                        Remarks.setText(Remarks1.get(j).toString());
                        Remarks.setTextColor(Color.BLACK);
                        Remarks.setTypeface(Typeface.DEFAULT, 
                                                        Typeface.ITALIC);
                        Remarks.setLayoutParams(new TableRow.LayoutParams
                   (TableRow.LayoutParams.FILL_PARENT,
               TableRow.LayoutParams.WRAP_CONTENT));
                        Remarks.setPadding(5, 5, 5, 5);
                        Remarks.setId(j);
                        tr.addView(Remarks);  // Adding textView to 
                                                       tablerow.

                        DueDate = new TextView(OrderRequest.this);
                        DueDate.setText(DueDate1.get(j).toString());
                        DueDate.setTextColor(Color.BLACK);
                        DueDate.setTypeface(Typeface.DEFAULT, 
                                      Typeface.ITALIC);
                        DueDate.setLayoutParams(new 
                   TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 
               TableRow.LayoutParams.WRAP_CONTENT));
                        DueDate.setPadding(5, 5, 5, 5);
                        DueDate.setId(j);
                        tr.addView(DueDate);  // Adding textView to 
                                                               tablerow.

                        tl.addView(tr, new TableLayout.LayoutParams(
                                TableRow.LayoutParams.FILL_PARENT,
                                TableRow.LayoutParams.WRAP_CONTENT));

                    }
                    } else {
                    Toast.makeText(OrderRequest.this, 
             DateOfBooking1+""+Product1+""+DealerName1
            +""+Destination1+""+DueDate1+""+CustomerName1
             +""+Quantity1+""+Remarks1+"Sorry.....",
               Toast.LENGTH_LONG).show();
                }




            }catch(Exception e)
            {
                Log.e("showing",e+"");
            }

                   }

                 }

              @Override
              protected String doInBackground(String... params) {

        try {
            Connection con = (Connection) connectionClass.CONN();
            if (con == null) {
                z = "Error in connection with SQL server";
            } else {
                String query = "select 
                DocDate,CustomerName,Destination,DealerName,
          ProductName,Quantity,Remarks,DueDate from [Dealer].[dbo].
                      [BookingOrder]";
                Statement stmt = con.createStatement();
                rs2 = stmt.executeQuery(query);
                try {

                    if (rs2.next()) {

                        isSuccess = true;
                        z = "Successfully Viewed";
                    }
                }catch (Exception n)
                {
                    z = "selecting";
                    Log.e("selecting",n+"");
                }
        }

        }
        catch (Exception ex)
        {
            isSuccess = false;
            z = "Exceptions";
            Log.e("Exc", ex + "");
            return null;
        }

        return z;
    }
     }

I am getting exception as "android.os.NetworkOnMainThreadException",Resultset is giving me the values which are located in SQL server.but the code after getting values from resultset is not executing.How can I overcome this exception,Already I checked this exception,but the solutions are not working.Help me.Thank you in advance.

  • You code is unreadabe and your question is a high candidate for duplication since `NetworkOnMainThread` is a common exception. Please format your code and add a proper explanation why the other solutions didn't worked. – Iulian Popescu Jun 20 '17 at 06:35
  • Possible duplicate of [How do I fix android.os.NetworkOnMainThreadException?](https://stackoverflow.com/questions/6343166/how-do-i-fix-android-os-networkonmainthreadexception) – Enzokie Feb 20 '18 at 05:19

1 Answers1

0

android.os.NetworkOnMainThreadException

is thrown when an application attempts to perform a network related operation on the main thread. This is only thrown for applications targeting the Honeycomb SDK or higher versions. Ensure that your application is not attempting to perform any network related operation on its main thread.

Manish Singh Rana
  • 822
  • 1
  • 13
  • 26