Reputation: 45
I have implemented AsyncTask to make http requests to a webService but it gives me an error when I try to get the compiler string returned by the server.
Here's the code of my activity where I think the AsyncTask:
public String consultaPreguntaBBDD(int num, String tab)
{
HttpAsyncTask httpAsyncTask = new HttpAsyncTask(String.valueOf(num),tab);
/*(!)*/String resul = httpAsyncTask.execute("http://appdomain.hol.es/webService.php");
return resul;
}
Where i've wrote this (!) the compiler says: Type mismatch: cannot convert from AsyncTask to String and this is the AsyncTask class:
class HttpAsyncTask extends AsyncTask<String, Void, String>
{
private static String id;
private static String te;
public HttpAsyncTask(String id,String te)
{
this.id = id;
this.te = te;
}
@Override
protected String doInBackground(String... urls)
{
return POST(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result)
{
}
public static String POST(String url)
{
InputStream inputStream = null;
String result = "";
try
{
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
// pass parameters in this way
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", id));
nameValuePairs.add(new BasicNameValuePair("te", te));
//add data
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// 10. convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e)
{
Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return result
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException {
// TODO Auto-generated method stub
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
Log.i("http","result: "+result+"\n");
return result;
}
}
i have two files, one for each class can anyone help me? thank's
Upvotes: 0
Views: 157
Reputation: 877
The problem is that:
httpAsyncTask.execute()
returns an instance of itself, which is an AsyncTask
class. This cannot be converted to a String
, which is why you get the error.
.execute
does not return the result of running your AsyncTask
- it merely starts it. To get the result, you need to access it where it becomes available, once the worker thread has completed its task.
How you should solve it is to fully implement:
protected void onPostExecute(String result)
in your AsyncTask
class. The parameter to this function is the result that you are trying to get in the beginning from .execute
. Here you can safely access it when the result is ready and you can do with it here what you want (call an appropriate function to handle the result, update a textview etc). This is the generel idea behind asynchronous method calls. You don't have the result right away, but when it becomes available.
Upvotes: 3
Reputation: 563
edit some code like this
public String consultaPreguntaBBDD(int num, String tab)
{
HttpAsyncTask httpAsyncTask = new HttpAsyncTask(String.valueOf(num),tab);
Object resul = httpAsyncTask.execute("http://appdomain.hol.es/webService.php");
return resul.toString();
}
httpAsyncTask.execute("http://appdomain.hol.es/webService.php"); is returning a object so change resul to objext type and then convert it into string
Upvotes: -1