我正在Android技术中工作。在我的应用程序中,我从Web服务中提取数据,我使用了一个用于使用Web服务的同步任务。它在Toast上正确显示数据,即true或假,但我的进度栏不断移动它不会停止当后台任务完成其执行时,我在onPostExecute()称为解除方法,但它没有关闭进度条。再次开始调用Web服务,但进度条不被取消。ProgressBar在Android中完成后台执行后不会停止
你能告诉我,我做错了什么,我code.Please解决我的问题..
我附上这里的源代码
异步任务
public class MyTask extends AsyncTask<String,Void,String>
{
@Override
protected void onPostExecute(String result)
{
// TODO Auto-generated method stub
super.onPostExecute(result);
pd.dismiss();
Toast.makeText(getApplicationContext(), ""+result,Toast.LENGTH_LONG).show();
}
@Override
protected void onPreExecute()
{
// TODO Auto-generated method stub
super.onPreExecute();
pd=new ProgressDialog(MainActivity.this);
pd.setMessage("Please Wait");
pd.setCancelable(false);
pd.setIndeterminate(false);
pd.show();
}
@Override
protected String doInBackground(String... arg0)
{
// TODO Auto-generated method stub
v=new Validate();
String result;
result=v.SendParam(url,user,pass);
return result;
}
}
这里ID Validate.java代码
class Validate
{
ArrayList<NameValuePair> namevaluepair;
InputStream is;
String res;
public String SendParam(String url,String username,String password)
{
try
{
HttpClient client=new DefaultHttpClient();
HttpPost post=new HttpPost(url);
// add parameter here
namevaluepair=new ArrayList<NameValuePair>();
namevaluepair.add(new BasicNameValuePair("id",username));
namevaluepair.add(new BasicNameValuePair("password",password));
post.setEntity(new UrlEncodedFormEntity(namevaluepair));
HttpResponse response=client.execute(post);
HttpEntity entity=response.getEntity();
is=entity.getContent();
}
catch(Exception e)
{
Log.d("Httpconnection","error",e);
}
// read response from
try
{
BufferedReader reader=new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder builder=new StringBuilder();
String line;
while((line=reader.readLine())!=null)
{
builder.append(line+"\n");
}
is.close();
res=builder.toString();
}
catch(Exception e)
{
Log.d("reading Input Stream","",e);
}
return res;
}
}
您在哪里声明了pd? – Blackbelt
in mainActivity我相信@blackbelt –
我在MainActivity.ProgressDialog中声明了pd pd; – user3202493