-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.

A.A.
  • 402
  • 1
  • 4
  • 19
  • Where do you call that try block? In which function? Outside onCreate() ? Your onCreate() is a mess. – greenapps Nov 03 '17 at 20:37
  • I suppose I should've prefaced by saying I'm new and trying to learn Android development, so if it's a mess, that's why. Usually I try to get things to work and then clean up the code after. – A.A. Nov 06 '17 at 13:14
  • Maybe you have to add urlConnection.connect(); where it hangs. – greenapps Nov 06 '17 at 18:36
  • Tried that, and when I do that, urlConnection.connect() itself hangs. – A.A. Nov 06 '17 at 18:39
  • Does it hang or does it time out? – greenapps Nov 06 '17 at 18:40
  • It seems to hang as I don't get any timeout error even when leaving it to run for a minute. – A.A. Nov 06 '17 at 18:41
  • I tried your url in a browser. Please try for yourself. Dns lookup fails. – greenapps Nov 06 '17 at 18:42
  • The URL works for me but I'm inside my work's domain. Seems like this might be a network security issue interfering with my request? I will make sure my mobile device that's making the request is on our internal network. – A.A. Nov 06 '17 at 18:46
  • Just try other urls too. – greenapps Nov 06 '17 at 18:56
  • Confirmed, was security issue. Does not hang when making request to https://httpbin.org/headers. – A.A. Nov 07 '17 at 13:37

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,154
  • 2
  • 16
  • 19
  • Can I do String[] projects = asyncRequest.execute(url); and expect the onPostExecute to pass the result into projects? – A.A. Nov 06 '17 at 13:17
  • Thanks, I did all that but I still have the core problem of the HttpURLConnection request hanging. – A.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. – A.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. – A.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. – A.A. Nov 06 '17 at 18:37