Java 在Asynctask非活动类中预执行时未打开进度对话框
我正在使用ksoap调用一个web服务。 当应用程序启动时,或者当我滚动到页面末尾时,应用程序正确地从Web服务获取数据,并按我希望的方式显示数据,但当我希望在异步任务中显示进度对话框时,它无法正常工作。它显示了PostExecute之后的进度对话框。那么,我的代码出了什么问题?我不明白我的问题出在哪里 我有一个名为ElvatraWebService的web服务类: 下面是一个方法,它使用异步任务获取最新帖子Java 在Asynctask非活动类中预执行时未打开进度对话框,java,android,android-asynctask,progressdialog,Java,Android,Android Asynctask,Progressdialog,我正在使用ksoap调用一个web服务。 当应用程序启动时,或者当我滚动到页面末尾时,应用程序正确地从Web服务获取数据,并按我希望的方式显示数据,但当我希望在异步任务中显示进度对话框时,它无法正常工作。它显示了PostExecute之后的进度对话框。那么,我的代码出了什么问题?我不明白我的问题出在哪里 我有一个名为ElvatraWebService的web服务类: 下面是一个方法,它使用异步任务获取最新帖子 private String GetLastPublicPosts(int coun
private String GetLastPublicPosts(int counter){
...
...
...
...
return resTxt; //Returns JSON String
}
public String getLatestPost(int counter, Activity a){
CallLatestPost task = new CallLatestPost(counter,a);
try {
strJSON = task.execute("getLatest").get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Log.i("sss", "--" + strJSON);
return strJSON;
}
private class CallLatestPost extends AsyncTask<String, Void, String> {
private int Counter = 0;
private ProgressDialog dialog;
private Activity activity;
private Context context;
public CallLatestPost (int c,Activity actt) {
super();
this.Counter = c;
activity = actt;
context = actt;
dialog = new ProgressDialog(this.context);
}
@Override
protected String doInBackground(String... params) {
ElvatraWebService elws = new ElvatraWebService();
String tmp = elws.GetLastPublicPosts(this.Counter);
//Log.i("strJSON",strJSON);
return tmp;
}
@Override
protected void onPostExecute(String result) {
//Set response
super.onPostExecute(result);
if (dialog.isShowing()) {
dialog.dismiss();
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(context);
dialog.setMessage("Connecting...");
//dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show();
Log.i("@@@Async", "=== " + "PreExec");
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
私有字符串GetLastPublicPosts(int计数器){
...
...
...
...
return resTxt;//返回JSON字符串
}
公共字符串getLatestPost(int计数器,活动a){
CallLatestPost任务=新的CallLatestPost(计数器,a);
试一试{
strJSON=task.execute(“getLatest”).get();
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(执行例外){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//Log.i(“sss”、“--”+strJSON);
返回strJSON;
}
私有类CallLatestPost扩展异步任务{
专用整数计数器=0;
私人对话;
私人活动;
私人语境;
公共CallLatestPost(内部c、活动actt){
超级();
这个计数器=c;
活动=actt;
上下文=actt;
dialog=新建ProgressDialog(this.context);
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
ElvatraWebService elws=新的ElvatraWebService();
字符串tmp=elws.GetLastPublicPosts(this.Counter);
//Log.i(“strJSON”,strJSON);
返回tmp;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//设定响应
super.onPostExecute(结果);
if(dialog.isShowing()){
dialog.dismise();
}
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
dialog=新建进度对话框(上下文);
设置消息(“连接…”);
//对话框。setUndeterminate(true);
对话框。可设置可取消(true);
dialog.show();
Log.i(“@@@Async”,“==”+“PreExec”);
}
@凌驾
受保护的void onProgressUpdate(void…值){
}
}
我有一门课叫波斯特。它将从Elvatrawebservice调用getLatestPost。
然后,它将使用PostAdapter类将JSON字符串转换为JsonArray。PostAdapter是一个扩展BaseAdapter的类
在主活动中,我在onCreate方法中调用了一个名为LoadContent的本地方法。
这是我的活动课
public class MainActivity extends Activity {
String displayText="";
public TextView tv;
public ListView lv;
public ProgressBar pg;
int theCounter=20;
String[] strTAG = new String[] {"title","photo","date","desc","linkurl"};
//int[] intField = new int[] {R.id.title,R.id.imgPost, R.id.Date, R.id.desc, R.id.link};
ListAdapter sa;
List<HashMap<String,Object>> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recentposts);
lv = (ListView) findViewById(R.id.list);
lv.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
final int lastItem = firstVisibleItem + visibleItemCount;
if(lastItem == totalItemCount) {
//load more data
// Load content of listview
LoadContent();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onDestroy() {
super.onDestroy(); // Always call the superclass
// Stop method tracing that the activity started during onCreate()
android.os.Debug.stopMethodTracing();
}
public void LoadContent(){
lv = (ListView) findViewById(R.id.list);
Post p = new Post(theCounter);
theCounter+=20;
if (p.GetLatestPosts(lv,MainActivity.this))
{
//tv.setText("true");
}
}
}
公共类MainActivity扩展活动{
字符串displayText=“”;
公共图文电视;
公共列表视图lv;
公共图书馆;
计数器的整数=20;
字符串[]strTAG=新字符串[]{“title”、“photo”、“date”、“desc”、“linkurl”};
//int[]intField=newint[]{R.id.title,R.id.imgPost,R.id.Date,R.id.desc,R.id.link};
Listsa;
名单;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.recentposts);
lv=(ListView)findViewById(R.id.list);
lv.setOnScrollListener(新的OnScrollListener(){
@凌驾
公共无效onScrollStateChanged(AbsListView视图,int scrollState){
}
@凌驾
public void onScroll(AbsListView视图,int firstVisibleItem,
int visibleItemCount,int totalItemCount){
final int lastItem=firstVisibleItem+visibleItemCount;
如果(lastItem==totalItemCount){
//加载更多数据
//加载listview的内容
LoadContent();
}
}
});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
公共空间{
super.ondestory();//始终调用超类
//停止跟踪活动在onCreate()期间启动的方法
android.os.Debug.stopMethodTracing();
}
公共void LoadContent(){
lv=(ListView)findViewById(R.id.list);
职位p=新职位(计数器);
计数器+=20;
if(p.GetLatestPosts(lv、MainActivity.this))
{
//tv.setText(“真”);
}
}
}
您有
task.execute("getLatest").get();
您不应该调用get()
,因为这会阻止ui线程等待结果,从而使其不再异步。只用
task.execute("getLatest");
您可以在
doInBackground
中返回结果,并在onPostExecute
中更新ui,当我将此行strJSON=task.execute(“getLatest”).get()
更改为strJSON=task.execute(“getLatest”)
时,我将出现类型不匹配错误。它将建议我将strJson的类型从String
更改为AsyncTask
。那么,我该怎么办呢?不需要strJSON=task.execute(“getLatest”)只要task.execute(“getLatest”)
。您可以使用接口作为对activity@user3594119选中此仅使用任务。执行(“getLatest”)
您可以使用接口返回活动