My application want to upload data to backend. Upload functionality available 2 places. One is after put invoice, it need to upload to backend without afftect other UI, that means through Serviceanother upload functionality will show all table to user if the user click on Upload button need to upload, through AsyncTask.
Now the problem is, if I call same upload table method then it thow can't create handler inside thread that has not called looper.prepare() this error.
06-14 09:53:52.504: W/System.err(2288): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
06-14 09:53:52.514: W/System.err(2288): at android.os.Handler.<init>(Handler.java:152)
06-14 09:53:52.514: W/System.err(2288): at android.app.Activity.<init>(Activity.java:714)
06-14 09:53:52.514: W/System.err(2288): at xont.ventura.controller.syn.UploadActivity.<init>(UploadActivity.java:43)
06-14 09:53:52.514: W/System.err(2288): at xont.ventura.controller.service.UploadService.uploadUsingService(UploadService.java:102)
06-14 09:53:52.514: W/System.err(2288): at xont.ventura.controller.service.UploadService$ServiceThread.run(UploadService.java:3872)
My code is :
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "Upload Service Started", Toast.LENGTH_LONG).show();
if(! APPURL.equals("")){
serviceThread = new ServiceThread();
serviceThread.start();
if(result.equals("The operation timed out")) {
Toast.makeText(this,"Service is not working OR Time out to Connect to service !!!!!!!",Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(this, "GPRS/WIFI is not available", Toast.LENGTH_LONG).show();
}
Log.d(TAG, "onStart");
}
private class ServiceThread extends Thread {
@Override
public void run() {
result = uploadUsingService();
}
};
public String uploadUsingService(){
uploadTable = getUploadTable();
String result = "";
try {
// dbAdapter.openDataBase();
if(uploadTable.size() > 0){
for (Map.Entry<Integer, String> entry : uploadTable.entrySet()) {
String value = entry.getValue();
if(value.equals("WMInvoiceHeader")){
result = new UploadActivity().getInvoiceHeader();
}else if(value.equals("WMInvoiceLine")){
result = new UploadActivity().getInvoiceLine();
}else if(value.equals("WMInvoiceHeaderDiscount")){
result = new UploadActivity().getInvoiceHeaderDiscount();
}else if(value.equals("WMInvoiceSpecialDiscount")){
result = new UploadActivity().getInvoiceSpecialDiscount();
}else if(value.equals("WMInvoiceCancelHeader")){
result = new UploadActivity().getInvoiceCancelHeader();
}else if(value.equals("WMInvoiceCancelLine")){
result = new UploadActivity().getInvoiceCancelLine();
}else if(value.equals("WMTransactionControl")){
result = new UploadActivity().getTransactionControl();
}else if(value.equals("WMVisitDetail")){
result = new UploadActivity().getVisitDetail();
}
if(result.equals("The operation timed out")) break;
}
}
//Toast.makeText(this, "Upload Service End", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
Here the problem is calling new UploadActivity().functionname If i write this function inside the same class. it doesn’t have any issue. like getInvoiceCancelLine() .If i write like this then 2 places have to write upload functionality method. How can i reusable same method 2 place.
Please help me out from this issuse?
Thanks in advance.
UI Thread(Event thread) is not thread safe, so you cant do any operation on UI into another thread.
By seeing your code it seems you are doing some UI stuff into method
you can invoke this method outside of another thread, and then pass the result to ServiceThread.