AsyncTask doInBackground不执行

时间:2016-07-05 20:25:06

标签: android android-asynctask

我尝试了几乎所有解决方案但是我的Inner AsyncTask类存在一些问题。似乎doInBackground()没有被执行。

onPreExecute()onPostExecute()正在运作。但是,doInBackground()不会读取代码。

我非常确定getJSON()100%是正确的,因为我在

之前使用了相同的代码

这是我的班级

public class My extends Activity implements Runnable {
    public static String s = null;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_splash);
            findViewById(android.R.id.content).postDelayed(this, 3000);
            getJSON("My link ");
        }

        @Override
        public void run() {
            startActivity(new Intent(SplashActivity.this, MainActivity.class));
            finish();
        }
}

的AsyncTask:

 private void getJSON(String url) {

    class GetJSON extends AsyncTask<String, Void, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

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

            String uri = params[0];

            BufferedReader bufferedReader = null;
            try {
                URL url = new URL(uri);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                StringBuilder sb = new StringBuilder();

                bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

                String json;
                while((json = bufferedReader.readLine())!= null){
                    sb.append(json+"\n");
                }

                s = sb.toString().trim();

                return sb.toString().trim();

            } catch(Exception e){
                return null;
            }
        }

        @Override
        protected void onPostExecute(String ss) {
            super.onPostExecute(ss);
            s = ss;
        }
    }
    GetJSON gj = new GetJSON();
    gj.execute(url);
}

由于

0 个答案:

没有答案