如何解决android.os.NetworkOnMainThreadException?

问题描述 投票:-3回答:1
  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;
    }
     }

我得到异常为“android.os.NetworkOnMainThreadException”,Resultset给了我位于SQL server中的值。但是从resultset获取值后的代码没有执行。我怎么能克服这个异常,我已经检查了这个异常,但解决方案不起作用。帮助我。提前谢谢你。

android
1个回答
0
投票

android.os.NetworkOnMainThreadException

当应用程序尝试在主线程上执行与网络相关的操作时抛出。仅针对Honeycomb SDK或更高版本的应用程序进行此操作。确保您的应用程序未尝试在其主线程上执行任何与网络相关的操作。

© www.soinside.com 2019 - 2024. All rights reserved.