Android AsyncTask中执行的方法不适用于字符串
我尝试像这样执行Android AsyncTask中执行的方法不适用于字符串,android,android-asynctask,Android,Android Asynctask,我尝试像这样执行AsyncTask private static final String REQUESTED_URL = "//my url"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.earthquake_activity); EarthQuakeAsyncTas
AsyncTask
private static final String REQUESTED_URL = "//my url";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.earthquake_activity);
EarthQuakeAsyncTask task = new EarthQuakeAsyncTask();
task.execute(REQUESTED_URL); //this is where the error is
}
但Android Studio表示,它无法解析方法execute(String)
。我有一个来自Udacity
的教程,它们的示例非常相似
/** URL for earthquake data from the USGS dataset */
private static final String USGS_REQUEST_URL =
"//url";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EarthquakeAsyncTask task = new EarthquakeAsyncTask();
task.execute(USGS_REQUEST_URL); //it works
}
有人能告诉我为什么会发生这种情况吗
编辑:这是我的地震异步任务
类:
private class EarthQuakeAsyncTask extends AsyncTask<URL,Void,ArrayList<EarthQuake>> {
@Override
protected ArrayList<EarthQuake> doInBackground(URL... urls) {
if(urls.length==0||urls[0]== null){
return null;
}
// Create URL object
URL url = createUrl(REQUESTED_URL);
try {
jsonResponse = makeHttpRequest(url);
} catch (IOException e) {
// TODO Handle the IOException
}
ArrayList<EarthQuake> earthquake = QueryUtils.extractEarthquakes(jsonResponse);
return earthquake;
}
@Override
protected void onPostExecute(ArrayList<EarthQuake> earthquake) {
if (earthquake == null) {
return;
}
updateUi();
}
private URL createUrl(String stringUrl) {
URL url;
try {
url = new URL(stringUrl);
} catch (MalformedURLException exception) {
Log.e(LOG_TAG, "Error with creating URL", exception);
return null;
}
return url;
}
private String makeHttpRequest(URL url) throws IOException {
// If the URL is null, then return early.
if (url == null) {
return jsonResponse;
}
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000 /* milliseconds */);
urlConnection.setConnectTimeout(15000 /* milliseconds */);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// If the request was successful (response code 200),
// then read the input stream and parse the response.
if (urlConnection.getResponseCode() == 200) {
inputStream = urlConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
} else {
Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
}
} catch (IOException e) {
Log.e(LOG_TAG, "Problem retrieving the earthquake JSON results.", e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputStream != null) {
inputStream.close();
}
}
return jsonResponse;
}
private String readFromStream(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
}
}
私有类EarthQuakeAsyncTask扩展了AsyncTask{
@凌驾
受保护的ArrayList doInBackground(URL…URL){
if(url.length==0 | | url[0]==null){
返回null;
}
//创建URL对象
URL URL=createUrl(请求的URL);
试一试{
jsonResponse=makeHttpRequest(url);
}捕获(IOE异常){
//TODO处理IOException
}
ArrayList地震=QueryUtils.地震(jsonResponse);
回归地震;
}
@凌驾
执行后受保护的空隙(ArrayList地震){
如果(地震==null){
返回;
}
updateUi();
}
私有URL createUrl(字符串stringUrl){
网址;
试一试{
url=新url(stringUrl);
}捕获(畸形异常异常){
Log.e(Log_标记,“创建URL时出错”,异常);
返回null;
}
返回url;
}
私有字符串makeHttpRequest(URL)引发IOException{
//如果URL为空,则提前返回。
如果(url==null){
返回jsonResponse;
}
HttpURLConnection-urlConnection=null;
InputStream InputStream=null;
试一试{
urlConnection=(HttpURLConnection)url.openConnection();
setReadTimeout(10000/*毫秒*/);
setConnectTimeout(15000/*毫秒*/);
urlConnection.setRequestMethod(“GET”);
urlConnection.connect();
//如果请求成功(响应代码200),
//然后读取输入流并解析响应。
if(urlConnection.getResponseCode()==200){
inputStream=urlConnection.getInputStream();
jsonResponse=readFromStream(inputStream);
}否则{
e(Log_标记,“错误响应代码:”+urlConnection.getResponseCode());
}
}捕获(IOE异常){
e(Log_标签,“检索地震JSON结果时出现问题”,e);
}最后{
if(urlConnection!=null){
urlConnection.disconnect();
}
如果(inputStream!=null){
inputStream.close();
}
}
返回jsonResponse;
}
私有字符串readFromStream(InputStream InputStream)引发IOException{
StringBuilder输出=新的StringBuilder();
如果(inputStream!=null){
InputStreamReader InputStreamReader=新的InputStreamReader(inputStream,Charset.forName(“UTF-8”);
BufferedReader reader=新的BufferedReader(inputStreamReader);
字符串行=reader.readLine();
while(行!=null){
输出。追加(行);
line=reader.readLine();
}
}
返回output.toString();
}
}
}这是因为您的AsyncTask类的定义方式不适合使用字符串参数处理execute方法。让我解释一下 您开发的AsyncTask类如下所示:
private class MyAsyncTask extends AsyncTask<TYPE1, TYPE2, TYPE3> {
protected TYPE3 doInBackground(TYPE1... type1_variables) {
// Do some long process here..
return variable_of_type_TYPE3;
}
protected void onPostExecute(TYPE3 result) {
// Do something here
}
}
private class EarthQuakeAsyncTask extends AsyncTask<String, Void, Void> {
...
}
您的类签名表明您希望URL类型作为参数,但在execute()方法中传递的是字符串类型。您所需要做的就是更改您的类签名,使其预期的字符串与此代码中的字符串相同
private class EarthQuakeAsyncTask extends AsyncTask<String,Void,ArrayList<EarthQuake>> {
@Override
protected ArrayList<EarthQuake> doInBackground(String... urls) {
if(urls.length==0||urls[0]== null){
return null;
}
// Create a URL object from the String passed to the execute method
URL url = createUrl(urls[0]);
try {
jsonResponse = makeHttpRequest(url);
} catch (IOException e) {
// TODO Handle the IOException
}
ArrayList<EarthQuake> earthquake = QueryUtils.extractEarthquakes(jsonResponse);
return earthquake;
}
@Override
protected void onPostExecute(ArrayList<EarthQuake> earthquake) {
if (earthquake == null) {
return;
}
updateUi();
}
private URL createUrl(String stringUrl) {
URL url;
try {
url = new URL(stringUrl);
} catch (MalformedURLException exception) {
Log.e(LOG_TAG, "Error with creating URL", exception);
return null;
}
return url;
}
private String makeHttpRequest(URL url) throws IOException {
// If the URL is null, then return early.
if (url == null) {
return jsonResponse;
}
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000 /* milliseconds */);
urlConnection.setConnectTimeout(15000 /* milliseconds */);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// If the request was successful (response code 200),
// then read the input stream and parse the response.
if (urlConnection.getResponseCode() == 200) {
inputStream = urlConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
} else {
Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
}
} catch (IOException e) {
Log.e(LOG_TAG, "Problem retrieving the earthquake JSON results.", e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputStream != null) {
inputStream.close();
}
}
return jsonResponse;
}
private String readFromStream(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
}
}
}
私有类EarthQuakeAsyncTask扩展了AsyncTask{
@凌驾
受保护的ArrayList doInBackground(字符串…URL){
if(url.length==0 | | url[0]==null){
返回null;
}
//从传递给execute方法的字符串创建URL对象
URL URL=createUrl(URL[0]);
试一试{
jsonResponse=makeHttpRequest(url);
}捕获(IOE异常){
//TODO处理IOException
}
ArrayList地震=QueryUtils.地震(jsonResponse);
回归地震;
}
@凌驾
执行后受保护的空隙(ArrayList地震){
如果(地震==null){
返回;
}
updateUi();
}
私有URL createUrl(字符串stringUrl){
网址;
试一试{
url=新url(stringUrl);
}捕获(畸形异常异常){
Log.e(Log_标记,“创建URL时出错”,异常);
返回null;
}
返回url;
}
私有字符串makeHttpRequest(URL)引发IOException{
//如果URL为空,则提前返回。
如果(url==null){
返回jsonResponse;
}
HttpURLConnection-urlConnection=null;
InputStream InputStream=null;
试一试{
urlConnection=(HttpURLConnection)url.openConnection();
setReadTimeout(10000/*毫秒*/);
setConnectTimeout(15000/*毫秒*/);
urlConnection.setRequestMethod(“GET”);
urlConnection.connect();
//如果请求成功(响应代码200),
//然后读取输入流并解析响应。
if(urlConnection.getResponseCode()==200){
inputStream=urlConnection.getInputStream();
jsonResponse=readFromStream(inputStream);