Quantcast
Channel: How can I fix 'android.os.NetworkOnMainThreadException'? - Stack Overflow
Viewing all articles
Browse latest Browse all 191

Answer by Gurpreet singh for How can I fix 'android.os.NetworkOnMainThreadException'?

$
0
0

Use the below code to perform heavy tasks.

// Your package hereimport java.util.List;import org.apache.http.NameValuePair;import android.app.Activity;import android.app.ProgressDialog;import android.content.Context;import android.os.AsyncTask;import android.view.View.OnSystemUiVisibilityChangeListener;public class AsyncRequest extends AsyncTask<String, Integer, String> {    Context context;    ProgressDialog pDialog;    // Three Constructors    public AsyncRequest(Activity a, String m, List<NameValuePair> p) {        context = a;        method = m;        parameters = p;    }    public AsyncRequest(Activity a) {        this.caller = (OnAsyncRequestComplete) a;        context = a;    }    public String doInBackground(String... urls) {        //Perform your task here        return result;    }    public void onPreExecute() {        pDialog = new ProgressDialog(context);        pDialog.setMessage("Please wait..");        pDialog.setCancelable(false);        pDialog.show();    }    public void onProgressUpdate(Integer... progress) {        // You can implement some progressBar and update it in this record.        //   setProgressPercent(progress[0]);    }    public void onPostExecute(String response) {        if (pDialog != null && pDialog.isShowing()) {            pDialog.dismiss();        }        // Get the result here    }    protected void onCancelled(String response) {        if (pDialog != null && pDialog.isShowing()) {            pDialog.dismiss();        }    }}

Viewing all articles
Browse latest Browse all 191

Latest Images

Trending Articles



Latest Images