Android Asynctask Method That I Dont Know How To Solve
Solution 1:
I don't think you fully understand the full concept of AsyncTasks. You use these when you want to run an operation in a background thread and this is a very nice/flexible way of accomplishing this task. What is really nice to me is onPostExecute()
executes on the main thread, so it can really do some powerful things once your work is completed in doInBackground()
. You should keep in mind though that because onPostExecute()
does execute on the main thread, you do not want to perform any networking operations here.
Here is a simple example of an AsyncTask:
privateclassmyAsyncTaskextendsAsyncTask<String, Void, Boolean> {
@OverrideprotectedvoidonPreExecute() {
// before we start working
}
@OverrideprotectedBooleandoInBackground(String... args) {
//do work in the backgroundreturntrue;
}
@OverrideprotectedvoidonPostExecute(Boolean success) {
// the work is done.. now what?
}
}
doInBackground()
is where you are going to be doing the bulk of your work, so I will try to help you out with the basic structure you want. I just copied and pasted your code where I thought it should go so this is not 100% gauranteed, but hopefully it will help kick off what you want to do:
privateclassJSONParserextendsAsyncTask<String, Void, JSONObject> {
staticInputStreamis=null;
staticJSONObjectjObj=null;
staticStringjson="";
// variables passed in:
String url;
String method;
List<NameValuePair> params;
// constructorpublicJSONParser(String url, String method,
List<NameValuePair> params) {
this.url = url;
this.method = method;
this.params = params;
}
@Overrideprotected JSONObject doInBackground(String... args) {
try {
if(method == "POST"){
DefaultHttpClienthttpClient=newDefaultHttpClient();
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,
CookiePolicy.BROWSER_COMPATIBILITY);
HttpPosthttpPost=newHttpPost(url);
httpPost.setEntity(newUrlEncodedFormEntity(params));
HttpResponsehttpResponse= httpClient.execute(httpPost);
HttpEntityhttpEntity= httpResponse.getEntity();
is = httpEntity.getContent();
} elseif(method == "GET"){
DefaultHttpClienthttpClient=newDefaultHttpClient();
StringparamString= URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGethttpGet=newHttpGet(url);
HttpResponsehttpResponse= httpClient.execute(httpGet);
HttpEntityhttpEntity= httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReaderreader=newBufferedReader(newInputStreamReader(
is, "iso-8859-1"), 8);
StringBuildersb=newStringBuilder();
Stringline=null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Fehler!", "Fehler mein umwandeln von Stream in String: " + e.toString());
}
try {
jObj = newJSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
@OverrideprotectedvoidonPostExecute(JSONObject obj) {
// Now we have your JSONObject, play around with it.
}
}
Edit:
I forgot to mention that you can also pass in args
which is a string array. You can just create args and pass it in when you call your AsyncTask:
new JSONParser(url, method, params).execute(args);
and you can access args in doInBackground()
Here is some more information on AyncTask: http://developer.android.com/reference/android/os/AsyncTask.html
Post a Comment for "Android Asynctask Method That I Dont Know How To Solve"