0

在这里,首先,当数据同步到本地数据库时,我的应用程序从服务器同步数据后台线程是布尔类型,因此我无法调用功能来从那里获取图像。如果我从 postexecute 调用方法然后我得到 NetworkOnMainthread 异常我被困在我需要使用安全线程的情况下面是代码

protected void onPreExecute() {
    mDialog = ProgressDialog.show(viewContext, "", "Synchronizing Data",
            true);

};

/*
 * (non-Javadoc)
 * 
 * @see android.os.AsyncTask#doInBackground(Params[])
 */
@Override
protected Boolean doInBackground(Void... arg0) {
    if (type.contains("ferry")) {
        return SynchronizeRepositoryFerry(false);

    } else {
        return SynchronizeRepositories(false, initialSync);
    }
}

/*
 * (non-Javadoc)
 * 
 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
 */
@Override
protected void onPostExecute(Boolean result) {
    this.syncComplete = result;
    mDialog.dismiss();
    if (type.contains("Dash")) {
        new BackgroundTask().execute();
    }
    else{
        Intent intent = new Intent();
        intent.setClass(viewContext, classType);
        viewContext.startActivity(intent);

    }


}

class BackgroundTask extends AsyncTask<Void, Void, Void> {
    ProgressDialog mDialog;

    protected void onPreExecute() {
        mDialog = ProgressDialog.show(viewContext, "", "Loading Images",
                true);
    };

    @Override
    protected Void doInBackground(Void... params) {
        try {
            int id = 0;
            String url = "http://i.zdnet.com/blogs/3-29-androids.jpg";
            com.jumbybay.businessobjects.User user = new com.jumbybay.businessobjects.User();
            DatabaseHelper dbHelper = new DatabaseHelper(viewContext);
            IUserRepository repository = dbHelper.getUserRepository();
            List<com.jumbybay.businessobjects.User> imageList;
            try {
                imageList = repository.Retrieve();
                for (int i = 0; i < imageList.size(); i++) {
                    user = imageList.get(i);
                    // url = user.getPicture();
                    id = user.getId();
                    savesd(id, url);

                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (final Exception e) {

        }
        return null;
    }

    public void savesd(int id, String uri) throws IOException {
        URL url;
        if (uri == null) {
            url = new URL("http://i.zdnet.com/blogs/3-29-androids.jpg");
        } else {
            url = new URL(uri);
        }
        InputStream input = url.openStream();
        try {
            File storagePath = Environment.getExternalStorageDirectory();
            OutputStream output = new FileOutputStream(new File(
                    storagePath, +id + ".jpg"));

            try {
                byte[] buffer = new byte[20000];
                int bytesRead = 0;
                while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
                    output.write(buffer, 0, bytesRead);
                }
            } finally {
                output.close();
            }
        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            input.close();
        }

    }

    protected void onPostExecute(Void result) {
        mDialog.dismiss();
        Intent intent = new Intent();
        /*
         * intent.setClass(viewContext, classType);
         * viewContext.startActivity(intent);
         */

        intent.setClass(viewContext, classType);
        viewContext.startActivity(intent);

    };
}
4

2 回答 2

1
@Override
    protected Boolean doInBackground(Void... arg0) {
        if (type.contains("ferry")) {
            return SynchronizeRepositoryFerry(false);

        } else {
            SynchronizeRepositories(false, initialSync);
            com.jumbybay.businessobjects.User user = new com.jumbybay.businessobjects.User();
            DatabaseHelper dbHelper = new DatabaseHelper(viewContext);
            IUserRepository repository = dbHelper.getUserRepository();
            List<com.jumbybay.businessobjects.User> imageList;
            try {
                int id;
                String url = "http://i.zdnet.com/blogs/3-29-androids.jpg";
                imageList = repository.Retrieve();
                for (int i = 0; i < imageList.size(); i++) {
                    user = imageList.get(i);
                    // url = user.getPicture();
                    id = user.getId();
                    savesd(id, url);
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block// url = user.getPicture();
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        return SynchronizeRepositories(false, initialSync);
    }
于 2012-08-28T05:09:31.303 回答
0

这个问题有两个解决方案,但第一个是很好的解决方案。

1)不要在主 UI 线程中编写网络调用,为此使用异步任务。

2) 在 setContentView(R.layout.activity_main); 之后将以下代码写入 MainActivity 文件;但这不是正确的方法。

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

并将下面的 import 语句放入您的 java 文件中。

import android.os.StrictMode;

有关更多信息,请参见下面的链接。

引起:android.os.NetworkOnMainThreadException

于 2012-08-28T05:49:07.790 回答