我使用的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:
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
請幫助....
請編輯您的問題,併發布你的崩潰相關的完整的Java堆棧跟蹤。 – CommonsWare