0

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?

4

3 回答 3

6

您没有覆盖onPostExecute的正确方法。

你有:

protected void onPostExecute(Void result)

你需要:

protected void onPostExecute(Object result)

请注意,您提供的第三个泛型参数的类型是Object. 那是onPostExecute用作参数的类型。所以,方法签名onPostExecute需要接受一个Object,而不是Void

您可能应该在此处使用布尔结果类型而不是对象,并删除 Json 状态类变量。这使您的 AsyncTask 更加灵活,并且可以让您在执行后向用户显示操作完成的一些指示。

于 2013-08-13T13:50:17.353 回答
0

我不得不说你在 AsyncTask 中的代码没有任何意义。

AsyncTask 被设计为从 UI 线程运行的另一个线程。因此,您应该将它用作正在运行的 UI 线程中的内部类,然后 onPostExecute() 部分可以做一些事情来显示结果,或者如果您将它作为独立类保留,您可以作为您的代码。您应该设计一个接口,其他类,如活动或片段,运行 new AsyncTask.execute() 应该实现该接口。

此外,java 不是 javascript。doInBackground() 中的变量仅在函数中受到限制。因此,您在 onPostExecute() 中所做的将一无所获。

您应该使用

JSONObject jsonOrg

作为一个类变量,或者你应该在 doInBackground() 结束时返回它并在 onPostExecute() 中取回它

毕竟,我建议你看一下api文档的例子。虽然它有点复杂,但它完美地展示了一切。

于 2013-08-13T14:03:47.237 回答
-1

尝试使用覆盖方法

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

正如威廉所建议的那样,类型应该与覆盖方法匹配。我已经编辑了下面的答案

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

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

                   }
于 2013-08-13T13:57:16.647 回答