Android throw NetworkOnMainThreadException [duplicate] - android

This question already has answers here:
Android - android.os.NetworkOnMainThreadException
(10 answers)
Closed 8 years ago.
I have an asyntask
public class AsynNetworkOperation extends AsyncTask<String,Void,Void>{
private Context context = null;
private ProgressDialog dialog = null;
private String title = "";
private WebRequest request = null;
private String accessMethod = "";
private HttpResponse response = null;
AsynResponse delegate = null;
public AsynNetworkOperation(Context context, String method, String dialogTitle)
{
this.context = context;
accessMethod = method;
this.title = dialogTitle;
delegate = (AsynResponse) context;
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog = new ProgressDialog(context);
dialog.setMessage(title);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
#Override
protected Void doInBackground(String... data) {
// TODO Auto-generated method stub
request = new WebRequest(context);
if(accessMethod.equals(ServiceUri.AccessMethod.GET)){
response = request.makeHttpGetCall(data[0]);
}
return null;
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
delegate.responseResult(response);
//dispose();
}
private void dispose()
{
context = null;
dialog = null;
title = "";
request = null;
accessMethod = "";
delegate = null;
}
}
and interface
public interface AsynResponse {
public void responseResult(HttpResponse response);
}
and then I have an SqliteHelper class
//constructor
public SQLLiteDbHelper(Context context,int dbVersion) {
super(context,DATABASE_NAME, null, dbVersion);
this.context = context;
Log.d("tag","db version is "+DATABASE_VERSION);
crypt = new Cryptography();
utils = new Utils(context);
}
#Override
public void onCreate(final SQLiteDatabase db) {
String s;
try {
new AsynNetworkOperation(context, ServiceUri.AccessMethod.GET, "loading").execute(ServiceUri.SERVICE_URI+"s?d=abc");
} catch (Throwable t) {
Toast.makeText(context, t.toString(), Toast.LENGTH_LONG).show();
Log.d("tag",t.toString());
}
}
and a MainActivity class
public class MainActivity extends ListActivity implements AsynResponse{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchText = (EditText) findViewById(R.id.searchText);
utils = new Utils(MainActivity.this);
db=(new SQLLiteDbHelper(MainActivity.this,utils.getInt(Key.Db_version))).getReadableDatabase();
request = new WebRequest(this);
status = new AsynGetEmployeeStatus();
}
public void responseResult(HttpResponse response){
HttpEntity et = response.getEntity();
String strr = utils.getResponseBody(et); //it throw exception network on main thread
}
}
and get responsebody code is
public String getResponseBody(final HttpEntity entity) throws Exception {
InputStream instream = entity.getContent();
if (instream == null) {
return null;
}
if (entity.getContentLength() > Integer.MAX_VALUE) {
return null;
}
StringBuilder buffer = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, HTTP.UTF_8));
String line = null;
try {
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
}
finally {
instream.close();
reader.close();
line=null;
}
return buffer.toString();
}
it is working fine on emulator and but it is throwing network on main thread exception on device.I dont know why it is throwing exception.There are other network opeartion also which use the same asyntask but that work fine on device. only in this case it is throwing exception. Please help me finding the problem.
thanks

when it goes into getResponseBody method, it crashes at while ((line = reader.readLine()) != null) line
Apparently, you are calling getResponseBody() on the main application thread. Do that work in doInBackground() of your AsyncTask.
I am not getting why it is throwing exception while i am reading the content.because i have already the http response.
No, you do not. You started the HTTP request. You have not finished the HTTP request until you close the InputStream you get from the HttpEntity. That InputStream is reading off of the socket that represents the HTTP connection.

add StrictMode:
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

Related

JSoup parsed elements empty

