OnSuccess方法在活动完成后响应以填充微调控件

时间:2017-12-12 17:43:44

标签: java android loopj

Hello软件开发人员。

我正在开发一个使用REST服务的Android应用程序,因此我正在使用Http客户端库(http://loopj.com/android-async-http/),但我想填充一个微调控件,所以我首先调用方法fillSpinnerTiposSiniestros并填充一个ArrayList稍后使用该ArrayList来填充微调器的适配器,但我不知道为什么首先建立适配器然后调用REST客户端,因此微调器仍为空。

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_edit_sinister);

    editTextValorClave = (EditText) findViewById(R.id.editTextSinisterValorClave);
    editTextValorCurp = (EditText) findViewById(R.id.editTextSinisterValorCurp);
    editTextValorDependencia = (EditText) findViewById(R.id.editTextSinisterValorDependencia);
    editTextValorRFC = (EditText) findViewById(R.id.editTextSinisterValorRFC);
    editTextValorStatus = (EditText) findViewById(R.id.editTextSinisterValorStatus);
    //editTextValorTipoSiniestro = (EditText) findViewById(R.id.editTextSinisterValorTipoSiniestro);
    spinnerTipoSiniestro = (Spinner) findViewById(R.id.spinnerTipoSiniestro);

    editTextValorNumAmbulancia = (EditText) findViewById(R.id.editTextSinisterNumAmbulancia);
    editTextValorNombreSieniestro = (EditText) findViewById(R.id.editTextSinisterNombreSieniestro);
    editTextValorLocalizacion = (EditText) findViewById(R.id.editTextSinisterValorLocalizacion);
    textViewMuestraEncabezado = (TextView) findViewById(R.id.textViewTitulo);

    botonAccionAddEdit = (Button) findViewById(R.id.buttonSinisterAddEdit);
    botonAccionAddEdit.setOnClickListener(eventoAccionBotonAddEdit);

    Bundle miBlunde = getIntent().getExtras();

    opcion = miBlunde.getInt("Opcion");
    claveGlobal = miBlunde.getInt("Clave");

    CargarDatosSpinner;
}

private void rellenarSpinnerTiposSiniestros(){
    List<Header> encabezados = new ArrayList<Header>();
    encabezados.add(new BasicHeader("Accept", "application/json"));


    SiniestroRESTClient.get(AddEditSinisterActivity.this, "api/TipoSiniestro", encabezados.toArray(new Header[encabezados.size()]), null,
            new JsonHttpResponseHandler()
            {
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONArray response)
                {
                    arrayTipoSiniestro = new ArrayList<TipoSiniestro>();
                    tipoSiniestroAdapter = new SpinnerTipoSiniestroAdapter(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, (TipoSiniestro[]) arrayTipoSiniestro.toArray());

                    for(int i = 0; i < response.length(); i++)
                    {
                        try
                        {
                            tipoSiniestroAdapter.add(new TipoSiniestro(response.getJSONObject(i)));
                        }
                        catch(JSONException e)
                        {
                            e.printStackTrace();
                        }
                    }
                    tipoSiniestroAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    spinnerTipoSiniestro.setAdapter(tipoSiniestroAdapter);
                }
            });
}

我尝试使用此解决方案asynchttpclient gives response after activity,但我有一个例外:

  

致命异常:AsyncTask#1                                                                            处理:com.maestria.lostperson,PID:13433                                                                            java.lang.RuntimeException:执行doInBackground()时发生错误                                                                                在android.os.AsyncTask $ 3.done(AsyncTask.java:309)                                                                                在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)                                                                                at java.util.concurrent.FutureTask.setException(FutureTask.java:223)                                                                                在java.util.concurrent.FutureTask.run(FutureTask.java:242)                                                                                在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:234)                                                                                在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)                                                                                at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:588)                                                                                在java.lang.Thread.run(Thread.java:818)                                                                             引起:java.lang.IllegalArgumentException:AsyncHttpClient中使用的同步ResponseHandler。您应该在循环线程中创建响应处理程序或使用SyncHttpClient。                                                                                在com.loopj.android.http.AsyncHttpClient.sendRequest(AsyncHttpClient.java:1493)                                                                                在com.loopj.android.http.AsyncHttpClient.get(AsyncHttpClient.java:1095)                                                                                在com.maestria.lostperson.clients.SiniestroRESTClient.get(SiniestroRESTClient.java:24)                                                                                at com.maestria.lostperson.AddEditSinisterActivity $ 2.doInBackground(AddEditSinisterActivity.java:159)                                                                                在android.os.AsyncTask $ 2.call(AsyncTask.java:295)                                                                                在java.util.concurrent.FutureTask.run(FutureTask.java:237)                                                                                在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:234)                                                                                在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)                                                                                at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:588)                                                                                在java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:0)

您正尝试从后台线程触摸UI组件。只需添加:

  runOnUiThread(new Runnable() {
        @Override
        public void run() {
             arrayTipoSiniestro = new ArrayList<TipoSiniestro>();
                tipoSiniestroAdapter = new SpinnerTipoSiniestroAdapter(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, (TipoSiniestro[]) arrayTipoSiniestro.toArray());

                for(int i = 0; i < response.length(); i++)
                {
                    try
                    {
                        tipoSiniestroAdapter.add(new TipoSiniestro(response.getJSONObject(i)));
                    }
                    catch(JSONException e)
                    {
                        e.printStackTrace();
                    }
                }
                tipoSiniestroAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinnerTipoSiniestro.setAdapter(tipoSiniestroAdapter);
        }
    });

围绕你的回复。或者在调用异步方法之前创建一个Handler并在返回时使用Handler来处理它。无论你喜欢哪种都很好。它不够重要,不能以某种方式强烈偏好。