Reputation: 194
I've tried everything, but nothings works.
I'm trying to call an Api, which works. However, onPostExecute() doesn't get called which makes that my callback doesn't work.
This is my AsyncTask class:
class RetrieveInput extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream stream = connection.getInputStream();
Scanner s = new Scanner(stream).useDelimiter("\\A");
String result = s.hasNext() ? s.next() : "";
return result;
} catch (IOException ex) {
Log.d("Error", "Error fetching data");
Log.d("STACKTRACE", ex.getStackTrace().toString());
}
return null;
}
@Override
protected void onPostExecute(String result) {
repo.setString(result);
}
}
This is where the AsyncTask gets called:
public void getString() {
if (url != null) {
RetrieveInput input = new RetrieveInput();
input.execute(url);
}
}
This is where my callback is located:
public class UserRepository implements IRepository {
private Connection connection;
private String result;
private Gson gson;
public UserRepository() {
this.connection = new Connection(this);
this.gson = new Gson();
}
public User find(String userName) {
connection.setUrl("http://10.0.2.2:8080/api-0.1.0/users/get/" + userName);
connection.getString();
User user = gson.fromJson(this.result, User.class);
return user;
}
@Override
public void setString(String result) {
this.result = result;
}
}
Thanks in advance!
Upvotes: 0
Views: 259
Reputation: 1954
Maybe you have another exception in doInBackground()
Method add this catch block to your try catch :
catch (Exception ex) {
ex.getStackTrace()
}
and also check your log and make sure your log line is not printed in logcat
so you will have this:
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream stream = connection.getInputStream();
Scanner s = new Scanner(stream).useDelimiter("\\A");
String result = s.hasNext() ? s.next() : "";
return result;
}catch (IOException ex) {
Log.d("Error", "Error fetching data");
Log.d("STACKTRACE", ex.getStackTrace().toString());
} catch (Exception ex) {
ex.getStackTrace()
}
Upvotes: 0
Reputation: 7905
From the official android developers site:
There are a few threading rules that must be followed for this class to work properly:
- The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN.
- The task instance must be created on the UI thread.
- execute(Params...) must be invoked on the UI thread.
- Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually. The task can be executed only once (an exception will be thrown if a second execution is attempted.)
I would emphasize on the 3rd bullet. execute
must be invoked on the UI thread. In your code it shows that execute
is called from a getString()
method but it is not clear if this method exists in a class belonging to the UI thread e.g. an activity. I suppose there is no UI thread in which the onPostExecute
method can be executed.
Upvotes: 1
Reputation: 4132
There might be an exception in your network call that is returned and hence null is sent as the result.Also you are returning result in try and again sending null as result at the end of the method which gets overridden.So alter your code like this
String dataResult = null;
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream stream = connection.getInputStream();
Scanner s = new Scanner(stream).useDelimiter("\\A");
String result = s.hasNext() ? s.next() : "";
dataResult = result;
} catch (IOException ex) {
Log.d("Error", "Error fetching data");
Log.d("STACKTRACE", ex.getStackTrace().toString());
} finally {
return dataResult;
}
Upvotes: 0