onPostExecute is not receiving the result value (Android)

Asked

Viewed 39 times

1

Good night,

I am accessing via Asynctask JSON, only on onPostExecute, to String result is not receiving the result value.

Follow Home access code.class

            String login = textousuario.getText().toString();
            String senha = textosenha.getText().toString();
            String urllogin = "http...."
            String tipo = "";
            MecanismoString mecanismoString = new MecanismoString(this, this);
            mecanismoString.execute(tipo, urllogin, login, senha);

Follows code Asynctask

public class MecanismoString extends AsyncTask <String, String, String> {

    private Interfacestring Interfacestring;
    private RequestQueue requestQueue;
    private StringRequest request;
    private Interfacestring execinterface;

    String usuario, senha, result;

    Context context;
    Toast toast;

    public MecanismoString (Context context, Interfacestring execinterface){
        this.context = context;
        this.Interfacestring = execinterface;
    }




    @Override
    public String doInBackground(String... params) {

        Log.i("Script", "1 ->" + params[0]);
        Log.i("Script", "2 ->" + params[1]);
        Log.i("Script", "3 ->" + params[2]);
        Log.i("Script", "4 ->" + params[3]);

        usuario = params[2];
        senha = params[3];

        requestQueue = Volley.newRequestQueue(context);

        request = new StringRequest(Request.Method.POST, params[1], new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);

                    result = jsonObject.getString("perfil");


                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> hashMap = new HashMap<String, String>();
                hashMap.put("login", usuario);
                hashMap.put("senha", senha);

                return hashMap;
            }
        };

        requestQueue.add(request);

        return (result);
    }


    @Override
    protected void onPostExecute(String result) {

        execinterface.carregarString(result);

    }

}

Follow the interface code

public void carregarString(String result) {

        Toast.makeText(context.getApplicationContext(),
                result,
                Toast.LENGTH_SHORT) .show();

    }

In case this code has to return a value that will be executed in Toast in Mainactivity....

Follows dialog of Error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ic.eng.br.eunaobra, PID: 9428
    java.lang.NullPointerException: Attempt to invoke interface method 'void ic.eng.br.eunaobra.Interfacestring.carregarString(java.lang.String)' on a null object reference
        at ic.eng.br.eunaobra.MecanismoString.onPostExecute(MecanismoString.java:94)
        at ic.eng.br.eunaobra.MecanismoString.onPostExecute(MecanismoString.java:22)
        at android.os.AsyncTask.finish(AsyncTask.java:695)
        at android.os.AsyncTask.-wrap1(Unknown Source:0)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
  • Edit your question and add the main Act code where you try to recover the value

  • Updated the post...

No answers

Browser other questions tagged

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