Android-使用AsyncTask加载进度
我无法在应用程序上显示加载进度条。我试着找了几天,但仍然没有任何进展。有人能帮我查看我的代码并给我任何意见吗。我的问题是,当我打开页面时,它会直接在eclipse上显示错误并迫使我关闭。在“我的调试”选项卡中打开的错误页-“ThreadPoolExecutor.java” 请任何人帮帮我Android-使用AsyncTask加载进度,android,android-asynctask,android-progressbar,Android,Android Asynctask,Android Progressbar,我无法在应用程序上显示加载进度条。我试着找了几天,但仍然没有任何进展。有人能帮我查看我的代码并给我任何意见吗。我的问题是,当我打开页面时,它会直接在eclipse上显示错误并迫使我关闭。在“我的调试”选项卡中打开的错误页-“ThreadPoolExecutor.java” 请任何人帮帮我 class DownloadMsg extends AsyncTask<Void, String, Void> { protected void onPreExecute() {
class DownloadMsg extends AsyncTask<Void, String, Void> {
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(ViewMsgMain.this);
progressDialog.setMessage("Loading. Please wait...");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.show();
}
protected Void doInBackground(Void... params) {
try{
//http post
HttpClient httpclient = new DefaultHttpClient();
//===================================================
// Get member id from local database
sp_memberid = getSharedPreferences(memberid, MODE_PRIVATE);
if(sp_memberid.contains("memberid")==true)
{memid = sp_memberid.getInt("memberid", 0);}
Log.e("view message member id == ", "~ "+memid);
//===================================================
HttpPost httppost = new HttpPost("http://localhost/project/viewmessage.php?memberid="+memid);
//httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); //encode a list of NameValuePair objects suitable for HTTP calls
HttpResponse response = httpclient.execute(httppost); // to make an HTTPPOST call with the HttpClient
HttpEntity entity = response.getEntity();
inputstream = entity.getContent();
//Convert response to string
BufferedReader reader = new BufferedReader(new InputStreamReader(inputstream,"UTF-8"));
stringbuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
stringbuilder.append(line + "\n");
}
inputstream.close();
result = stringbuilder.toString();
}
catch(Exception e){
Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
}
try{
JSONArray jArray = new JSONArray(result);
JSONObject json_data=null;
if(jArray.length() == 0)
{
}
else
{
setContentView(R.layout.viewmsglist);
for(int i=0;i<jArray.length();i++)
{
json_data = jArray.getJSONObject(i);
content_type = content_type + json_data.getString("uploadedcontenttype")+",";
content_path = content_path+"http:/localhost/project/"+(String) json_data.getString("contentpath")+",";
imageAudio_image = imageAudio_image + json_data.getString("imageaudiopath")+",";
//r.add(json_data.getString("member_id") + json_data.getString("member_name") + json_data.getString("member_usernamepath"));
content_id = content_id + json_data.getString("contentid")+",";
imageaudio_id = imageaudio_id + json_data.getString("imageaudioid")+",";
content_date = content_date + json_data.getString("contentdate")+",";
over_contentid = over_contentid + json_data.getString("overallid")+",";
uploaded_content_id = uploaded_content_id + json_data.getString("uploadedcontentid")+",";
sender_id = sender_id + json_data.getString("senderid")+",";
receiver_id = receiver_id + json_data.getString("receiverid")+",";
read_status =read_status + json_data.getString("readstatus")+",";
sender_member_image = sender_member_image + "http://localhost/project/www/"+json_data.getString("memberimage")+",";
}
// Split the data retrieved from database
content_type_split = content_type.split(",");
content_path_split = content_path.split(",");
content_id_split = content_id.split(",");
content_date_split = content_date.split(",");
sender_member_image_split = sender_member_image.split(",");
uploaded_content_id_split = uploaded_content_id.split(",");
imageAudio_image_split = imageAudio_image.split(",");
over_contentid_split = over_contentid.split(",");
mInflater = (LayoutInflater) getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
Log.e("content_type_split.length","~ "+content_type_split.length);
for(int i=0;i<content_type_split.length;i++){
try {
rd = new RowData(i,content_type_split[i],content_date_split[i]);
} catch (ParseException e) {
e.printStackTrace();
}
data.add(rd);
}
Log.e("finish here","finish" + data);
//getListView().setTextFilterEnabled(true);*/
Log.e("finish there","t finish");
//new DownloadMsg().execute();
Log.e("retrieved ", "~ "+content_type + "@ " + content_type + " # "+ content_path + " $ "+ sender_id);
/**
* Updating parsed JSON data into ListView
* */
CustomAdapter adapter = new CustomAdapter(ViewMsgMain.this, R.layout.list,R.id.title, data);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
}
catch(JSONException e1){
Toast.makeText(getApplicationContext(), "Tiada mesej", Toast.LENGTH_SHORT).show();
//Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
}
return null;
}
protected void onProgressUpdate(Integer... statusCode) {
switch (statusCode[0]) {
case CANCELED:
removeDialog(PROGRESSDIALOG_ID);
Toast.makeText(getApplicationContext(),
getString(R.string.CanceledMessage),
Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
break;
case OTHER_INTERNAL_ERROR:
removeDialog(PROGRESSDIALOG_ID);
Toast.makeText(getApplicationContext(),
getString(R.string.internal_exception_message),
Toast.LENGTH_LONG).show();
break;
case SECURITY_ERROR:
removeDialog(PROGRESSDIALOG_ID);
Toast.makeText(getApplicationContext(),
getString(R.string.security_exception_message),
Toast.LENGTH_LONG).show();
break;
case SERVER_STATUS_UPLOADED:
if(!uploadFlag)
uploadFlag = true;
default:
progressDialog.setProgress(uploadCounter);
adapter.notifyDataSetChanged();
}
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all tb_cafe
progressDialog.dismiss();
// updating UI from Background Thread
}//end of onPostExecute
class DownloadMsg扩展异步任务{
受保护的void onPreExecute(){
super.onPreExecute();
progressDialog=新建progressDialog(ViewMsgMain.this);
progressDialog.setMessage(“正在加载,请稍候…”);
progressDialog.setUndeterminate(false);
progressDialog.setCancelable(假);
progressDialog.show();
}
受保护的Void doInBackground(Void…参数){
试一试{
//http post
HttpClient HttpClient=新的DefaultHttpClient();
//===================================================
//从本地数据库获取成员id
sp_memberid=getSharedReferences(memberid,MODE_PRIVATE);
if(sp_memberid.contains(“memberid”)==true)
{memid=sp_memberid.getInt(“memberid”,0);}
Log.e(“查看消息成员id==”,“~”+memid);
//===================================================
HttpPost HttpPost=新的HttpPost(“http://localhost/project/viewmessage.php?memberid=“+memid);
//setEntity(新的UrlEncodedFormEntity(nameValuePairs));//编码适合HTTP调用的NameValuePair对象列表
HttpResponse response=httpclient.execute(httppost);//使用httpclient进行httppost调用
HttpEntity=response.getEntity();
inputstream=entity.getContent();
//将响应转换为字符串
BufferedReader=新的BufferedReader(新的InputStreamReader(inputstream,“UTF-8”));
stringbuilder=新的stringbuilder();
字符串行=null;
而((line=reader.readLine())!=null)
{
stringbuilder.append(行+“\n”);
}
inputstream.close();
结果=stringbuilder.toString();
}
捕获(例外e){
Toast.makeText(getBaseContext(),e.toString(),Toast.LENGTH_LONG).show();
}
试一试{
JSONArray jArray=新JSONArray(结果);
JSONObject json_data=null;
if(jArray.length()==0)
{
}
其他的
{
setContentView(R.layout.viewmsglist);
对于(int i=0;iu,您正在doin后台线程中设置setcontentview。您不能在doin后台设置任何与UI相关的代码。这是一个后台线程。它与UI线程没有关系。只有您可以在预执行或后执行中更改UI。其他选项是在doin后台调用onProgressUpdate()
请注意,onProgressUpdate()用于通过此方法操作异步操作的进度。请注意数据类型为Integer的参数。这对应于类定义中的第二个参数。可以通过调用publishProgress()从doInBackground()方法体中触发此回调.您正在doin后台线程中设置setcontentview。您不能在doin后台设置任何与UI相关的代码。这是一个后台线程。它与UI线程没有关系。只有您可以在预执行或后执行中更改UI。其他选项是在doin后台调用onProgressUpdate()
请注意,onProgressUpdate()用于通过此方法操作异步操作的进度。请注意数据类型为Integer的参数。这对应于类定义中的第二个参数。可以通过调用publishProgress()从doInBackground()方法体中触发此回调在UI线程中,您可以在这里更新任何UI元素,包括设置新的列表适配器
因此,在您的例子中,您可能需要doInBackground
返回一个包含状态和适配器的自定义对象,因此类似于
public class Reply {
public int status;
public String message;
public Vector<RowData> data;
}
顺便说一句,您在onProgressUpdate
中拥有的所有代码,您可能会将其放入onPostExecute
。onProgressUpdate
用于增量更新长时间操作的进度。您可以从后台线程调用publishProgress
,以便最终将onProgressUpdate调用为o但是,由于您的操作是一个阻塞http请求,因此在这里使用onProgressUpdate
可能毫无意义,并且该逻辑应该成为onPostExecute
的一部分异步任务
被分段,以便doInBackground
在后台运行,并且当您在bAckGround,您不能触摸UI。doInBackground
操作应返回一个结果,该结果将传递给onPostExecute
。onPostExecute
在UI线程上运行,您可以在此更新任何UI元素,包括设置新的列表适配器
因此,在您的例子中,您可能需要doInBackground
返回一个包含状态和适配器的自定义对象,因此类似于
public class Reply {
public int status;
public String message;
public Vector<RowData> data;
}
顺便说一句,您在onProgressUpdate
中拥有的所有代码,您可能会将其放入onPostExecute
。onProgressUpdate
用于增量更新长时间操作的进度。您可以调用publishProgress
public Reply doInBackground() {
// do your time consuming work
Reply reply = new Reply();
// if error, create reply, and set the error status
// reply.error=CANCELLED
// if no error, you have data, then set data
// reply.data = data
return reply;
}
public void onPostExecute(Reply reply) {
if (reply.data !=null) {
// create CustomAdapter using reply.data and set in listview
} else {
// Make a toast with the reply.message or reply.error
// do any other error clean up that is required on the UI
}
}