Android : adding progressBar for web service data android

on Sunday, September 7, 2014


I have a web service and I am getting data from webservice so ı have waiting time.To solve waiting , I prefer add progress dialog.When I added the progress bar , the project didnt work.Here is my codes :



Runnable runnable = new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
setInfo();
}

};
//Toast.makeText(CardChooseActivity.this, adapter.getItem(position), Toast.LENGTH_SHORT).show();
(new Thread(runnable)).start();
}


I call the setInfo function and here is my setInfo function:



private void setInfo()
{
final ProgressDialog ringProgressDialog = ProgressDialog.show(ReportAllActivity.this, "Please wait ...", "Downloading Infos...", true);
ringProgressDialog.setCancelable(true);
new Thread(new Runnable() {
@Override
public void run() {
try {
String successMessage="";
jsonObject = new JSONObject();

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,10000);
HttpConnectionParams.setSoTimeout(httpParams,10000);
try {
jsonObject.put("data", "data1");
jsonObject.put("data1", "data2");
jsonObject.put("data2", "data3");
jsonObject.put("data3", "data4");

StringEntity stringEntity = new StringEntity(jsonObject.toString());
stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(stringEntity);

CookieStore cookieStore = new BasicCookieStore();
cookieStore.addCookie(cookie);
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpResponse httpResponse = httpClient.execute(httpPost,localContext);
HttpEntity entity = httpResponse.getEntity();


if (entity != null) {
successMessage = EntityUtils.toString(httpResponse.getEntity());
Log.i("Report All activity", successMessage);
}
jArray = new JSONArray(successMessage);

runOnUiThread(new Runnable() {
public void run() {

fillTable(jArray);

}
});


}catch (IOException e){
Log.e("Login_Issue", e.toString());
}catch (JSONException e) {
e.printStackTrace();
}
} catch (Exception e) {

}
ringProgressDialog.dismiss();
}
}).start();


And my logcat errors:



09-07 10:37:17.156: E/AndroidRuntime(510): FATAL EXCEPTION: Thread-13
09-07 10:37:17.156: E/AndroidRuntime(510): java.lang.RuntimeException: Can't create handler
inside thread that has not called Looper.prepare()
09-07 10:37:17.156: E/AndroidRuntime(510): at android.os.Handler.<init>(Handler.java:121)
09-07 10:37:17.156: E/AndroidRuntime(510): at android.app.Dialog.<init>(Dialog.java:101)
09-07 10:37:17.156: E/AndroidRuntime(510): at android.app.AlertDialog.<init>
(AlertDialog.java:63)
09-07 10:37:17.156: E/AndroidRuntime(510): at android.app.ProgressDialog.<init>
(ProgressDialog.java:80)
09-07 10:37:17.156: E/AndroidRuntime(510): at android.app.ProgressDialog.<init>
(ProgressDialog.java:76)
09-07 10:37:17.156: E/AndroidRuntime(510): at
android.app.ProgressDialog.show(ProgressDialog.java:101)
09-07 10:37:17.156: E/AndroidRuntime(510): at
android.app.ProgressDialog.show(ProgressDialog.java:90)
09-07 10:37:17.156: E/AndroidRuntime(510): at
com.example.example.ReportAllActivity.setInfo(ReportAllActivity.java:119)
09-07 10:37:17.156: E/AndroidRuntime(510): at
com.example.example.ReportAllActivity.access$0(ReportAllActivity.java:117)
09-07 10:37:17.156: E/AndroidRuntime(510): at
com.example.example.ReportAllActivity$1.run(ReportAllActivity.java:109)
09-07 10:37:17.156: E/AndroidRuntime(510): at java.lang.Thread.run(Thread.java:1096)

0 comments:

Post a Comment