在android中连接web服务时显示进度条
我想在等待web服务请求的响应时显示一个进度条。但在此期间,android进度条没有加载在android中连接web服务时显示进度条,android,web-services,ksoap2,Android,Web Services,Ksoap2,我想在等待web服务请求的响应时显示一个进度条。但在此期间,android进度条没有加载 public class WebService extends Activity { private static final String NAMESPACE="http://tempuri.org/"; private static final String METHOD_NAME="AddEmployee"; private static
public class WebService extends Activity {
private static final String NAMESPACE="http://tempuri.org/";
private static final String METHOD_NAME="AddEmployee";
private static final String URL="http://10.32.4.24/Android/AndroidBus.svc";
private static final String SOAP_ACTION="http://tempuri.org/IAndroidBus/AddEmployee";
String celsius;
Button b;
TextView tv;
EditText et;
String res,resultval;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_service);
et=(EditText)findViewById(R.id.editText1);
tv=(TextView)findViewById(R.id.Result);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new service().execute();
}
});
}
private class service extends AsyncTask<Void, Void, String> {
ProgressDialog pd;
protected void onPreExecute(){
pd=new ProgressDialog(getBaseContext());
pd.show();
}
@Override
protected String doInBackground(Void... arg0) {
System.out.println("In DoIn Background");
// Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi=new PropertyInfo();
pi.setName("Name");
pi.setValue(et.getText().toString());
request.addProperty(pi);
// Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
setProgress(1);
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE( URL);
// this is the actual part that will call the webservice
androidHttpTransport.debug=true;
androidHttpTransport.call(SOAP_ACTION, envelope);
String resdump=androidHttpTransport.responseDump.toString();
System.out.println(resdump);
setProgress(2);
// Get the SoapResult from the envelope body.
//SoapObject result = (SoapObject) envelope.bodyIn;
SoapPrimitive result=(SoapPrimitive)envelope.getResponse();
setProgress(3);
if (result != null) {
// Get the first property and change the label text
// txtFar.setText(result.getProperty(0).toString());
res = result.toString();
} else {
Toast.makeText(getApplicationContext(), "No Response",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
protected void onPostExecute(String h) {
String result = h;
pd.dismiss();
tv.setText(result + "°F");
}
}
}
公共类Web服务扩展活动{
私有静态最终字符串命名空间=”http://tempuri.org/";
私有静态最终字符串方法\u NAME=“AddEmployee”;
私有静态最终字符串URL=”http://10.32.4.24/Android/AndroidBus.svc";
私有静态最终字符串SOAP_ACTION=”http://tempuri.org/IAndroidBus/AddEmployee";
字符串摄氏度;
按钮b;
文本视图电视;
编辑文本;
字符串res,resultval;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u web\u服务);
et=(EditText)findViewById(R.id.editText1);
tv=(文本视图)findViewById(R.id.Result);
b=(按钮)findViewById(R.id.button1);
b、 setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
新服务().execute();
}
});
}
私有类服务扩展异步任务{
进展性帕金森病;
受保护的void onPreExecute(){
pd=新建ProgressDialog(getBaseContext());
pd.show();
}
@凌驾
受保护的字符串doInBackground(无效…arg0){
System.out.println(“In-DoIn后台”);
//初始化soap请求+添加参数
SoapObject请求=新的SoapObject(名称空间、方法名称);
PropertyInfo pi=新的PropertyInfo();
pi.设置名称(“名称”);
设置值(et.getText().toString());
请求。添加属性(pi);
//声明SOAP请求的版本
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(请求);
envelope.dotNet=true;
进展(1);
试一试{
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
//这是将调用webservice的实际部分
androidHttpTransport.debug=true;
调用(SOAP_操作,信封);
字符串resdump=androidHttpTransport.responseDump.toString();
系统输出打印项次(resdump);
进展(2);
//从信封正文获取SoapResult。
//SoapObject结果=(SoapObject)envelope.bodyIn;
SoapPrimitive结果=(SoapPrimitive)信封.getResponse();
进展(3);
如果(结果!=null){
//获取第一个属性并更改标签文本
//setText(result.getProperty(0.toString());
res=result.toString();
}否则{
Toast.makeText(getApplicationContext(),“无响应”,
Toast.LENGTH_LONG).show();
}
}捕获(例外e){
e、 printStackTrace();
}
返回res;
}
受保护的void onPostExecute(字符串h){
字符串结果=h;
pd.解散();
tv.setText(结果+“°F”);
}
}
}
我想在发送和获取请求/响应时显示进度条。尝试在“进度”对话框中设置消息
protected void onPreExecute(){
pd=new ProgressDialog(WebService.this);
pd.setMessage("Loading...");
pd.setIndeterminate(true);
pd.show();
}
尝试将消息设置为“进度”对话框
protected void onPreExecute(){
pd=new ProgressDialog(WebService.this);
pd.setMessage("Loading...");
pd.setIndeterminate(true);
pd.show();
}
您应该在这里尝试不同的上下文对象 而不是这个,
ProgressDialog(getBaseContext());
试一试
您应该在这里尝试不同的上下文对象 而不是这个,
ProgressDialog(getBaseContext());
试一试
我不知道setProgress调用的作用,但是假设它更新了进度对话框,您必须让您的asynctask实现
protected void onProgressUpdate(Integer... progress) {
setProgress(progress);
}
调用publishProgress(2)代码>在doInBackground
中,而不是setProgress
这是因为您无法更新在不同线程上运行的doInBackGround方法中的ui元素。这样做,您可能不仅不会更新对话框,而且会破坏应用程序 我不知道setProgress调用的作用,但是假设它更新了进度对话框,那么您必须让您的asynctask实现
protected void onProgressUpdate(Integer... progress) {
setProgress(progress);
}
调用publishProgress(2)代码>在doInBackground
中,而不是setProgress
这是因为您无法更新在不同线程上运行的doInBackGround方法中的ui元素。这样做,您可能不仅不会更新对话框,而且会破坏应用程序 四个步骤,我们开始。
(1) 。清除活动中的进度对话框
(2) 。启动AsyncTask类时启动对话框
例如
(3) 。在AsyncTask类的doInBackground()方法中完成繁重的工作(web服务或任何)
(4) 。然后关闭AsyncTask类的onPostExecute()中的进度对话框
四个步骤,我们开始。
(1) 。清除活动中的进度对话框
(2) 。启动AsyncTask类时启动对话框
例如
(3) 。在AsyncTask类的doInBackground()方法中完成繁重的工作(web服务或任何)
(4) 。然后关闭AsyncTask类的onPostExecute()中的进度对话框
你试图做的是错误的。SoapSerializationEnvelope、HttpTransportSE和Result之间的进度集毫无意义,因为在HttpTransportSE.call(…)内部进行了大量工作。
如果您希望在接收和发送的字节数上有一个真正的下载/上传进度条,则必须修改HttpTrasportSE类。详细地说,您必须修改这个类的call()和read()
如你所见,例如,我
dialog = ProgressDialog.show(CurrentActivity.this, "", "loading search..");
SearchTask task= new GetSearchSeedsData();
task.execute(urlString);
dialog.dismiss();
public List call(String soapAction, SoapEnvelope envelope, List headers, File outputFile) throws IOException, XmlPullParserException {
(...)
OutputStream os = connection.openOutputStream();
os.write(requestData, 0, requestData.length);
os.flush();
os.close();
(...)
public List call(String soapAction, SoapEnvelope envelope, List headers, File outputFile, ProgressDialog dialog)
throws IOException, XmlPullParserException {
(...)
if(dialog != null)
{
dialog.setIndeterminate(false);
dialog.setProgress(0);
InputStream iss = new ByteArrayInputStream(requestData, 0, requestData.length);
int byteCount = 0;
byte[] buf = new byte[256];
while (true)
{
int rd = iss.read(buf, 0, 256);
byteCount += rd;
if (rd == -1)
break;
os.write(buf, 0, rd);
dialog.setProgress((int) (((float)byteCount/(float)requestData.length)*100));
}
dialog.setIndeterminate(true);
iss.close();
buf = null;
}
else
os.write(requestData, 0, requestData.length);
os.flush();
os.close();
(...)