0

I use AsnycTask to connect URL and parse the return xml:

class Connecting extends AsyncTask<String, String, String> {
    private String URLPath = "";
    private HttpURLConnection Connection;
    private InputStream InputStream;
    private boolean Return1 = false;
    private int Return2 = -1;

    public Connecting (String fn, String u) {
        FileName = fn;
    URLPath = u;
        Connection = null;
    InputStream = null;

    Return1 = false;
    Return2 = -1;

    execute();
    }

    public boolean getReturn1() {
    return Return1;
    }

    public int getReturn2() {
    return Return2;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... aurl) {
        try {
            URL url = new URL(URLPath);
            Connection = (HttpURLConnection)url.openConnection();
            Connection.setConnectTimeout(10000);
            Connection.setReadTimeout(10000);
            Connection.setDoInput(true);
            Connection.setUseCaches(false);
            Connection.connect();
            InputStream = Connection.getInputStream();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String unused) {
        super.onPostExecute(unused);

        try {
            InputStreamReader fsr = new InputStreamReader(InputStream);

            BufferedReader br = new BufferedReader(fsr);
            String line = "";
            while((line = br.readLine()) != null) {
                //parse Reture1 and Return2
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        Connection = null;
    }
}

And I use below code to call it:

Connecting con = new Connecting(Name, URL);
System.out.println("Return1 " + con.getReturn1());
System.out.println("Return2 " + con.getReturn2());

It will get false and -1, which the init value.
And connect URL after print message.
I want to get the value which has connect success and parse from the xml.
How can I do it?

1

2 Answers 2

1

AsyncTask is a class that helps to run in background. You can use it if you want to access to remote server using for example HTTP connection. In doBackground method you have to the the "heavy" task, the one that requires time and could block the UI. When you finish at the end of doBackground you have to return the value that is the result of the task. Then in the onPostExecute you use this result to update for example the UI. In your case it seems to me you aren't using correctly the AsyncTask. First of all you return null in doBackground and dont set return1 and return2 as you should. And in onPostExecute you read the response while yuo should do it in doBackground. There's another method you can override called onPreExecute that is called before doBackground method.

In my blog i've an example how to use AsyncBackground in this case and it could help you. If you like give a look here

1

The AsyncTask runs (as the name says) asynchronously to the main-thread. If you want to happen something after the task is done, you have to put that code in the onPostExecute() method. So you may put the System.out there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.