2017-06-20 15 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私はこれを克服することができますexecuting.Howではありません例外、すでにこの例外はチェックされていますが、解決策が機能していません。お手伝いください。事前に感謝します。どのようにandroid.os.NetworkOnMainThreadExceptionを解決するには?

+0

あなたは 'NetworkOnMainThread'が一般的な例外であるため、あなたのコードは未読であり、あなたの質問は重複の候補となります。コードを書式化して、他のソリューションが機能しなかった理由を説明してください。 –

+0

[どうすればandroid.os.NetworkOnMainThreadExceptionを修正できますか?](https://stackoverflow.com/questions/6343166/how-do-i-fix-android-os-networkonmainthreadexception) – Enzokie

答えて

0

アプリケーションがメインスレッドにネットワーク操作を実行しようとしたときandroid.os.NetworkOnMainThreadException

がスローされます。 これは、Honeycomb SDKまたはそれ以降のバージョンをターゲットとするアプリケーションでのみスローされます。アプリケーションがメインスレッドでネットワーク操作を実行しようとしていないことを確認します。

関連する問題