2017-01-07 72 views
0

我使用的AsyncTask在後臺做一些操作...錯誤的AsyncTask ..... NetworkOnMainThreadException

我打電話onCreate方法中的AsyncTask通過

新BGSERIVCE()。執行();

public class BGSERIVCE extends AsyncTask<Void,Void,Void> { 


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


      FirebaseUser currentuser = FirebaseAuth.getInstance().getCurrentUser(); 

      Cdb = getApplicationContext().openOrCreateDatabase("zumi1.db", Context.MODE_PRIVATE, null); 
      Cdb.execSQL(TABLE_CREATE); 


//   Toast.makeText(getApplicationContext(), "background Running....", Toast.LENGTH_SHORT).show(); 
      String email_Current = currentuser.getEmail().replace(".", "@"); 
      ref = new Firebase("https://zumi-60a8f.firebaseio.com/Users/"); 
      final ContentValues values = new ContentValues(); 
      ref.child(email_Current).addValueEventListener(new com.firebase.client.ValueEventListener() { 
       @Override 
       public void onDataChange(com.firebase.client.DataSnapshot snapshot) { 
        for (com.firebase.client.DataSnapshot postSnapshot : snapshot.getChildren()) { 


         //Adding it to a string 
         String url = snapshot.child("image").getValue().toString(); 
         String Dname = snapshot.child("Dname").getValue().toString(); // NAME 

         String email = snapshot.child("email_phone").getValue().toString().replace("@zumi.com", ""); // [email protected] 
         String status_E = snapshot.child("status").getValue().toString(); 


         values.put("email_phone", email); 
         values.put("status", status_E); 
         values.put("Dname", Dname); 
         values.put("snyc", "Yes"); 
         try { 
          Toast.makeText(MainActivity.this, "URL : " + url, Toast.LENGTH_SHORT).show(); 
          InputStream is = (InputStream) new URL(url).getContent(); 
          byte[] image = new byte[is.available()]; 
          is.read(image); 
          // values.put("image", image); 

          Cdb.insert("current_Luser", null, values); 

          is.close(); 

         } catch (Exception e) { 
          //Toast.makeText(MainActivity.this, "error Occured : "+e, Toast.LENGTH_SHORT).show(); 
          Log.e("", "" + e); 

         } 
         // values.put("image",); 


        } 
       } 

       @Override 
       public void onCancelled(FirebaseError firebaseError) { 
        //System.out.println("The read failed: " + firebaseError.getMessage()); 
       } 
      }); 

      return null; 
     } 
    } 

錯誤:>>>

[01-08 00:09:54.023 8732:8732 E /] android.os.NetworkOnMainThreadException

01-08 00:09:54.028 8732-8732/com.example.cosmic.zumi_test I/System.out: 

開放: https://firebasestorage.googleapis.com/v0/b/zumi-60a8f.appspot.com/o/Profile_Image%2Fcropped-122662379.jpg?alt=media&token=c8f3e9ee-637d-4bdd-9a76-a186ecd07e37

01-08 00:09:54.028 8732-8732/com.example.cosmic.zumi_test I/System.out: [CDS][DNS] getAllByNameImpl netId = 0 
01-08 00:09:54.028 8732-8732/com.example.cosmic.zumi_test D/libc-netbsd: [getaddrinfo]: hostname=firebasestorage.googleapis.com; 

servname =(null); cache_mode =(null),netid = 0; mark = 0

01-08 00:09:54.028 8732-8732/com.example.cosmic.zumi_test D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); 

ai_flags = 4; ai_family = 0

請幫助....

+1

請編輯您的問題,併發布你的崩潰相關的完整的Java堆棧跟蹤。 – CommonsWare

回答

1

即使你開始運行在不同的線程中的AsyncTask,您就onDataChange回調預先在主線程上。

在那裏,一旦達到 InputStream is = (InputStream) new URL(url).getContent(); 崩潰在NetworkOnMainThreadException

火力地堡做網絡調用工作線程你和代表工作。使用的AsyncTask或線程可運行爲InputStream

FirebaseUser currentuser = FirebaseAuth.getInstance().getCurrentUser(); 
    Cdb = getApplicationContext().openOrCreateDatabase("zumi1.db", Context.MODE_PRIVATE, null); 
    Cdb.execSQL(TABLE_CREATE); 

    String email_Current = currentuser.getEmail().replace(".", "@"); 
    ref = new Firebase("https://zumi-60a8f.firebaseio.com/Users/"); 
    final ContentValues values = new ContentValues(); 
    ref.child(email_Current).addValueEventListener(new com.firebase.client.ValueEventListener() { 
     @Override 
     public void onDataChange(com.firebase.client.DataSnapshot snapshot) { 
      for (com.firebase.client.DataSnapshot postSnapshot : snapshot.getChildren()) { 

       //Adding it to a string 
       String url = snapshot.child("image").getValue().toString(); 
       String Dname = snapshot.child("Dname").getValue().toString(); // NAME 

       String email = snapshot.child("email_phone").getValue().toString().replace("@zumi.com", ""); // [email protected] 
       String status_E = snapshot.child("status").getValue().toString(); 

       values.put("email_phone", email); 
       values.put("status", status_E); 
       values.put("Dname", Dname); 
       values.put("snyc", "Yes"); 
       new Thread(new Runnable() { 
        @Override 
        public void run() { 
         try { 
          Toast.makeText(MainActivity.this, "URL : " + url, Toast.LENGTH_SHORT).show(); 
          InputStream is = (InputStream) new URL(url).getContent(); 
          byte[] image = new byte[is.available()]; 
          is.read(image); 
          // values.put("image", image); 
          Cdb.insert("current_Luser", null, values); 
          is.close(); 

         } catch (Exception e) { 
          //Toast.makeText(MainActivity.this, "error Occured : "+e, Toast.LENGTH_SHORT).show(); 
          Log.e("", "" + e); 
         } 
        } 
       }).start(); 
      } 
     } 
    }); 
+0

你能幫我解釋一下示例代碼嗎... PLs ..我是一個初學者 –

+0

就是這樣,看看它是否適合你 – pantos27

+0

@PraveshKumarLamba? – pantos27

相關問題