0

in onCreate, I create and execute pt:

PostTask pt =  new PostTask(this);
pt.execute()

(PostTask extends AsyncTask)

However, the app stops as soon as it gets to the first line of PostTask::doInBackground:

protected String doInBackground(String... params) {
String url=params[0];

Am I missing something here?

Below is the log error:

                                                                 --------- 
beginning of crash
10-21 02:38:31.694 5506-5591/org.researchstack.sampleapp E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                       Process: org.researchstack.sampleapp, PID: 5506
                                                                       java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                           at android.os.AsyncTask$3.done(AsyncTask.java:309)
                                                                           at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                           at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                           at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                           at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                                           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                           at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                           at java.lang.Thread.run(Thread.java:818)
                                                                        Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                                                                           at org.researchstack.sampleapp.PostTask.doInBackground(SampleApplication.java:103)
                                                                           at org.researchstack.sampleapp.PostTask.doInBackground(SampleApplication.java:85)
                                                                           at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                                           at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                           at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                                                                           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                                                                           at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                                           at java.lang.Thread.run(Thread.java:818) 
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
Dan
  • 487
  • 5
  • 19

1 Answers1

0

Since your AsyncTask is expecting the url to come into the params varargs, you need to provide the url when you execute the AsyncTask:

String url = "http://www.example.com"
PostTask pt =  new PostTask(this);
pt.execute(url);

Then, the url will be the first element in params, so the code you already have in the AsyncTask will work:

protected String doInBackground(String... params) {
    String url=params[0];
    //.........
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137