ProgressDialog而活动请求SoapObject从We

ProgressDialog而活动请求SoapObject从We

本文介绍了如何实现ProgressDialog而活动请求SoapObject从Web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道ProgressDialog与线程的问题已经被问了很多次,但没有一个解决方案,似乎工作为我的项目。基本上我想做的事情是这样的:1),当用户点击一个按钮的活动发送身份验证请求到服务器2),而这是被做了ProgressDialog显示3)当效应初探来了,我想解散ProgressDialog和返回的对象被读取并通过活动PTED间$ P $

如果我:1)设置线程与效应初探更新的应用领域,下一个方法(这是线程之外)访问现场时,抛出NPE2)如果我包括在主题下一种方法,第二种方法抛出一个'了java.lang.RuntimeException:内螺纹无法创建处理程序,并没有叫尺蠖prepare()

对不起了很长的文字,但我完全在这个失去它...我的code是某事是这样的:

 公共类XXX延伸活动实现OnClickListener {

//(...)
私人SoapObject返回object;
私人字符串响应;

/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    //(...)
        authProgressDialog = ProgressDialog.show(XXX.this,,鉴定......,真,假);
        新主题(新的Runnable(){
            @覆盖
            公共无效的run(){
                认证(); //方法是通过SOAP调用API
                authenticateReal(); //方法处理响应
            }
        })。开始();

        mHandler =新的处理程序(){
            公共无效的handleMessage(信息MSG){
                super.handleMessage(MSG);
                开关(msg.what){
                    案例10:
                        authProgressDialog.dismiss();
                        打破;
                }
            }
        };
    }
}

公共无效验证(){
    // API的东西(...)
    AndroidHttpTransport AHT =新AndroidHttpTransport(URL);
    尝试 {
        aht.call(SOAP_ACTION,的SoapEnvelope);
        返回object =(SoapObject)soapEnvelope.getResponse();
        。响应= returnObject.getProperty(ResponseStatus)的toString();
    }
    赶上(例外五){
        e.printStackTrace();
    }
    最后 {
        mHandler.sendEmptyMessage(10);
    }
}

//方法需要访问返回object和效应初探对象和
//在这其中的NPE的或其他异常抛出
公共无效authenticateReal(){
//(...)
}
 

解决方案

您更好地使用 的AsyncTask (这是Android的方式):

  @覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    新TheTask()执行()。
}

私有类TheTask扩展的AsyncTask<虚空,虚空,虚空> {

    @覆盖
    在preExecute保护无效(){
        authProgressDialog = ProgressDialog.show(XXX.this,,鉴定......,真,假);
    }

    @覆盖
    保护无效doInBackground(虚空...... PARAMS){
        认证(); //方法是通过SOAP调用API
        authenticateReal(); //方法处理响应
        返回null;
    }

    @覆盖
    保护无效onPostExecute(无效的结果){
        authProgressDialog.dismiss();
    }
}
 

顺便说一句......我发现这presentation是非常有用的(它谈论REST的应用程序,但可以采用同样的理念,为不同类型的应用程序)的

I know that ProgressDialog with Threads questions have been asked many times but none of the solutions seem to work for my project. Basically what I want to do is this:1) when a user clicks a button the Activity sends an auth request to the server2) while this is being done a ProgressDialog is shown3) when the reponse comes I want to dismiss the ProgressDialog and the return object to be read and interpreted by the Activity

If I:1) set the Thread to update the Application field with the reponse, the next method (which is outside of the Thread) throws an NPE when accessing the field2) if I include the next method in the Thread, the second method throws a 'java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()'

Sorry for a long text, but I am totally losing it over this... My code is sth like this:

public class XXX extends Activity implements OnClickListener {

// (...)
private SoapObject returnObject;
private String response;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // (...)
        authProgressDialog = ProgressDialog.show(XXX.this, "", "Authenticating...", true, false);
        new Thread(new Runnable() {
            @Override
            public void run() {
                authenticate(); // method that calls the API via SOAP
                authenticateReal(); // method that handles the response
            }
        }).start();

        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 10:
                        authProgressDialog.dismiss();
                        break;
                }
            }
        };
    }
}

public void authenticate() {
    // API stuff (...)
    AndroidHttpTransport aht = new AndroidHttpTransport(URL);
    try {
        aht.call(SOAP_ACTION, soapEnvelope);
        returnObject = (SoapObject) soapEnvelope.getResponse();
        response = returnObject.getProperty("ResponseStatus").toString();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        mHandler.sendEmptyMessage(10);
    }
}

// Method that needs to access returnObject and reponse objects and
// it is here where the NPE's or other exceptions are thrown
public void authenticateReal() {
// (...)
}
解决方案

You better use AsyncTask (which is the Android way):

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    new TheTask().execute();
}

private class TheTask extends AsyncTask<Void, Void, Void>{

    @Override
    protected void onPreExecute() {
        authProgressDialog = ProgressDialog.show(XXX.this, "", "Authenticating...", true, false);
    }

    @Override
    protected Void doInBackground(Void... params) {
        authenticate(); // method that calls the API via SOAP
        authenticateReal(); // method that handles the response
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        authProgressDialog.dismiss();
    }
}

By the way... I have found this presentation to be very useful (it talks about REST apps, but you can apply the same concept for different kind of apps): Developing Android REST client applications

这篇关于如何实现ProgressDialog而活动请求SoapObject从Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 05:30