在主UI上执行网络进程

时间:2012-11-27 13:14:13

标签: java android eclipse json asynchronous

我的Android应用程序需要运行一些基本数据。使用JSON从服务器下载此数据。在Xcode中我只是使用了sendynchronous请求,但是当我在主ui上进行网络连接时,我发现Eclipse给了我一个错误。 在asynctask上发现了很多东西,但我希望我的应用程序等到下载所需的数据(同步?)。

我尝试使用asynctask .execute()。get()并在onPostExecute中设置变量,但是当我返回变量时,我得到一个NullPointerException。有人知道如何使这项工作?在应用程序运行之前我真的需要这些数据,所以我希望我的应用程序等到数据下载完毕。

MainActivity称之为:

SingletonClass appIDSingleton = SingletonClass.getInstance();
this.ID = appIDSingleton.getAppID();

Singleton类:

public String getAppID() {
           try {
            new DownloadAppID().execute(APP_ID_URL).get(5000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TimeoutException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           return AppID; //AppID is still NULL (because the download isnt finished yet?)
       }

private class DownloadAppID extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String response = "";
            for (String url : urls) {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                    HttpResponse execute = client.execute(httpGet);
                    InputStream content = execute.getEntity().getContent();

                    BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
                    String s = "";
                    while ((s = buffer.readLine()) != null) {
                        response += s;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return response;
        }

        @Override
        protected void onPostExecute(String result) {

            System.out.println(result);
            AppID = result;
            }
       }

1 个答案:

答案 0 :(得分:0)

您需要了解您的getAppID方法无法返回将异步计算的结果。

例如,您可以为异步任务提供侦听器,以便在应用程序ID可用时通知:

    SingletonClass appIDSingleton = SingletonClass.getInstance();
    appIDSingleton.getAppID(new AppIdDownloadListener() {

        @Override
        public void appIDAvailable(String appId) {
            this.ID = appId; 
        }
    });


public void getAppID(AppIdDownloadListener listener) {
    try {
        new DownloadAppID(listener).execute(APP_ID_URL).get(5000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TimeoutException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public interface AppIdDownloadListener {
    public void appIDAvailable(String appId);
}

private class DownloadAppID extends AsyncTask<String, Void, String> {

    private AppIdDownloadListener listener;

    public DownloadAppID(AppIdDownloadListener listener) {
        this.listener = listener;
    }

    @Override
    protected String doInBackground(String... params) {
        /* Your stuff here */
    }

    @Override
    protected void onPostExecute(String result) {
        System.out.println(result);
        listener.appIDAvailable(result);
    }
}