回调函数从异步任务中恢复数据

时间:2015-03-09 08:19:54

标签: android asynchronous interface callback

我想从异步任务中恢复数据而不是直接使用阅读器上的get方法,因为它会阻止UI。所以我已经实现了一个简单的接口,它提供了一个回调函数,wicth传递来自Url的数据。但是当我启动应用程序时,它会崩溃。这是我的代码:

      MAIN ACTIVITY:
            public class MainActivity extends ActionBarActivity implements OnTaskComplete{

                String result = null;

                @Override
                public void callBackFunction(String s) {
                    result = s;
                }

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    String handler = null;

                    try {
                        URL address = new URL("http://www.youth-stories.com/api/focusOnAll.php");
                        MyAsync reader = new MyAsync(this, this, handler);
                        reader.execute(address);

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

                    Toast.makeText(this, handler, Toast.LENGTH_LONG).show();

                }

            }


            ASYNC TASK:

            public class MyAsync extends AsyncTask<URL, Void, String> {

                ProgressDialog dialog = null;
                Context context = null;

                private OnTaskComplete mlistener;
                private String myString;

                public MyAsync(Context context, OnTaskComplete mlistener, String contents) {
                    this.mlistener = mlistener;
                    myString = contents;
                    dialog = new ProgressDialog(context);


                }

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    dialog.setMessage("Loading...");
                    dialog.show();
                }

                String result = null;

                @Override
                protected String doInBackground(URL... params) {


                    try {
                        BufferedReader reader = new BufferedReader(
                                new InputStreamReader(
                                        params[0].openStream()
                                )
                        );

                        result = reader.readLine();

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

                    return result;
                }

                @Override
                protected void onPostExecute(String s) {
                    super.onPostExecute(s);
                    mlistener.callBackFunction(s);
                    myString = s;
                    dialog.dismiss();
                }

            }

            INTERFACE:
            public interface OnTaskComplete {
                public void callBackFunction(String s);
            }


03-09 09:21:04.975  24825-24853/com.example.callbackdata D/OpenGLRenderer﹕ Render dirty regions requested: true
03-09 09:21:04.981  24825-24825/com.example.callbackdata D/Atlas﹕ Validating map...
03-09 09:21:05.004  24825-24858/com.example.callbackdata E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: com.example.callbackdata, PID: 24825
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:451)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
            at java.net.InetAddress.getAllByName(InetAddress.java:215)
            at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
            at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
            at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
            at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
            at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:190)
            at java.net.URL.openStream(URL.java:470)
            at com.example.callbackdata.MyAsync.doInBackground(MyAsync.java:44)
            at com.example.callbackdata.MyAsync.doInBackground(MyAsync.java:12)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
            at libcore.io.Posix.android_getaddrinfo(Native Method)
            at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:438)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
            at java.net.InetAddress.getAllByName(InetAddress.java:215)
            at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
            at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
            at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
            at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
            at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:190)
            at java.net.URL.openStream(URL.java:470)
            at com.example.callbackdata.MyAsync.doInBackground(MyAsync.java:44)
            at com.example.callbackdata.MyAsync.doInBackground(MyAsync.java:12)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: android.system.ErrnoException: android_getaddrinfo failed: EACCES (Permission denied)
            at libcore.io.Posix.android_getaddrinfo(Native Method)
            at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:438)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
            at java.net.InetAddress.getAllByName(InetAddress.java:215)
            at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
            at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
            at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
            at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
            at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:190)
            at java.net.URL.openStream(URL.java:470)
            at com.example.callbackdata.MyAsync.doInBackground(MyAsync.java:44)
            at com.example.callbackdata.MyAsync.doInBackground(MyAsync.java:12)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
03-09 09:23:57.001  25225-25225/com.example.callbackdata I/art﹕ Late-enabling -Xcheck:jni
03-09 09:23:57.174  25225-25260/com.example.callbackdata D/OpenGLRenderer﹕ Render dirty regions requested: true
03-09 09:23:57.179  25225-25225/com.example.callbackdata D/Atlas﹕ Validating map...
03-09 09:23:57.224  25225-25260/com.example.callbackdata I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/24/14, 167c270, I68fa98814b
03-09 09:23:57.225  25225-25260/com.example.callbackdata I/OpenGLRenderer﹕ Initialized EGL, version 1.4
03-09 09:23:57.237  25225-25260/com.example.callbackdata D/OpenGLRenderer﹕ Enabling debug mode 0
03-09 09:25:29.852  25621-25639/com.example.callbackdata D/OpenGLRenderer﹕ Render dirty regions requested: true
03-09 09:25:29.862  25621-25621/com.example.callbackdata D/Atlas﹕ Validating map...
03-09 09:25:29.927  25621-25639/com.example.callbackdata I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/24/14, 167c270, I68fa98814b
03-09 09:25:29.927  25621-25639/com.example.callbackdata I/OpenGLRenderer﹕ Initialized EGL, version 1.4
03-09 09:25:29.940  25621-25639/com.example.callbackdata D/OpenGLRenderer﹕ Enabling debug mode 0

1 个答案:

答案 0 :(得分:0)

Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)

您需要在清单中添加此行:

<uses-permission android:name="android.permission.INTERNET" />

编辑: 您似乎已经完成了它,是否可以将您的清单文件添加到您的帖子中?