How to get the value of an AsyncTask? Wait until you get the result

1

First of all, I am somewhat inexperienced (very inexperienced) with AsyncTask and I have two problems.

first I pass a user and pass to the Task to compare the ones with those of my DB in the Login section and the second is the same but I need to fill a Spinner with the answer in the SpinnerQ section.

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

    AlertDialog AD;
    Context context;
    MyTask (Context ctx){
        context =ctx;
    }
    boolean  validacion;
    @Override
    public String doInBackground(String... params) {

        String type = params[0];



        if(type=="spinnerq"){
            String registro_url = "http://10.0.2.2/spinner2.php";
            String dataParsed, singleParsed;
            String json = "";
            String result = "";
            try {

                URL url = new URL(registro_url);
                HttpURLConnection http = (HttpURLConnection)url.openConnection();
                http.setRequestMethod("POST");

                http.setDoInput(true);
                InputStream IS =http.getInputStream();
                BufferedReader BR = new BufferedReader(new InputStreamReader(IS,"iso-8859-1"));
                String Line;
                while((Line=BR.readLine())!=null){

                    json +=Line;

                }
                BR.close();
                IS.close();
                http.disconnect();
                result=json.toString();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return result; }
        return null;    }

    @Override
    protected void onPreExecute() {
       AD = new AlertDialog.Builder(context).create();
        AD.setTitle("Login Status");
    }

    @Override
    public void onPostExecute(String result) {
       AD.setMessage(result);
       AD.show();
    }

    @Override
    protected void onProgressUpdate(String... values) {
    }
}

This brings me this Json that can be seen in an alert dialog

  

{"Question": "Tangananica or Tanganana?"} {"Question": "What happens to   Lupita? "} {" Question ":" what will the black want? "}

The problem is that I can not pass it to my activity and put it on the spinner.

public class Seleccionpregunta extends AppCompatActivity {

static Spinner sp;
static TextView tv;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_seleccionpregunta);
    sp=(Spinner)findViewById(R.id.questionSpinner);
    tv = (TextView) findViewById(R.id.textView3);
    MyTask BW = new MyTask(this);
    String type = "spinnerq";
    BW.execute(type);



}

I would really appreciate it if you can help me!

    
asked by Sodro 14.12.2017 в 01:36
source

1 answer

3

You can do it this way using the get () , to obtain the value obtained in onPostExecute() :

  String resultadoAsynctask =  new MyAsyncTask().execute().get();

You must take into account that the process will wait until you get the AsyncTask result, this by the use of get () :

  

get () Wait if it is necessary for the calculation to be completed,   and then retrieve your result.

In your code it would be:

 try {

        String type = "spinnerq";
        String resultadoAsynctask =  new MyTask().execute(type).get();

    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    
answered by 14.12.2017 / 18:42
source