In my android application, I copy some data from server database to local database.It may take over 15 minutes. For slower connections, it may be exceeded. So I want to display a waiting progress bar.
The code is posted below.
refresh.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
final ProgressDialog progressDialog = ProgressDialog.show(login.this, "", "Please wait...");
new Thread() {
public void run() {
try{
mySQLiteAdapter.openToWrite();
mySQLiteAdapter.deleteAll();
radapter.openToWrite();
radapter.deleteAll();
uadapter.openToWrite();
uadapter.deleteAll();
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxxxxxxxxxxxxxxxxxxxx");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
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();
json = sb.toString();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
}
try {
jObj = new JSONObject(json);
contacts = jObj.getJSONArray("get");
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
if(!c.isNull("rname"))// || c.getString("rcode").equals(null))
{
rname = c.getString("rname");
rcode = c.getString("rcode");
radapter.insert(rcode, rname);
}
else
{
rname = "";
rcode = c.getString("rcode");
radapter.insert(rcode, rname);
}
}
Toast.makeText(getBaseContext(), " Ryot BackUp completed", Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
Toast.makeText(getBaseContext(), "Error"+e.toString(), Toast.LENGTH_LONG).show();
}
progressDialog.dismiss();
}
}.start();
adapter.deleteAll();
oadapter.deleteAll();
e2.setText("Back Up completed");
}
catch (Exception e) {
Log.e("tag", e.getMessage());
}
progressDialog.dismiss();
}
}.start();
}
});
When I'm running my application, the progress bar is displayed only for few seconds and the Catch block is executed, showing logcat error as
Logcat:
02-26 14:26:07.151: E/tag(301): Can't create handler inside thread that has not called Looper.prepare()
Can someone explain what is the mistake in my code and how to solve it
Toast.makeText(getBaseContext(), " Ryot BackUp completed", Toast.LENGTH_SHORT).show();
The problem is caused by the above code, this method will change UI, you can't update UI in a non-ui thread. So you need to call it from main ui thread.
Since it is a time-consuming work, I highly recommend you to use AsyncTask in Android.
But if you only want to make a little change in your existing code, try to use
context.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(...).show();
}
});
or handler, you can take a look at How Handler Work in Android . A basic example:
public Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
//show toast here
}
}
protected void startLongRunningOperation() {
Thread t = new Thread() {
public void run() {
//do something here
//update the result
mHandler.postDelayed(mUpdateResults, 200);}
mHandler.post(mUpdateResults);
}
};
t.start();
}
Instead of creating a new Thread make a HandlerThread - http://developer.android.com/reference/android/os/HandlerThread.html
Just replace "new Thread()" with "new HandlerThread("any string")" and it should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.