-1

The Problem

I have an AsyncTask task called from an Activity's OnCreate method. This task makes an http request. The HTTP request hangs. Once the "CODE HANGS HERE" code in the code below is executed, I observe in the debugger that the Async threads are perpetually 'running' and never return anything.

The Code

Here's the OnCreate method of the activity:

protected void onCreate(Bundle savedInstanceState) {
    asyncRequest.delegate = this;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activty_attach);
    Button retakeButton = (Button) (findViewById(R.id.retake_button));

    retakeButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(AttachActivity.this, MainActivity.class);
            startActivity(intent);
        }
    });
    try {
        URL url;
        url = new URL("http://btl-cromwell:9000/api/engine/v1/version");
        asyncRequest.execute(url);
    } catch (Exception e) {
        Log.e(logtag, e.toString());
    }
}

Note the URL that is passed to he async task should just return JSON containing the version number of the service receiving the request.

The async task (asyncRequest) code is below:

public class AsyncRequest extends AsyncTask<URL, Void, List<String>> {
    private String logtag = "AsyncRequestTask";
    public AsyncResponse delegate;
    List<String> projects = new ArrayList<String>();

    @Override
    protected void onPreExecute(){

        super.onPreExecute();
    }

    @Override
    protected List<String> doInBackground(URL... urls) {
            try {
            // Creating & connection Connection with url and required Header.
            HttpURLConnection urlConnection = (HttpURLConnection) urls[0].openConnection();
            urlConnection.setRequestProperty("Content-Type", "application/json");
            urlConnection.setRequestMethod("GET");   //POST or GET
            urlConnection.setRequestProperty("User-Agent", "Test");
            // CODE HANGS HERE
            int responseCode = urlConnection.getResponseCode();
            String responseMessage = urlConnection.getResponseMessage();
            projects.add(responseMessage);
            } catch (Exception e) {
                Log.e(logtag, e.toString());
            }
            return projects;
        }


    @Override
    protected void onPostExecute(List<String> result){
        delegate.processFinish(result);
    }
}

Once I have the request working I will populate the projects variable with what I actually want to return but for now I just have it set to responseMessage. I'm sure this is just something to do with my unfamiliarity in making requests in Java, but I have spent days on this and can't figure it out. Any help is greatly appreciated.

Amr A.
  • 372
  • 1
  • 3
  • 14

1 Answers1

0
         asyncRequest.execute(url);
            asyncRequest.getStatus();
            String[] projects = asyncRequest.get();

It is not possible to do both an .execute and a .get().

As you should never use .get(), you better remove that statement.

Remove all code after asyncRequest.execute(url); and put that code in the onPostExecute of your AsyncTask.

greenapps
  • 11,036
  • 2
  • 14
  • 19
  • Can I do String[] projects = asyncRequest.execute(url); and expect the onPostExecute to pass the result into projects? – Amr A. Nov 06 '17 at 13:17
  • Thanks, I did all that but I still have the core problem of the HttpURLConnection request hanging. – Amr A. Nov 06 '17 at 18:04
  • Then please edit your post and show the code you use now. – greenapps Nov 06 '17 at 18:11
  • Okay, I edited the above, hopefully it is more clear now. – Amr A. Nov 06 '17 at 18:29
  • I asked you to move code to onPostExecute. I see nothing there. Ok a new call. What makes you think it hangs? – greenapps Nov 06 '17 at 18:33
  • I use the delegate/listener thing to pass the result of the AsyncTask to the Activity itself, as described here: https://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a because I want to use these to populate the AutoCompleteTextView. – Amr A. Nov 06 '17 at 18:35
  • I believe it hangs because when I run it in debugger, in Android Studio, the code 'getResponseCode' never returns a value. – Amr A. Nov 06 '17 at 18:37