Thread.sleep(50);
You are calling sleep on the ui thread which blocks it. Remove it.
Use AsyncTask
instead of a Thread
.
http://developer.android.com/reference/android/os/AsyncTask.html
Show progress dialog in onPreExecute
. Do your http request in doInbackground
. Dismiss dialog in onPostExecute
and update ui accordingly.
To invoke
new TheTask().execute();
Then
class TheTask extends AsyncTask<Void, Void, Void> {
ProgressDialog pd;
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progress.dismiss();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progress = ProgressDialog.show(MainActivity.this, "dialog title",
"dialog message", true);
}
@Override
protected Void doInBackground(Void... arg0) {
// http request
return null;
}
}
Edit:
public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new TheTask().execute();
}
class TheTask extends AsyncTask<Void, Void, JSONObject> {
InputStream is = null;
String result = "";
JSONObject jArray = null;
ProgressDialog pd;
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
pd.dismiss();
// parse and set List adapter here
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = ProgressDialog.show(MainActivity.this, "dialog title",
"dialog message", true);
}
@Override
protected JSONObject doInBackground(Void... arg0) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("******");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}
}
Edit2:
class TheTask extends AsyncTask<Void, Void, JSONArray> {
InputStream is = null;
String result = "";
JSONArray jArray = null;
ProgressDialog pd;
@Override
protected void onPostExecute(JSONArray result) {
super.onPostExecute(result);
pd.dismiss();
ArrayList<String> list= new ArrayList<String>();
try
{
for(int i=0;i<result.length();i++)
{
JSONObject jb = result.getJSONObject(i) ;
String name = jb.getString("name");
list.add(name);
}
}catch(Exception e)
{
e.printStackTrace();
}
setListAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, list));
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = ProgressDialog.show(MainActivity.this, "dialog title",
"dialog message", true);
}
@Override
protected JSONArray doInBackground(Void... arg0) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("******");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONArray(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}
}