简体   繁体   中英

Unable to call methods from onPostExecute

I am using the an asynchronous task to run a JSON downloader as thus: (abridged)

public class JSONDownloader extends AsyncTask<Object, Object, Object>{
    @Override
    protected Object doInBackground(Object... params) {
        if(JSONstate == false){
            try {
                final URL url = new URL([REDACTED]);

                final URLConnection urlConnection = url.openConnection();
                urlConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
                urlConnection.connect();
                final InputStream inputStream = urlConnection.getInputStream();
                final StringBuilder sb = new StringBuilder();
                while (inputStream.available() > 0) {
                    sb.append((char) inputStream.read());
                }
                String result = sb.toString();

                JSONObject jsonOrg = new JSONObject(result);

                String ok = "ok";

                Response = jsonOrg.getString("response");
                System.out.println(Response);

                if(Response.equals(ok)){
                    Settingsresponse = true;

                    orgName = jsonOrg.getString("orgName");
                    System.out.println("orgName" + orgName);
                    accessPointName = jsonOrg.getString("attendanceRecorderName");
                    System.out.println("accessPointName" + accessPointName);
                    lat = jsonOrg.getString("latitude");
                    System.out.println("lat" + lat);
                    longi = jsonOrg.getString("longitude");
                    System.out.println("longi" + longi);
                    floor = jsonOrg.getString("floor");
                    System.out.println("floor" + floor);
                    orgId = jsonOrg.getString("orgId");
                    System.out.println("orgId" + orgId);
                }
                else{
                    System.out.println("Data sent was erroneous");
                    Settingsresponse = false;
                }
            } catch (Exception e) {
                System.err.print(e);      
            }
        }
        else if(JSONstate == true){
            try {                   
                                    [redacted]
                }
                else{
                    System.out.println("Data sent was erroneous");
                    Settingsresponse = false;
                }
            } catch (Exception e) {
                System.err.print(e);      
            }
        }
        return null;
    }
    protected void onPostExecute(Void result){
        if(JSONstate == false){
            System.out.println("This piece of code is definitely being run");
            setfields();
        }
        else if(JSONstate == true){
            settestfields();
        //This method does not run upon the completion of the JSON request, as it supposedly should
                    }
    }
}

Once the JSONRequest has been completed, the 'onPostExecute' method doesn't run. I have been attempting to use this method so that a set of fields can be updated as soon as the request is complete, instead of having to set a definite wait time. Am I simply utilizing the code wrong? Or is there something I've missed?

You aren't overriding the correct method for onPostExecute .

You have:

protected void onPostExecute(Void result)

You need:

protected void onPostExecute(Object result)

Notice the third generic parameter you supplied was of type Object . That's the type that onPostExecute uses as an argument. So, the method signature for onPostExecute needs to accept an Object , not Void .

You should probably use a result type of boolean here rather than object, and remove the Json state class variable. This keeps your AsyncTask more flexible, and could allow you to display some indication the operation completed to the user after execution.

I have to say you codes in AsyncTask is nothing matches the point.

AsyncTask is designed as another thread running out from the UI-thread. So you should either use it as a inner class which is in a running UI-thread, then the onPostExecute() part can do something to show the result, or you as your codes, if you leave it as a stand alone class. You should design an interface, other class, like activity or fragment, which run new AsyncTask.execute() should implements that interface.

Also, java is not javascript. Your variables in doInBackground() is only limited in the function. So what you did in onPostExecute() will get nothing.

You should either use

JSONObject jsonOrg

as a class variable or you should return that at the end of doInBackground() and gain it back in onPostExecute()

After all, I suggest you look at the api document's example. Although it is a little complex, but it shows everything perfect.

try to use override methods

               @Override
               protected void onPostExecute(Void result) {
                    super.onPostExecute(result);
                    Log.i("in on ", "entered");
                    hideProgress();
                   }

As suggested by william the type should match with the override methods. I have edited the answer below

          public class JSONDownloader extends AsyncTask<Object, Object, Object>

               @Override
               protected void onPostExecute(Object result) {
                    super.onPostExecute(result);

                   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM