Change your code as for showing loading bar using AsyncTask
private class Getdataasynktask extends AsyncTask<String, Void, String> { ProgressDialog progressDialog = new ProgressDialog(context); @Override protected void onPostExecute(String result) { // show loaging bar here progressDialog.setMessage("Loading..."); progressDialog.setCancelable(false); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setProgress(0); // set percentage completed to 0% progressDialog.show(); } @Override protected String doInBackground(String... params) { String strdata= getdatafromserver(); return strdata; } @Override protected void onPreExecute(String result) { objwebview.loadData(result.toString(), "text/html", "UTF-8"); // dismiss progress bar here progressDialog.dismiss(); } public String getdatafromserver(){ String line; objwebview.loadData(str.toString(), "text/html", "UTF-8"); HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet("http://www.google.com"); try { HttpResponse response = client.execute(request); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuilder str = new StringBuilder(); while((line = reader.readLine()) != null) { str.append(line); } } catch(Exception e) { e.printStackTrace(); line=e.toString(); } return line; } }
for executing this AsyncTask from UI Thread use
new Getdataasynktask().execute("");
and you can also use onProgressUpdate and publishProgress for showing loading bar using AsyncTask and publishing results to Ui thread . for more info see
http://developer.android.com/reference/android/os/AsyncTask.html