Web サービスのリクエストからの応答を待っている間にプログレス バーを表示したい。ただし、この間、Android プログレス バーは読み込まれません。
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");
}
}
}
リクエスト/レスポンスの送受信中にプログレスバーを表示したい。