2017-06-20 68 views
-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; 
    } 
    } 

我得到的例外是“android.os.NetworkOnMainThreadException”,ResultSet是给我分别位于SQL server.but代码从ResultSet中获取值后的值是不是我executing.How能够克服这个异常,我已经检查了这个异常,但解决方案不起作用。请帮助我。提前感谢您。如何解决android.os.NetworkOnMainThreadException?

+0

因为'NetworkOnMainThread'是一个常见的例外,所以你的代码是不可读的,你的问题是一个重复的候选者。请格式化您的代码并为其他解决方案无法正常工作的原因添加正确的解释。 –

+0

可能的重复[如何解决android.os.NetworkOnMainThreadException?](https://stackoverflow.com/questions/6343166/how-doi-i-fix-android-os-networkonmainthreadexception) – Enzokie

回答

0

android.os.NetworkOnMainThreadException

是当应用程序试图在其主线程执行联网操作抛出。 这仅适用于定位到Honeycomb SDK或更高版本的应用程序。确保您的应用程序不尝试在其主线程上执行任何联网操作。

相关问题