-3

First of all i have to say, i have read this:

How to get the return value from onpostexecute method in android?

And that:

How do I return a boolean from AsyncTask?

With no help, maybe i didn't understand what they were saying. Here is my porblem: i just need to get back the String value which is in my OnPostExecute method. not from doInBackground, i need it from my OnPostExecute.

My code:

 public void max(String build,String cls)
{

    BackgroundTask3 myTask = new BackgroundTask3();
    myTask.execute(build,cls);


    Toast.makeText(getApplicationContext(),"fcur is" + Fcur,Toast.LENGTH_LONG).show();

}

and:

public class BackgroundTask3 extends AsyncTask<String,Void,String>
{

    String capacity_url;
    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
    String finalValue;
      json_capacity = result;
        finalValue=parseMax();
        Toast.makeText(getApplicationContext(),"finalValue is" + finalValue,Toast.LENGTH_LONG).show();
        super.onPostExecute(result);

    }

    @Override
    protected void onPreExecute() {
        capacity_url = "http://www.skedge.co.il/ANCurrentCapacity.php";
    }

    @Override
    protected String doInBackground(String... params) {
        String cls,build;
        build= params[0];
        cls = params[1];

        try {
            URL url = new URL (capacity_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
            String data_string = URLEncoder.encode("Fclass","UTF-8")+"="+URLEncoder.encode(cls,"UTF-8")+"&"+
                    URLEncoder.encode("building","UTF-8")+"="+URLEncoder.encode(build,"UTF-8")+"&"+
                    URLEncoder.encode("hour","UTF-8")+"="+URLEncoder.encode(hour,"UTF-8");
            bufferedWriter.write(data_string);
            bufferedWriter.flush();
            bufferedWriter.close();
            //here is the new
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
            StringBuilder stringBuilder = new StringBuilder();
            while ((JSON_STRING = bufferedReader.readLine())!= null)
            {
                stringBuilder.append(JSON_STRING+"\n");
            }
            outputStream.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return stringBuilder.toString().trim();


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }
}

All i want to do is: assign the value which is in finalValue String inside the OnPostExecute,to the Fcur (public String) which is inside to max function.

Looks very simple but for some reason i can't get it done.

Community
  • 1
  • 1
Yarden Rotem
  • 85
  • 12

1 Answers1

2

Best way to get a callback from background thread is to use interfaces as a callback from AsyncTask for example:

create an interface that can be called in onPostExecute()

public interface ResponseCallback {

    void onRespond();
}

and before calling asynckTask define it like this:

   ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onRespond() {
              //code to be done after calling it from onPostExecute
            }
        };

and pass callback to the constructor of of the asynckTask and call it in onPostExecute


of course you can modify the signature of the interface to what ever you want.

Atef Hares
  • 4,715
  • 3
  • 29
  • 61