Reputation: 566
I've made a class extending AsyncTask
to send POST requests to my server, from my application. It has been working up until now. I haven't changed anything, but all of a sudden it is freezing the UI thread and its own thread when I try to execute it.
I've tried using Log.d()
to find out where it gets u to before freezing, and it turns out that it freezes right after doInBackground()
is called.
I've also tried changing this...
String result = new AsyncPost().execute(params).get();
To this...
AsyncTask task = new AsyncPost();
task.execute();
String result = task.get();
It is nothing to do with the parameters I'm giving it, since it freezes no matter what parameters are passed to it.
I have also tried using Postman to check that my server is working properly (and it is), so it is not a server-side issue either.
This is my AsyncTask
class. Sorry for the excessve amount of code, but I can't figure out which specific part of it is bad, since it was all working up until now.
public class AsyncPost extends AsyncTask<Object, Integer, String> {
@Override
protected String doInBackground(Object ... params){
//android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_MORE_FAVORABLE);
Object urlObj = params[0];
Object argsObj = params[1];
String urlStr = (String) urlObj;
Map<String, String> args = (Map<String, String>) argsObj;
URL url;
HttpsURLConnection http = null;
byte[] out = null;
int len = 0;
Log.d("POST", "STARTED");
try {
url = new URL(urlStr);
http = (HttpsURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setDoOutput(true);
StringJoiner sj = new StringJoiner("&");
for (Map.Entry<String, String> entry : args.entrySet()) {
sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "="
+ URLEncoder.encode(entry.getValue(), "UTF-8"));
}
out = sj.toString().getBytes(StandardCharsets.UTF_8);
len = out.length;
http.setFixedLengthStreamingMode(len);
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
http.connect();
Log.d("POST", "CONNECTED");
}
catch (Exception e){
e.printStackTrace();
return null;
}
try (OutputStream os = http.getOutputStream()){
os.write(out);
}
catch (Exception e){
e.printStackTrace();
return null;
}
try {
Log.d("POST_RESPONSECODE", Integer.toString(http.getResponseCode()));
BufferedReader responseReader = new BufferedReader(new InputStreamReader(http.getInputStream()));
StringBuilder responseBuilder = new StringBuilder();
String response;
while ((response = responseReader.readLine()) != null){
responseBuilder.append(response);
}
Log.d("POST", responseBuilder.toString());
return responseBuilder.toString();
}
catch (Exception e){
e.printStackTrace();
return null;
}
finally{
http.disconnect();
}
}
@Override
public void onProgressUpdate(Integer ... progress){
Log.d("POST", "PROGRESS = " + Integer.toString(progress[0]));
}
}
What should I do to fix this problem?
Should I be using AsyncTask
at all for this task. I've tried using Thread
, but that freezes my UI too.
I've looked at other questions, but they all seem to be doing this...
Object result = new AsyncTask().get();
which is what is causing their problems, which doesn't apply to me.
Upvotes: 0
Views: 73
Reputation: 756
If you don't want to wait for the result on the main thread, you need to use an interface that is used by your AsyncPost
class in your overriden onPostExecute
method. Don't forget to make the receiving class implement it:
interface AsyncPostListener {
void continueWith(String result);
}
@Override
public void continueWith(String results) {
//continue your logic here
}
public class AsyncPost extends AsyncTask<Object, Integer, String> {
private AsyncPostListener listener;
....
public AsyncPost(AsyncPostListener listener){
this.listener = listener;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(listener != null)
listener.continueWith(s);
}
}
AsyncPost ap = new AsyncPost(this);
Upvotes: 1
Reputation: 46
Calling get() on AsyncTask will basically block the execution on the that thread, in your case the main thread. Instead of that what you want to do is implement in your AsyncTask onPostExecute() method (https://developer.android.com/reference/android/os/AsyncTask.html#onPostExecute(Result))
Upvotes: 2