为什么在AsyncTask中抛出NetworkOnMainThreadException?

时间:2019-05-17 17:44:17

标签: amazon-web-services android-asynctask amazon-dynamodb networkonmainthread

每个人。 我正在使用抽屉活动,使用片段更改内容,所以有 PerfilFragment ,例如:

public class PerfilFragment extends Fragment {

    final CurrentView currentView = new CurrentView();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_perfil, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if (getActivity() != null) {
            getActivity().setTitle(R.string.menu_perfil);

            final CognitoCachingCredentialsProvider credentialsProvider;

            if (getContext() != null) {
                AWSConfiguration awsConfig = new AWSConfiguration(getContext());
                credentialsProvider = new CognitoCachingCredentialsProvider(
                        getContext(),
                        awsConfig
                );

                currentView.setCredentialsProvider(credentialsProvider);
                currentView.setView(view);

                final FloatingActionButton fab = view.findViewById(R.id.fab);
                fab.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        new Insert().doInBackground(currentView);
                    }
                });
            }
        }
    }
}

CurrentView 类型是一个内部类,以及 Insert 类:

class CurrentView {
    private CognitoCachingCredentialsProvider credentialsProvider;
    private View view;

    CognitoCachingCredentialsProvider getCredentialsProvider() {
        return credentialsProvider;
    }

    void setCredentialsProvider(CognitoCachingCredentialsProvider credentialsProvider) {
        this.credentialsProvider = credentialsProvider;
    }

    View getView() {
        return view;
    }

    void setView(View view) {
        this.view = view;
    }
}

class Insert extends AsyncTask<CurrentView, Void, Table> {

    private final static String TABLE = "perfil";

    @Override
    protected Table doInBackground(CurrentView... currentView) {
        AmazonDynamoDBClient dbClient;

        try {
            dbClient = new AmazonDynamoDBClient(currentView[0].getCredentialsProvider());
            Table perfilTable = Table.loadTable(dbClient, TABLE);

            Document perfilDocument = new Document();

            final EditText perfilNome = currentView[0].getView().findViewById(R.id.perfil_nome_txt);
            final EditText perfilDataNasc = currentView[0].getView().findViewById(R.id.perfil_datanasc_txt);
            final EditText perfilGenero = currentView[0].getView().findViewById(R.id.perfil_genero_txt);

            perfilDocument.put("userId", currentView[0].getCredentialsProvider().getCachedIdentityId());
            perfilDocument.put("nome", perfilNome.getText().toString());
            perfilDocument.put("datanasc", perfilDataNasc.getText().toString());
            perfilDocument.put("genero", perfilGenero.getText().toString());

            Document perfilInserido = perfilTable.putItem(perfilDocument);

            if (perfilInserido.containsKey("genero")) {
                DynamoDBEntry perfilInseridoGenero = perfilInserido.get("genero");

                if (perfilInseridoGenero != null) {
                    if (perfilInseridoGenero.asString().equals(perfilGenero.getText().toString())) {
                        Snackbar
                                .make(
                                        currentView[0].getView(),
                                        "Perfil atualizado!",
                                        Snackbar.LENGTH_LONG
                                )
                                .setAction("Atualizar", null)
                                .show();
                    }
                }
            }

            return perfilTable;
        } catch (Exception e) {
            Log.e("EXCEPTION", e.getMessage(), e);
            return null;
        }
    }
}

在以下行的 doInBackground 方法内引发异常: Table perfilTable = Table.loadTable(dbClient, TABLE);

我想念什么?

1 个答案:

答案 0 :(得分:0)

AWS开发者论坛的用户awsvbk回答我:

  

您需要调用execute()方法,而不是doInBackground()方法。调用execute()会在后台线程内部调用AsyncTask的doInBackground()方法。

new Insert().execute(<PARAMS>)
     

如果您仍然遇到DynamoDB文档模型的任何其他问题   SDK,请在GitHub上为我们解决一个问题   https://github.com/aws/aws-sdk-android