android doinbackground无法正常工作

时间:2018-08-08 05:32:46

标签: android-asynctask

我的AysynTask无法正常工作。请帮帮我

这是日志详细信息    在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 08-08 10:54:36.939 28068-28068 / com.example.rajatanurag.guessthecelebrityapp W / System.err:位于android.app.ActivityThread.-wrap12(ActivityThread.java) 08-08 10:54:36.941 28068-28068 / com.example.rajatanurag.guessthecelebrityapp W / System.err:at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)         在android.os.Handler.dispatchMessage(Handler.java:102) 08-08 10:54:36.942 28068-28068 / com.example.rajatanurag.guessthecelebrityapp W / System.err:at android.os.Looper.loop(Looper.java:154)         在android.app.ActivityThread.main(ActivityThread.java:6119)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 08-08 10:54:36.945 28068-28094 / com.example.rajatanurag.guessthecelebrityapp W / System.err:java.lang.ArrayIndexOutOfBoundsException:length = 0;索引= 0 08-08 10:54:36.947 28068-28094 / com.example.rajatanurag.guessthecelebrityapp W / System.err:at com.example.rajatanurag.guessthecelebrityapp.MainActivity $ DownloadTask.doInBackground(MainActivity.java:31)         在com.example.rajatanurag.guessthecelebrityapp.MainActivity $ DownloadTask.doInBackground(MainActivity.java:20) 08-08 10:54:36.948 28068-28094 / com.example.rajatanurag.guessthecelebrityapp W / System.err:at android.os.AsyncTask $ 2.call(AsyncTask.java:305) 08-08 10:54:36.949 28068-28094 / com.example.rajatanurag.guessthecelebrityapp W / System.err:at java.util.concurrent.FutureTask.run(FutureTask.java:237)         在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)         在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:607)         在java.lang.Thread.run(Thread.java:761) 08-08 10:54:37.291 28068-28096 / com.example.rajatanurag.guessthecelebrityapp I / OpenGLRenderer:初始化的EGL版本1.4 08-08 10:54:37.291 28068-28096 / com.example.rajatanurag.guessthecelebrityapp D / OpenGLRenderer:交换行为1 08-08 10:54:37.291 28068-28096 / com.example.rajatanurag.guessthecelebrityapp W / OpenGLRenderer:无法使用EGL_SWAP_BEHAVIOR_PRESERVED选择配置,请重试而无需... 08-08 10:54:37.292 28068-28096 / com.example.rajatanurag.guessthecelebrityapp D / OpenGLRenderer:交换行为0 08-08 10:54:37.349 28068-28096 / com.example.rajatanurag.guessthecelebrityapp D / EGL_emulation:eglCreateContext:0xa2d85400:maj 2 min 0 rcv 2 08-08 10:54:37.395 28068-28096 / com.example.rajatanurag.guessthecelebrityapp D / EGL_emulation:eglMakeCurrent:0xa2d85400:ver 2 0(tinfo 0xa0e7eb60) 08-08 10:54:37.442 28068-28096 / com.example.rajatanurag.guessthecelebrityapp D / EGL_emulation:eglMakeCurrent:0xa2d85400:ver 2 0(tinfo 0xa0e7eb60)

以下是代码: 公共类MainActivity扩展了活动{

public class DownloadTask extends AsyncTask<String,Void,String>
{
    @Override
    protected String doInBackground(String... urls) {

        String result="";
        URL url;
        HttpsURLConnection urlConnection=null;

        try
        {
            url=new URL(urls[0]);
            urlConnection=(HttpsURLConnection)url.openConnection();
            InputStream inputStream=urlConnection.getInputStream();
            InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
            int data=inputStreamReader.read();

            while(data!=-1)
            {
                char current=(char)data;
                result+=current;
                data=inputStreamReader.read();
            }

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

        return null;
    }
}





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

    DownloadTask downloadTask = new DownloadTask();
    String result = null;
    try {

        if (Build.VERSION.SDK_INT >= 11)
            downloadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);


        else

            result = downloadTask.execute("http://www.bollywoodhungama.com/celebrities/top-100/").get();
        Log.i("contents of url", result);


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


}

1 个答案:

答案 0 :(得分:0)

您的问题与AsyncTask本身无关。似乎urls[0]导致ArrayIndexOutOfBoundsException。检查是否将正确的值传递给doInBackground方法。