提问者:小点点

asynctask函数无法识别结果值


我通过AsyncTask访问JSON,但在onPostExecute中,String结果没有得到结果值。

我希望它读取json并返回值“perfila”,之后,发送new到主活动

信用谷歌翻译……

访问MainActivity到MecanismoString

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);

异步任务MecanismoString

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);

    }

}

返回MecanismoString值的代码

public void carregarString(String result) {

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

    }

错误日志

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ic.eng.br.eunaobra, PID: 13735
    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)
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
    (HTTPLog)-Static: isSBSettingEnabled false
I/qtaguid: Untagging socket 76 failed errno=-22
W/NetworkManagementSocketTagger: untagSocket(76) failed with errno -22
Application terminated.

共1个答案

匿名用户

请传递正确的上下文和执行接口对象:

MecanismoString mecanismoString = new MecanismoString(MainActivity.this, correct execinterface object);

请将getApplicationContext()carareString()中删除,因为您已经有了上下文

 public void carregarString(String result) {
        Toast.makeText(context,result,Toast.LENGTH_SHORT) .show();
    }