Reputation: 143
I am a beginner to Android and Java. Not doing too bad so far but I have stumbled upon an issue that I can't wrap my head around.
I am trying to create a method in my application class that will do http calls with a value pair list passed to it. Here is the first part. This is in a activity that is activated by a button click.
// Add your data to array
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("action", "2"));
nameValuePairs.add(new BasicNameValuePair("cell", "2500270025"));
nameValuePairs.add(new BasicNameValuePair("date", "blah"));
nameValuePairs.add(new BasicNameValuePair("time", "AndDev is Cool!"));
nameValuePairs.add(new BasicNameValuePair("reason", "2"));
// need to call the request
String result = ((RespondApp) getApplication()).makeHTTPCall(nameValuePairs);
Once I get to the app here is the receiving part.
public String makeHTTPCall(List<NameValuePair> nameValuePairs) {
// this will be used to make all http requests from the whole app
new postToHttp().execute(nameValuePairs);
return null;
}
Here is the AsyncTask part.
class postToHttp extends AsyncTask<List<NameValuePair>, Void, String> {
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// I am sure I need something here just don't know what
}
@Override
protected String doInBackground(List<NameValuePair>... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.evfd.ca/cell.php");
try {
httppost.setEntity(new UrlEncodedFormEntity(params[0]));
Log.i("makeHttpCall", "done ecoding");
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String result = convertStreamToString(instream);
Log.i("Read from server", result);
return result;
}
} catch (ClientProtocolException e) {
return null;
} catch (IOException e) {
return null;
}
return null;
}
I am trying to get the response sent back by the webserver all the way back to the activity page that called this process. So ideally I want to just load up the value pairs make the call and get the http response back, and then continue on my merry way.
How can I do this?
Upvotes: 2
Views: 436
Reputation: 14153
You can work with the String
you downloaded on the main UI thread right from the AsyncTask
itself. Simply override protected void onPostExecute(String result)
in your AsyncTask
class and do the work there. This function will get called with whatever value you return from doInBackground()
.
Basically, do whatever the next step is inside of onPostExecute()
. See How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class? for a few ideas:
Nest the AsyncTask
class as an inner class within your Activity
so it can work with your Activity's variables, methods, etc.
Create an interface
that your Activity implements. Basically, the task would call something like onHttpTaskComplete(String result)
in the Activity.
Upvotes: 2