Application closing unexpectedly when sending post request

Asked

Viewed 39 times

1

Well I’m developing a screen using the holder view and in it I am sending a JSON to the server, but it has been a problem. By clicking the send button it simply closes the application, and in the error log nothing appears.

What could be the problem? I will put all my fragment for you to take a look:

@EFragment(R.layout.fragment_indique_restaurante)
public class FragmentIndique extends Fragment {

@ViewById
EditText txtIndicacao;

RetornoLogin cliente;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_indique_restaurante, container, false);
    Button btnEnviar = (Button) view.findViewById(R.id.btnEnviarIndicacao);

    btnEnviar.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            enviarIndicacao();
        }
    });

    return view;
}

@Override
public void onResume(){
    super.onResume();
    TextView toolbarTitle = (TextView) getActivity().findViewById(
            R.id.toolbar_title);
    toolbarTitle.setText(R.string.menu_recomende_restaurante);
}

public void enviarIndicacao(){

    //cliente = (RetornoLogin) getIntent().getSerializableExtra(CLIENTE);

    String txtObs = txtIndicacao.getText().toString();
   // retornoLogin.getCliente().getNomeCliente()

    try{

        //JSONObject jsonObj = new JSONObject(txtObs);
        JSONObject my_obj = new JSONObject();
        my_obj.put("titulo", "JSON x XML: a Batalha Final");
        my_obj.put("ano", 2012);
        my_obj.put("genero", "Ação");


        final AsyncHttpClient request = new AsyncHttpClient();
        request.addHeader("Authorization", Constantes.TOKEN_SERVIDOR);

        // Parametros
        RequestParams params = new RequestParams();
        params.put("cliente", my_obj);

        // Exibe a mensagem de progresso
        final ProgressDialog progress = MessageUtil.showProgress(getActivity(), R.string.aguarde, R.string.enviando_indicacao, true);
        final Context mContext = getContext();

        progress.setOnCancelListener(new DialogInterface.OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                request.cancelRequests(mContext, true);
            }
        });
        progress.show();

        request.post(Constantes.URL_CADASTRO, params,
                new AsyncHttpResponseHandler() {

                    @Override
                    public void onSuccess(String resposta) {
                        progress.dismiss();
                        sucessoResposta(resposta);
                    }

                    @Override
                    public void onFailure(Throwable t) {
                        progress.dismiss();
                        falhaResposta(t);
                    }
                });
    } catch (Exception e) {
        NegocioLog.inserir(Log.AVISO, e);
        MessageUtil.showError(getActivity(), R.string.erro_desconhecido);
    }
}

private void sucessoResposta(String resposta) {

    RetornoLogin retornoLogin;

    try {
        // Desserializa a resposta
        retornoLogin = JSONParser.parseLogin(resposta);

        // Conseguiu desserializar a resposta, verifica se houve sucesso
        if (retornoLogin.isSucesso()) {

        } else {
            // Exibe a mensagem de erro
            MessageUtil.showError(getActivity(), ResourceUtil.getString(retornoLogin.getMensagem()));
        }
    } catch (Exception e) {
        // Falha ao desserializar objeto, exibe uma mensagem
        MessageUtil.showError(getActivity(), R.string.erro_desconhecido);
        NegocioLog.inserir(Log.AVISO, e);
        return;
    }

}

private void falhaResposta(Throwable t) {
    // Envia para o servidor o log de erro
    MessageUtil.showError(getActivity(), R.string.erro_desconhecido_ws);
 }
}

1 answer

2


Hi, if that’s the whole code, there’s an error in txtIndication. It’s never booted, it’s out of your Try-catch, which is probably why you can’t capture the logs. Apparently the error is right on this first line, which probably returns null, since it does not know its component called 'txtIndication'.

  • I’ll test to see

  • You have yet to say what this component is, as you did in::: Button btnEnviar = (Button) view.findViewById(R.id.btnEnviarIndication);

  • Brother wasn’t even needed, it was just passing inside the Try and ready.

  • Show, but keep in mind that in the future, if you need to use this text, it will give error because it was not initialized. Ai in this case is simple, just point to your R.java file, exactly as you did to the :) hug button!

  • Yeah, I’m right here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.