如何使用ProgressDialog和异步任务同时获得方法 [英] How use ProgressDialog and Async Task get Method simultaneously
问题描述
我有一个从获取服务器响应的通用异步任务类。而我收到使用GET方法的响应。现在我knw的UI线程块时,我准时使用GET方法,其中bcoz我的进度对话框犯规showup的。
现在能有人告诉我替代做到这一点? (在任何情况下,我需要发回这使得执行的号召活动响应,从而开辟新的活动将不会帮助我)
code:
AsyncTask的类
公共类GetDataFromNetwork扩展的AsyncTask<无效,字符串,对象> {在preExecute保护无效(){
super.on preExecute();
progressDialog.show();
}
保护对象doInBackground(虚空...... PARAMS){
对象result = NULL;
尝试{
SoapSerializationEnvelope信封=新SoapSerializationEnvelope(SoapEnvelope.VER10);
新MarshalBase64()寄存器(信封);
envelope.dotNet = TRUE;
envelope.setOutputSoapObject(请求);
AndroidHttpTransport androidHttpTransport =新AndroidHttpTransport(ip地址+ web服务);
System.setProperty(http.keepAlive,真);
尝试{
androidHttpTransport.call空间(namespace +方法名,信封);
}赶上(例外五){ e.printStackTrace();
publishProgress(e.getMessage());
}
androidHttpTransport.debug = TRUE;
的System.out.println(回应:+ androidHttpTransport.requestDump);
结果= envelope.getResponse();
如果(结果!= NULL){
的System.out.println(GetDataFromNetwork.doInBackground()结果厚望---------+结果);
}
}赶上(例外五){
的System.out.println(GetDataFromNetwork.doInBackground()--------错误);
e.printStackTrace(); } 返回结果;
}保护无效onPostExecute(对象结果){
super.onPostExecute(结果);
progressDialog.dismiss();
}
code:活动
GetDataFromNetwork要求=新GetDataFromNetwork(
这个,
ProgressDialog.STYLE_SPINNER,
。getResources()的getText(R.string.autenticate)的ToString());
响应=(SoapObject)request.execute()获得()。
我在我的应用程序做的东西像这一切的时候,我发现最简单的方法是创建回调接口,并将其作为参数传递给我的AsyncTask的S。你做你的doInBackground()加工时,它的完成,你叫从onPostExecute传递结果对象作为参数的回调实例。
下面是它的一个简化版本。
Callback接口的例子:
包example.app;公共接口回调{ 无效的run(对象结果);
}
使用上面的回调接口的AsyncTask的例子:
公共类GetDataFromNetwork扩展的AsyncTask<无效,字符串,对象> {
回调回调;
公共GetDataFromNetwork(回调回调){
this.callback =回调;
} 在preExecute保护无效(){
super.on preExecute();
progressDialog.show();
}
保护对象doInBackground(虚空...... PARAMS){
对象result = NULL;
//做你的东西在这里
返回结果;
} 保护无效onPostExecute(对象结果){
callback.run(结果);
progressDialog.dismiss();
}}
如何在您的应用程序使用上面的类实例:
类示例{
公众的onCreate(捆绑savedInstanceState){
//初始化 GetDataFromNetwork要求=新GetDataFromNetwork(新回拨(){
公共无效的run(对象结果){
//做一些事情在这里与结果
}});
request.execute();
}
}
I have generic async task class which fetches response from server . And i receive those response by using get method . Now i knw that UI thread is block when i use get method , bcoz of which my progress Dialog doesnt showup on time .
Now can someone tell me alternative to do this ?? (In every case i need to send back the response to the activity which has made the call of execute so opening new activity wouldn't help me )
Code : AsyncTask Class
public class GetDataFromNetwork extends AsyncTask<Void,String,Object> {
protected void onPreExecute() {
super.onPreExecute();
progressDialog.show();
}
protected Object doInBackground(Void... params) {
Object result = null;
try {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
new MarshalBase64().register(envelope);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(ipAddress + webService);
System.setProperty("http.keepAlive", "true");
try {
androidHttpTransport.call(nameSpace + methodName, envelope);
} catch (Exception e) {
e.printStackTrace();
publishProgress(e.getMessage());
}
androidHttpTransport.debug = true;
System.out.println("response: " + androidHttpTransport.requestDump);
result = envelope.getResponse();
if(result!=null){
System.out.println("GetDataFromNetwork.doInBackground() result expection---------"+result);
}
} catch (Exception e) {
System.out.println("GetDataFromNetwork.doInBackground()-------- Errors");
e.printStackTrace();
}
return result;
}
protected void onPostExecute(Object result) {
super.onPostExecute(result);
progressDialog.dismiss();
}
Code : Activity
GetDataFromNetwork request = new GetDataFromNetwork(
this,
ProgressDialog.STYLE_SPINNER,
getResources().getText(R.string.autenticate).toString());
response= (SoapObject)request.execute().get();
I'm doing stuff like this all the time in my apps and the easiest way I found was to create "Callback" interface and pass it as a parameter to my "AsyncTask"s. You do your "doInBackground()" processing and when it's finished you call the "Callback" instance from onPostExecute passing the "result" object as parameter.
Below is a very simplified version of it.
Example of Callback interface:
package example.app;
public interface Callback {
void run(Object result);
}
Example of AsyncTask using the Callback interface above:
public class GetDataFromNetwork extends AsyncTask<Void,String,Object> {
Callback callback;
public GetDataFromNetwork(Callback callback){
this.callback = callback;
}
protected void onPreExecute() {
super.onPreExecute();
progressDialog.show();
}
protected Object doInBackground(Void... params) {
Object result = null;
// do your stuff here
return result;
}
protected void onPostExecute(Object result) {
callback.run(result);
progressDialog.dismiss();
}
}
Example of how to use the classes above in your app:
class Example {
public onCreate(Bundle savedInstanceState){
//initialize
GetDataFromNetwork request = new GetDataFromNetwork(new Callback(){
public void run(Object result){
//do something here with the result
}});
request.execute();
}
}
这篇关于如何使用ProgressDialog和异步任务同时获得方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!