Method Execute In Asynctask Does Not Work With String
I try to execute an AsyncTask like this private static final String REQUESTED_URL = '//my url'; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(
Solution 1:
Your class signature suggests that you are expecting a URL type as parameter, but you are passing a String type in the execute() method. All you need to do is to change your class signature to expect a String as in the one in this code.
privateclassEarthQuakeAsyncTaskextendsAsyncTask<String,Void,ArrayList<EarthQuake>> {
@Overrideprotected ArrayList<EarthQuake> doInBackground(String... urls) {
if(urls.length==0||urls[0]== null){
returnnull;
}
// Create a URL object from the String passed to the execute methodURLurl= createUrl(urls[0]);
try {
jsonResponse = makeHttpRequest(url);
} catch (IOException e) {
// TODO Handle the IOException
}
ArrayList<EarthQuake> earthquake = QueryUtils.extractEarthquakes(jsonResponse);
return earthquake;
}
@OverrideprotectedvoidonPostExecute(ArrayList<EarthQuake> earthquake) {
if (earthquake == null) {
return;
}
updateUi();
}
private URL createUrl(String stringUrl) {
URL url;
try {
url = newURL(stringUrl);
} catch (MalformedURLException exception) {
Log.e(LOG_TAG, "Error with creating URL", exception);
returnnull;
}
return url;
}
private String makeHttpRequest(URL url)throws IOException {
// If the URL is null, then return early.if (url == null) {
return jsonResponse;
}
HttpURLConnectionurlConnection=null;
InputStreaminputStream=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 {
StringBuilderoutput=newStringBuilder();
if (inputStream != null) {
InputStreamReaderinputStreamReader=newInputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReaderreader=newBufferedReader(inputStreamReader);
Stringline= reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
}
}
}
Solution 2:
That is because your AsyncTask class isn't defined in a manner to handle the execute method with a String parameter. Let me explain myself.
The AsyncTask class you develop will look like this:
privateclassMyAsyncTaskextendsAsyncTask<TYPE1, TYPE2, TYPE3> {
protectedTYPE3doInBackground(TYPE1... type1_variables) {
// Do some long process here..return variable_of_type_TYPE3;
}
protectedvoidonPostExecute(TYPE3 result) {
// Do something here
}
}
So for you to call task.execute(REQUESTED_URL);
you'd need to implement your AsyncTask class correctly.
For example it might look like this:
privateclassEarthQuakeAsyncTaskextendsAsyncTask<String, Void, Void> {
...
}
Post a Comment for "Method Execute In Asynctask Does Not Work With String"