0

All I want to do is use AsyncTask to do a http request. This is my code so far but I am not sure how to call this class from my main activity and how to get the results back. I have a String var "uri" that completes the url for my http request that I must pass to my class. after calling this class the next step is to pass the results (as a string) to a new function.

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;

class RequestTask extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... uri) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null;
        try {
            response = httpclient.execute(new HttpGet("http://www.mywebsite.com/xml_data/" + uri + "_meeting_info.xml?id=cacheSTOP3"));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();
            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            //TODO Handle problems..
        } catch (IOException e) {
            //TODO Handle problems..
        }
        return responseString;
    }

    @Override
    protected void onPostExecute(String result) {
     onPostExecute(result);
        //Do anything with response..
    }
}

Main Activity:

RequestTask getXML = new RequestTask();
getXML.execute(MyVarString);


//Now send the results from above to a new function 
Document doc = XMLfunctions.XMLfromString(getdata);
Denoteone
  • 4,043
  • 21
  • 96
  • 150
  • Is there a reason why the question was down voted? Please explain so I make sure to be more clear in the future. – Denoteone Nov 29 '11 at 21:38
  • **super.onPostExecute(result);** is not needed for calling. – nostra13 Nov 29 '11 at 21:42
  • 2
    I did not down vote, but I would guess its because a) AsyncTasks are pretty common and with a little research you could have found your answer or b) There is already a common question on SO that you could have searched for to find your answer. Personally I don't mind but some people get touchy about these things – Joe Nov 29 '11 at 21:43
  • Thanks @Joe ... believe me I have been pulling my hair out all day lots of info out there but I have not been able to wrap my head around it yet. Thanks for the info. – Denoteone Nov 29 '11 at 21:46

3 Answers3

4

You should put your code

//Now send the results from above to a new function 
Document doc = XMLfunctions.XMLfromString(getdata);

somewhere at the end of onPostExecute(String result) method. It's only way to perform your function after async task was executed.

nostra13
  • 12,377
  • 3
  • 33
  • 43
  • +1 and checked That help so now there are no errors but my original problem still is there that I was hoping the AsyncTask would fix. http://stackoverflow.com/questions/8315928/android-function-is-not-completing/8316185#comment10249311_8316185 – Denoteone Nov 29 '11 at 21:36
1

You're basically correct for executing the request, except that since doInBackground() has a varargs format, the MyVarString that you passed should be accessed as uri[0] inside the task, as uri itself is an array of Strings.

One option for returning the value is to make your task a private inner class of the main Activity. That way you can execute all the result code directly in onPostExecute() and you will have access to all the Activity data.

If you prefer to keep the task as a separate class, then use onPostExecute() to send a Broadcast Intent or notify the Activity via a custom listener interface. The callback could include the result, or simply notify the Activity that it must call getResult() on the AsyncTask to get the same string that was passed to onPostExecute() as a parameter.

HTH

devunwired
  • 62,780
  • 12
  • 127
  • 139
0

The arguments are accessed like an array. So for your example it would be something like this:

response = httpclient.execute(new HttpGet("http://www.mywebsite.com/xml_data/" + uri[0] + "_meeting_info.xml?id=cacheSTOP3"));

Anything that needs to handle the result should go into the onPostExecute method. Whatever you return from doInBackground will be the argument into onPostExecute. So again in your example that would look like this:

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

    Document doc = XMLfunctions.XMLfromString(result);
    //Do anything with response..
}
Joe
  • 2,649
  • 21
  • 33