gpt4 book ai didi

java - Asynctask 未知类型执行

转载 作者:行者123 更新时间:2023-12-01 10:09:20 26 4
gpt4 key购买 nike

这是我第一次使用 APIS 返回结果 JSON 对象。我认为我已经得到了正确的异步任务代码,但我只是不知道如何执行它。这是我的类(class)代码。对于我的布局,我拥有的只是一个带有 onClick () 方法 gg 的按钮、一个进度条和一个 TextView 。
这是异步任务:

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 方法,但它给了我一个错误

Unknown type execute

我需要在执行方法中添加一些参数吗?
如果是这样怎么办?

谢谢。

最佳答案

尝试

new RetrieveFeedTask().execute(); 

关于java - Asynctask 未知类型执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36240405/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com