I have a class that makes async requests and parses the returned jason (http://developer.android.com/reference/android/os/AsyncTask.html#onPreExecute%28%29)
public class JSONparser extends AsyncTask<NameValuePair, Void, JSONObject>{
// Response from the HTTP Request
static InputStream httpResponseStream = null;
// JSON Response String to create JSON Object
static String jsonString = "";
@Override
protected JSONObject doInBackground(NameValuePair... pairs) {
try {
// get a Http client
List<NameValuePair> params = new ArrayList<NameValuePair>();
String method = pairs[0].getValue();
String url = pairs[1].getValue();
for(int i=2; i < pairs.length; i++){
params.add(pairs[i]);
}
DefaultHttpClient httpClient = new DefaultHttpClient();
// If required HTTP method is POST
if (method == "POST") {
// Create a Http POST object
HttpPost httpPost = new HttpPost(url);
// Encode the passed parameters into the Http request
httpPost.setEntity(new UrlEncodedFormEntity(params));
// Execute the request and fetch Http response
HttpResponse httpResponse = httpClient.execute(httpPost);
// Extract the result from the response
HttpEntity httpEntity = httpResponse.getEntity();
// Open the result as an input stream for parsing
httpResponseStream = httpEntity.getContent();
}
// Else if it is GET
else if (method == "GET") {
// Format the parameters correctly for HTTP transmission
String paramString = URLEncodedUtils.format(params, "utf-8");
// Add parameters to url in GET format
url += "?" + paramString;
// Execute the request
HttpGet httpGet = new HttpGet(url);
// Execute the request and fetch Http response
HttpResponse httpResponse = httpClient.execute(httpGet);
// Extract the result from the response
HttpEntity httpEntity = httpResponse.getEntity();
// Open the result as an input stream for parsing
httpResponseStream = httpEntity.getContent();
}
// Catch Possible Exceptions
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
// Create buffered reader for the httpResponceStream
BufferedReader httpResponseReader = new BufferedReader(
new InputStreamReader(httpResponseStream, "iso-8859-1"), 8);
// String to hold current line from httpResponseReader
String line = null;
// Clear jsonString
jsonString = "";
// While there is still more response to read
while ((line = httpResponseReader.readLine()) != null) {
// Add line to jsonString
jsonString += (line + "\n");
}
// Close Response Stream
httpResponseStream.close();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
// Create jsonObject from the jsonString and return it
return new JSONObject(jsonString);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
// Return null if in error
return null;
}
}
}
and what I'd like to do is to know how I can change onPreExecute() and onPostExecute() (from the hyperlink above:
onPreExecute(), invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface. doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step. onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field. onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.
accordingly to what I need to do in a certain part of the code called like this
**** code ****
//treat onPreExecute here
//treat onPostExecute here
JSONObject returned = new JSONparser().execute(method,httpurl,"").get(); //commit