Asynctask未知类型执行

时间:2016-03-26 20:17:29

标签: java android api android-asynctask execute

这是我第一次让APIS返回结果JSON对象。我想我已经得到了异步任务代码,但我不知道如何执行它。这是我的班级代码。 对于我的布局,我只有一个带有onClick ()方法gg的按钮,一个进度条和一个文本视图 这是异步任务:

public class MainActivity extends Activity 
{
    ProgressBar progressBar;
    TextView responseView;
  EditText emailText;
    String URL;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
            progressBar = (ProgressBar) findViewById(R.id.progressBar);
            responseView = (TextView) findViewById(R.id.responseView);
            emailText = (EditText) findViewById(R.id.emailText);
            URL = "https://kgsearch.googleapis.com/v1/entities:search?query=taylor+swift&key=APIKEY&limit=1&indent=True";
}
    public void gg(View v)
    {
            new RetrieveFeedTask.execute();
    }


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

            private Exception exception;

            protected void onPreExecute() {
                    progressBar.setVisibility(View.VISIBLE);
                    responseView.setText("");
                    Toast.makeText(MainActivity.this, "pre execute", Toast.LENGTH_LONG).show();
            }

            protected String doInBackground(Void... urls) {
                    String email = emailText.getText().toString();
                    // Do some validation here


                    try {
                            URL url = new URL(URL);
                            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                            try {
                                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                                    StringBuilder stringBuilder = new StringBuilder();
                                    String line;
                                    while ((line = bufferedReader.readLine()) != null) {
                                            stringBuilder.append(line).append("\n");
                                    }
                                    bufferedReader.close();
                                    return stringBuilder.toString();
                            }
                            finally{
                                    urlConnection.disconnect();
                            }
                    }
                    catch(Exception e) {
                            Log.e("ERROR", e.getMessage(), e);
                            return null;
                    }
            }

            protected void onPostExecute(String response) {
                    if(response == null) {
                            response = "THERE WAS AN ERROR";
                            Toast.makeText(MainActivity.this, "post execute", Toast.LENGTH_LONG).show();
                    }
                    progressBar.setVisibility(View.GONE);
                    Log.i("INFO", response);
                    responseView.setText(response);
            }
    }


    }

所以在public void gg(View v) 我调用了.execute方法,但它给了我一个错误

  

未知类型执行

我是否必须在执行方法中添加一些参数? 如果是这样的话?

感谢。

1 个答案:

答案 0 :(得分:1)

尝试

new RetrieveFeedTask().execute();