I am trying to parse some data using JSoup, this is all happening in a asynctask (doInBackground) part of my MainActivity.
Unfortunately all the elements (9) are empty when I execute the app.
When I debug below codeline, I actually get the complete website, it`s all there.
The method readMultipleLinesRespone() is located in another class HttpUtility where I also call my Post and Get requests.
I tested this upfront by saving the website as a file and using JSoups assets ability, it worked perfectly then.
The setupAdapter() method in onPostExecute fills a ExpandableListview with data, should this info be nessecary. If you need more info pls ask.
Can somebody assist and tell me what I am doing wrong?
response1 = util.readMultipleLinesRespone(); <--- debugged and all data (seems) to be there but isn`t.
Edit: If I print response1, there is indeed no data to parse.
Logcat output:
E/Resonse:: [Ljava.lang.String;#3d3410a
Below is the method readMultipleLinesRespone from HttpUtility class:
public String[] readMultipleLinesRespone() throws IOException {
InputStream inputStream = null;
if (httpConn != null) {
inputStream = httpConn.getInputStream();
} else {
throw new IOException("Connection is not established.");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream));
List<String> response = new ArrayList<String>();
String line = "";
while ((line = reader.readLine()) != null) {
response.add(line);
}
reader.close();
return (String[]) response.toArray(new String[0]);
}
The asynctask where it`s all hapening:
private class FetchWebsiteData extends AsyncTask<Void, Void, Void> {
ProgressDialog mProgressDialog;
#Override
protected void onPreExecute() {
this.mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setMessage("Laden...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
#Override
protected Void doInBackground(Void... result) {
try {
util.sendGetRequest("https://mobile.somesite.nl/Data", null);
response1 = util.readMultipleLinesRespone();
} catch (IOException e) {
e.printStackTrace();
}
if (response1.length > 0) {
Document doc = Jsoup.parse(response1.toString());
// Get the html document title
Elements els = doc.select("span[class=item-value pull-right]");
if (els.size() > 0) {
fac_naam = els.get(0).text();
fac_straat = els.get(1).text();
fac_post = els.get(2).text();
con_tel = els.get(3).text();
con_email = els.get(4).text();
betaal_reknr = els.get(5).text();
betaal_houd = els.get(6).text();
zig_gebruiker = els.get(7).text();
zig_wacht = els.get(8).text();
}
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPreExecute();
setupAdapter();
mProgressDialog.dismiss();
}
}
In the meantime I solved the problem.
I did not pass the response string correctly to the asynctask which parses the required elements.
Just required a public string in which the response is being set and passed (not an elegant way but it works):
public static String HttpResponse = "";
In the HttpUtility class:
public String[] readMultipleLinesRespone() throws IOException {
...
TabFragment1.HttpResponse = response.toString();
...
return (String[]) response.toArray(new String[0]);
}
Then pass it to the asynctask:
#Override
protected Void doInBackground(Void... result) {
try {
util.sendGetRequest(LoginActivity.PersData_URL, null);
util.readMultipleLinesRespone();
} catch (IOException e) {
e.printStackTrace();
}
if (HttpResponse.length() > 0) {
Document doc = Jsoup.parse(HttpResponse.toString());
// Get the html document title
Elements els = doc.select("span[class=item-value pull-right]");
...
}
return null;
}

AsyncTask to fetch global game scores from a server

I am new to android and am completely puzzled by AsyncTasks. I need to create a leaderboard which will pull global leaderboard scores from a server.
I have posted below the two methods that were created in the LeaderboardsFragment which are used to access and display the scores - getGlobalScores and readStream.
I am unsure of how to use these in the AsyncTask - mostly how and what parameters to pass to the AsyncTask - most of the tutorials I have been looking at do not deal with 2D arrays. Any hints would be really appreciated, I am really having trouble understanding the literature surrounding this.
package uk.ni.appidemic.whackamole;
import java.io.BufferedReader;
public class LeaderboardsFragment extends Fragment {
AssetStore AS;
private TextView TopScores;
private String[][] global_scores = new String[10][3];
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_leaderboards, container, false);
//Go and get the asset store from the activity
AS = WhackAMoleActivity.getAssetManager();
TopScores = (TextView) rootView.findViewById(R.id.leaderboards);
// Extract and display the top score text view from the preferences
displayLocalScores();
// this method is used to send a highscore to the server (name and score)
// this method may get pulled out to the gameloop as its the only place it should be used in the final game
// but this can be used for testing purposes atm (Server needs to be on)
// sendScoreGlobal("porter", 1001);
//async Get global scores from the server and display them - new thread
new AsyncOperation().execute();
...................
public void getGlobalScores() {
//gets global score in HTML format to be parsed
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
URL url = new URL("http://62........./high_scores");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
readStream(con.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
/gets the data and stores the global scores in a 2d array
//it then displays to screen
public void readStream(InputStream in) {
BufferedReader reader = null;
try {
StringBuilder htmlIn = new StringBuilder();
StringBuilder globalScoreBuilder = new StringBuilder();
htmlIn.append("");
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
htmlIn.append(line);
}
// String to be scanned to find the pattern.
String html = htmlIn.toString();
String regexPattern = "<td align=\"left\" style=\"padding-left:10px;\">(\\d+?)</td>|<td align=\"right\" style=\"padding-right:10px;\">(\\w+?)</td>";
// Create a Pattern object
Pattern patternObject = Pattern.compile(regexPattern);
// Now create matcher object.
Matcher matcherObject = patternObject.matcher(html);
Log.d(getActivity().getResources().getString(R.string.LOG_TAG), "Trying to find regex matches");
TopScores.append("\n");
int nextFreePointer = 0;
int rowCount = 0;
while (matcherObject.find()) {
if (matcherObject.group(1) != null) {
Log.d(getActivity().getResources().getString(R.string.LOG_TAG), "Regex match : " + matcherObject.group(1));
globalScoreBuilder.append(matcherObject.group(1) + " ");
global_scores[rowCount][nextFreePointer] = matcherObject.group(1);
nextFreePointer++;
}
if (matcherObject.group(2) != null) {
Log.d(getActivity().getResources().getString(R.string.LOG_TAG), "Regex match : " + matcherObject.group(2));
globalScoreBuilder.append(matcherObject.group(2) + " ");
global_scores[rowCount][nextFreePointer] = matcherObject.group(2);
nextFreePointer++;
}
if (nextFreePointer > 2) {
nextFreePointer = 0;
rowCount++;
}
globalScoreBuilder.append("\n");
}
StringBuilder sb = new StringBuilder();
String lineSeparator = System.getProperty("line.separator");
for (String[] row : global_scores) {
sb.append(Arrays.toString(row)).append(lineSeparator);
}
String text = sb.toString();
TopScores.append("Global Top 10 Scores\n");
TopScores.append(text);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class AsyncOperation extends AsyncTask<String, Void, Void>{
protected void onPreExecute(){
}//end of onPreExecute
#Override
protected Void doInBackground(Void... values) {
}//doinBackground
protected void onProgressUpdate(Void... values){
}//onProgressUpdate
protected void onPostExecute(Void... result){
}//end of onPostExecute
}//end of AsyncOperation inner class
}//end of Leaderboards class
You should fetch your game score through a WebService class that extentds AsynTask. Below is my class that I am using in order to fetch remote data safely.
CODE:
public class WebServiceRestTask extends AsyncTask<HttpUriRequest, Void, Object> {
private static final String TAG = "WebServiceRestTask";
private AbstractHttpClient mClient;
private WeakReference<WebServiceRestCallback> mCallback;
private int ws_task;
public WebServiceRestTask(int ws_task) {
this(new DefaultHttpClient(), ws_task);
}
public WebServiceRestTask(AbstractHttpClient client, int task_number) {
mClient = client;
this.ws_task = task_number;
}
public interface WebServiceRestCallback {
public void onRequestSuccess(String response);
public void onRequestError(Exception error);
}
public void setResponseCallback(WebServiceRestCallback callback) {
mCallback = new WeakReference<WebServiceRestCallback>(callback);
}
#Override
protected Object doInBackground(HttpUriRequest... params) {
try {
HttpUriRequest request = params[0];
HttpResponse serverResponse = mClient.execute(request);
BasicResponseHandler handler = new BasicResponseHandler();
String response = handler.handleResponse(serverResponse);
return response + ws_task;
} catch (Exception e) {
Log.w(TAG, e);
return e;
}
}
#Override
protected void onPostExecute(Object result) {
if (mCallback != null && mCallback.get() != null) {
if (result instanceof String) {
mCallback.get().onRequestSuccess((String) result);
} else if (result instanceof Exception) {
mCallback.get().onRequestError((Exception) result);
} else {
mCallback.get().onRequestError(
new IOException("Unknown Error Contacting Host"));
}
}
}
}
Not at my workstation but think something like this should work.
public class AsyncOperation extends AsyncTask<String, Void, Void>{
private String[][] global_scores = new String[10][3];
protected void onPreExecute(){
// optionally show loading indicator
TopScores.append("\n");
}//end of onPreExecute
#Override
protected Void doInBackground(Void... values) {
try {
URL url = new URL("http://62........./high_scores");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
readStream(con.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
}//doinBackground
protected void onProgressUpdate(Void... values){
}//onProgressUpdate
protected void onPostExecute(Void... result){
// optionally hide loading indicator
StringBuilder sb = new StringBuilder();
String lineSeparator = System.getProperty("line.separator");
for (String[] row : global_scores) {
sb.append(Arrays.toString(row)).append(lineSeparator);
}
String text = sb.toString();
TopScores.append("Global Top 10 Scores\n");
TopScores.append(text);
}//end of onPostExecute
private void readStream(InputStream in) {
BufferedReader reader = null;
try {
StringBuilder htmlIn = new StringBuilder();
StringBuilder globalScoreBuilder = new StringBuilder();
htmlIn.append("");
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
htmlIn.append(line);
}
// String to be scanned to find the pattern.
String html = htmlIn.toString();
String regexPattern = "<td align=\"left\" style=\"padding-left:10px;\">(\\d+?)</td>|<td align=\"right\" style=\"padding-right:10px;\">(\\w+?)</td>";
// Create a Pattern object
Pattern patternObject = Pattern.compile(regexPattern);
// Now create matcher object.
Matcher matcherObject = patternObject.matcher(html);
Log.d(getActivity().getResources().getString(R.string.LOG_TAG), "Trying to find regex matches");
int nextFreePointer = 0;
int rowCount = 0;
while (matcherObject.find()) {
if (matcherObject.group(1) != null) {
Log.d(getActivity().getResources().getString(R.string.LOG_TAG), "Regex match : " + matcherObject.group(1));
globalScoreBuilder.append(matcherObject.group(1) + " ");
global_scores[rowCount][nextFreePointer] = matcherObject.group(1);
nextFreePointer++;
}
if (matcherObject.group(2) != null) {
Log.d(getActivity().getResources().getString(R.string.LOG_TAG), "Regex match : " + matcherObject.group(2));
globalScoreBuilder.append(matcherObject.group(2) + " ");
global_scores[rowCount][nextFreePointer] = matcherObject.group(2);
nextFreePointer++;
}
if (nextFreePointer > 2) {
nextFreePointer = 0;
rowCount++;
}
globalScoreBuilder.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}//end of AsyncOperation inner class

Android: ProgressDialog in external async class (sth block async thread)

I have main activity:
public class ChooseWriteSentenceActivity extends ActionBarActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String userName = "Zdzisiu";
String password = "Ziemniak";
MainServie service = new MainService(this);
boolean isExsist = service.findUser(String userName, String password);
//more code...
}
}
In my app service uses repositories and jsonconsumers but for simpler code I'm skipping them.
public class MyService{
private Context context;
public MyService(Context context){
this.context = context
}
public boolean findUser(String userName, String password){
String resultS = null;
try{
resultS = new QueryExecutorFindUser(context).execute(userName,password).get();
}
catch(Exception ex){
ex.printStackTrace();
}
boolean realRes = jsonConsumer(resultS).getFindUser();
return realRes;
}
}
public class QueryExecutorFindUser extends AsyncTask<String,Void,String> {
protected final String connectionUrl = "http://myWebService:44302/Service.svc/";
protected ProgressDialog progressDialog;
protected Context curContext;
public QueryExecutor(Context context){
curContext = context;
}
#Override
protected void onPreExecute()
{
super.onPreExecute();
progressDialog = ProgressDialog.show(curContext,"Loading...",
"Loading application View, please wait...", false, false);
}
#Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
progressDialog.dismiss();
}
protected String doInBackground(String... args){
String result = null;
String url = connectionUrl + args[0] + "/" + args[1];
HttpResponse response = null;
HttpClient httpclient = this.getNewHttpClient();
HttpGet get = new HttpGet(url);
get.setHeader("Accept", "application/json");
get.setHeader("Content-type", "application/json");
try{
response = httpclient.execute(get);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
if(response != null){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
result = out.toString();
}
} else{
throw new IOException(statusLine.getReasonPhrase());
}
} catch(Exception ex){
ex.getMessage();
} finally{
if(response != null){
try{
response.getEntity().getContent().close();
} catch(Exception ex){
}
}
}
return result;
}
}
And progress dialog is show but only after all code in onCreatre in ChooseWriteSentenceActivity including doInBacground(...) from QueryExecutor is finished (so it disappears practically at the same time). It looks like sth waiting for thread with QueryExecutorFindUser.doInBackground() and it is runs like synchronously (?), I think that because when I debug code onPreExecute() is running correctly (and start before doInBackground(...)) and progressDialog.isShowing() == true (but not on the screen :( ).
If I remove extends AsyncTask from QueryExecutorFindUser and make private class with this extension in main activity (and run all code from onCreated() including service.findUser() in thisPrivateClass.doInBackground(...)) it works okey.
I prefer to have progressDialog in one place no in all main activities (of cource in practise I use QueryExecutor for all queries not only findUser) but I don't have idea what i am doing wrong. I spent all day on it with no result :(
Dialogs are tied to an Activity and ultimately must be hosted by one. So until your app's activity gets created, the dialog will not display.

Need to access AsyncTask value in Main Class

I am facing an issue in Async task, can anyone please suggest me any solution.
I have downloaded this example from this link :
Source
My Current Structure is
Main Class extends MyTask and implements AsyncTaskCompleteListener interface.
AsyncTaskCompleteListener is an Interface contains the onTaskComplete Method .
MyTask extends Async Task and onPostExcute contains CallBackMethod which will return the result-set got from the doInBackground.
Http Class(Utils) contains the Http connection and returns the Result-set to AsyncTaskComleteListner from PostExecute.
I am trying to get my result-set Value in the main class from the interface method to perform my further operation.
I tried to get the value from static variables, static method but non of them worked, and also tried with creating a new class object to send and receive the result but every time it gives me NullPointerException . Because the statement written after the AsyncTask gets executes before getting the result.
I have also tried to get the Status of asyncTask from its method getStaus(), but it returns only Running and dose not notify when the task is completed or finished.
Here is the code sample:
Main Class Code :
package com.example.androidasynctask;
public class MainActivity extends Activity implements AsyncTaskCompleteListener {
public static String[] asyncResult;
String res[] = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnclick(View view) {
/*MyTask asyncTask = new MyTask(this);
String [] asyncTaskResult = asyncTask.execute("fetchCategory.php","1%Id%1");*/
//AsyncTask<String, Void, String[]> asyncTaskRes = new MyTask(this).execute("fetchCategory.php","1%Id%1");
//new MyTask(this).execute("fetchCategory.php","1%Id%1");
MyTask asyncTask = (MyTask) new MyTask(this).execute("fetchCategory.php","1%Id%1");
if(asyncTask.getStatus().equals(AsyncTask.Status.FINISHED) || asyncTask.getStatus().equals(AsyncTask.Status.PENDING)) {
asyncTask.execute();
}
else {
Log.v("In Else","Get Value");
}
}
#Override
public void onTaskComplete(String[] result) {
Log.v("IN ON TASK COMPLETE","VALUE = "+result[1]);
}
/*#Override
public void onTaskComplete(String result) {
System.out.println("calling onTaskComplete SIMPLE....");
System.out.println("result :: "+ result);
}*/
public static class GetAsyncResult
{
static String[] returnValues;
public GetAsyncResult()
{}
public GetAsyncResult(String[] res)
{
returnValues = res;
Log.v("getResultSetValues","returnValues"+returnValues[1]);
}
public void getResultSetValues()
{
Log.v("getResultSetValues","returnValues"+returnValues[1]);
}
}
}
Async Task Code :
public class MyTask extends AsyncTask<String, Void, String[]> {
private Activity activity;
private ProgressDialog dialog;
private AsyncTaskCompleteListener callback;
public String[] asyncResultSetValue = null;
public MyTask(Activity act) {
Log.v("MY TASK","ACTIVITY"+act);
this.activity = act;
this.callback = (AsyncTaskCompleteListener)act;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
Log.v("MY TASK","in ON PRE EXECUTE");
dialog = new ProgressDialog(activity);
dialog.setMessage("Loading...");
dialog.show();
}
#Override
protected String[] doInBackground(String... params) {
Log.v("MY TASK","DO IN BACKGROUND");
Log.v("PARAMS"," params[0] = "+params[0]+ "| params[1]"+params[1]);
asyncResultSetValue = Utils.process_query(params[0],params[1]);
return asyncResultSetValue;
}
#Override
protected void onPostExecute(String[] result) {
super.onPostExecute(result);
Log.v("MY TASK","in ON POST EXECUTE");
if (null != dialog && dialog.isShowing()) {
dialog.dismiss();
}
callback.onTaskComplete(result);
}
}
HTTP CLASS CODE :
public class Utils {
static String result = null;
String endResult;
static java.io.InputStream is = null;
static StringBuilder sb=null;
static String delimiter = "\\|";
static String delimiter1 = "\\%";
static String[] temp = null;
static String[] temp1 = null;
static ArrayList<NameValuePair> nameValuePairs;
static Context context;
static ProgressDialog mDialog;
static HttpResponse response;
static String[] resultset_value = null;
//static String url = "http://fortuneworkinprogress.in/News_App/"; //Global URL
static String url = "http://10.0.2.2/News_App/"; //Global URL
static String query_type,parameter;
/*************** PROCESS QUERY START ***************/
public static String[] process_query(String str_url, String parameter) {
// String strval = select_parameter;
String ret_val[] = null;
String get_sel_val[] = null;
int loopcount =0;
url = url+str_url; //!!!! ######### CONCATINATING AND CREATING FULL URL ######## !!!!!!//
Log.v("PROCESS QUERY PARAMETER","URL = "+url+" | PARAMTER = "+parameter);
nameValuePairs = new ArrayList<NameValuePair>();
//Log.i("STR VAL",""+strval); //To Check which values are recieved
try
{
String strval = parameter;
get_sel_val=strval.split(delimiter1);
for(int i =0; i < get_sel_val.length ; i++)
{
loopcount = Integer.parseInt(get_sel_val[0]); // First Delimeted Value which tells the number of count
Log.i("Loopcount","cnt = "+loopcount);
}
for(int j=1;j<=(loopcount*2);j=j+2) //For Loop for making Name Values Pares Dynamic
{
nameValuePairs.add(new BasicNameValuePair(get_sel_val[j],get_sel_val[j+1]));
//Log.i("J = ["+j+"]","pairvalue1 = "+get_sel_val[j]+"pairvalue2 ="+get_sel_val[j+1]);
}
}
catch(Exception e)
{
Log.w("Exception in the getting value","Exp = "+e);
}
//nameValuePairs.add(new BasicNameValuePair("id","1"));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
Log.v("CONNECT URL ","Final url "+url);
Log.w("CONNECTION STATUS ",httppost.toString());
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.w("PAERSE VALUE ",nameValuePairs.toString());
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.w("1", "Connection establised succesfuly");
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
Log.v("SB VALUE = ","sb = "+sb.toString());
String line="0";
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result=sb.toString();
// Toast.makeText(getBaseContext(), result ,Toast.LENGTH_LONG).show();
Log.w("result", result);
}
catch(Exception e)
{
Log.e("log_tag", "Error converting result "+e.toString());
Toast.makeText(null, "error converting response to string" ,Toast.LENGTH_LONG).show();
}
String[] temp = null;
String[] tempResult = null;
if(result!=null)
{
tempResult = result.split(delimiter); //Split the entire return string into "rows"
for(int i =0; i < tempResult.length-1 ; i++)
{
temp = null;
temp = tempResult[i].split(delimiter1); //Find columns for each row
ret_val = temp;
resultset_value=ret_val;
}
}
else
{
Toast.makeText(null, "Cannot Find Routes" ,Toast.LENGTH_LONG).show();
}
Log.v("BEFORE RETUNR = ","ret_val = "+ret_val.toString());
return ret_val; //Returning the result value array
}
/*************** PROCESS QUERY ENDS ***************/
public static boolean isNetworkAvailable(Activity activity)
{
ConnectivityManager connectivity = (ConnectivityManager) activity
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null)
{
return false;
}
else
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
{
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
}
}
return false;
}
}
Thanks in advance.
Because the statement written after the AsyncTask gets executes before getting the result.
the reason is AsyncTask runs on separate thread,not on your Main(UI) thread.
MyTask extends Async Task and onPostExcute contains CallBackMethod which will return the result-set got from the doInBackground.
you will be getting result values on this method
#Override
public void onTaskComplete(String[] result) {
Log.v("IN ON TASK COMPLETE","VALUE = "+result[1]);
}
Comment following piece of code,
if(asyncTask.getStatus().equals(AsyncTask.Status.FINISHED) || asyncTask.getStatus().equals(AsyncTask.Status.PENDING)) {
asyncTask.execute();
}
else {
Log.v("In Else","Get Value");
}
Make change,
public static String[] asyncResult; to public String[] asyncResult = null;
Change following,
asyncResultSetValue = Utils.process_query(params[0],params[1]); to asyncResult = Utils.process_query(params[0],params[1]);
and return asyncResultSetValue; to return asyncResult ;
look at value by adding one more log,you will be getting result values on this method
#Override
public void onTaskComplete(String[] result) {
Log.v("IN ON TASK COMPLETE","VALUE = "+result[1]);
Log.v("IN ON TASK COMPLETE","VALUE = "+asyncResult[1]);
}

Can an Android AsyncTask doInBackground be synchronized to serialize the task execution?

Is it possible to make AsyncTask.doInBackground synchronized - or achieve the same result in another way?
class SynchronizedTask extends AsyncTask {
#Override
protected synchronized Integer doInBackground(Object... params) {
// do something that needs to be completed
// before another doInBackground can be called
}
}
In my case, any AsyncTask.execute() can be started before a previous one has completed, but I need to execute the code in doInBackground only after the previous task has finished.
EDIT: As correctly pointed out, the synchronization works only on the same object instance.
Unfortunately, it is not possible to create an AsyncTask and call execute() more than once on the same object instance, as specified in the "Threading rules" section of the AsyncTask documentation.
The solution is to use a custom Executor to serialize the tasks, or, if you use API 11 or above, AsyncTask.executeOnExecutor(), as suggested in the comments below.
I posted an answer showing an implementation of a SerialExecutor that can be used to queue tasks that will be executed sequentially.
Ideally, I'd like to be able to use AsyncTask.executeOnExecutor() with a SERIAL_EXECUTOR, but this is only available for API level 11 or above:
new AsyncTask().executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, params);
To target the Android APIs below level 11, I ended up implementing a custom class which encapsulates an ExecutorService with a thread pool size of 1. The full code is open-sourced here.
Executors.newFixedThreadPool(int nThreads) creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. In my case, nThreads is 1, which means tasks can be queued, but only one task will be executed at any given time.
Here is the code:
public abstract class SerialExecutor {
private final ExecutorService mExecutorService;
public SerialExecutor() {
mExecutorService = Executors.newFixedThreadPool(1);
}
public void queue(Context context, TaskParams params) {
mExecutorService.submit(new SerialTask(context, params));
}
public void stop() {
mExecutorService.shutdown();
}
public abstract void execute(TaskParams params);
public static abstract class TaskParams { }
private class SerialTask implements Runnable {
private final Context mContext;
private final TaskParams mParams;
public SerialTask(Context context, TaskParams params) {
mContext = context;
mParams = params;
}
public void run() {
execute(mParams);
Activity a = (Activity) mContext;
a.runOnUiThread(new OnPostExecute());
}
}
/**
* Used to notify the UI thread
*/
private class OnPostExecute implements Runnable {
public void run() {
}
}
}
This can be extended and used as a serial task executor in an Activity:
public class MyActivity extends Activity {
private MySerialExecutor mSerialExecutor;
#Override
public void onCreate(Bundle savedInstanceState) {
// ...
mSerialExecutor = new MySerialExecutor();
}
#Override
protected void onDestroy() {
if (mSerialExecutor != null) {
mSerialExecutor.stop();
}
super.onDestroy();
}
public void onTrigger(int param) {
mSerialExecutor.queue(this, new MySerialExecutor.MyParams(param));
}
private static class MySerialExecutor extends SerialExecutor {
public MySerialExecutor() {
super();
}
#Override
public void execute(TaskParams params) {
MyParams myParams = (MyParams) params;
// do something...
}
public static class MyParams extends TaskParams {
// ... params definition
public MyParams(int param) {
// ... params init
}
}
}
}
You may want to think about using IntentService instead. It seems like it may be a better fit for your process since it has built in features for queuing.
public class RestAsyncTask1 extends AsyncTask<String, Void, String> {
private AsyncTaskCompleteListener callback;
private Context context;
private String method;
private static final AtomicInteger PROGRESS_NUM = new AtomicInteger(0);
private static ProgressDialog PROGRESS_DIALOG;
public RestAsyncTask1(Context context, AsyncTaskCompleteListener callback, String method) {
this.callback = callback;
this.context = context;
this.method = method;
}
public static String format(String url, String... params) {
String[] encoded = new String[params.length];
for (int i = 0; i < params.length; i++) {
encoded[i] = Uri.encode(params[i]);
}
return String.format(url, (String[]) encoded);
}
#Override
protected void onPreExecute() {
int x = PROGRESS_NUM.getAndIncrement();
if (x == 0) {
String title = "M_yug";
PROGRESS_DIALOG = new ProgressDialog(context);
// PROGRESS_DIALOG.setTitle(title);
PROGRESS_DIALOG.setIndeterminate(true);
PROGRESS_DIALOG.setCancelable(false);
PROGRESS_DIALOG.setOnCancelListener(null);
PROGRESS_DIALOG.setMessage("Loading. Please wait...");
PROGRESS_DIALOG.show();
}
}
#Override
protected String doInBackground(String... params) {
String url = params[0];
String response = null;
HttpURLConnection connection = null;
if (params.length > 1) {
if (method.equals(Method.GET)) {
url = format(url, (String[]) Arrays.copyOfRange(params, 1, params.length));
} else if (params.length > 2) {
url = format(url, (String[]) Arrays.copyOfRange(params, 1, params.length - 1));
}
try {
URL call = new URL(url);
connection = (HttpURLConnection) call.openConnection();
connection.setRequestProperty("Content-Type", "application/json");
//connection.setRequestProperty("M-Yug", Utilities.VERSION);
connection.setRequestMethod(method);
connection.setDoOutput(true);
if (method.equals("POST")) {
BufferedOutputStream outputStream = new BufferedOutputStream(connection.getOutputStream());
outputStream.write(params[params.length - 1].getBytes());
outputStream.flush();
}
int status = connection.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
InputStream is = connection.getInputStream();
response = readValue(is);
} else if (status == 400) {
InputStream is = connection.getErrorStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
reader.close();
Toast.makeText(context, "" + builder.toString(), Toast.LENGTH_SHORT).show();
}
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
return response;
}
#Override
protected void onPostExecute(String s) {
int x = PROGRESS_NUM.decrementAndGet();
if (x == 0 && PROGRESS_DIALOG != null && PROGRESS_DIALOG.isShowing()) {
PROGRESS_DIALOG.dismiss();
}
if (s!=null) {
String resopnse=s.toString();
callback.onSuccess(resopnse);
} else {
Toast.makeText(context,"Server Not Responding",Toast.LENGTH_SHORT).show();
}
}
private String readValue(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
}
return sb.toString();
}
enum Method {
GET, POST
}
}
AsyncTask is used to run a background thread so that you current process is not interupted .
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
where first of all your doInBackground function iscalled and the returned object will move to on post execute.
which line of code you want to run after some process you can put that in PostExecute function.
this will surely help you

Categories

Resources