如何在Ksoap Exception中设置Toast

时间:2014-06-22 20:19:02

标签: android exception ksoap

当Ksoap在IOexception或XmlPullParserException上获取异常时,我试图设置Toast。我在EXCEPTION中尝试使用此代码,但我无法显示吐司..我猜它的应用程序崩溃并且没有时间显示吐司,但我不确定......需要帮助!提前谢谢你:

    public class RequestWS extends Activity {
    private static final String NAMESPACE = "-------";
    private static String URL="http://-------";
    private static final String METHOD_NAME_SYNCHROAP = "-----";
    private static final String SOAP_ACTION_SYNCHROAP = "-----";
    private SoapObject request=null;
    private SoapSerializationEnvelope envelope=null;
    private Object  resultsRequestSOAP=null;

    public String requestSession() {

    request = new SoapObject(NAMESPACE, METHOD_NAME_SESSION);


    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true; 

    envelope.setOutputSoapObject(request);

    HttpTransportSE transporte = new HttpTransportSE(URL);

    try {   

        transporte.call(SOAP_ACTION_SESSION, envelope);
        resultsRequestSOAP = (Object)envelope.getResponse();
    } catch (IOException e) {
         RequestWS.this.runOnUiThread(new Runnable() {
                     public void run() {
                     Toast.makeText(activity, "ERROR", Toast.LENGTH_SHORT).show();
                     }
                     });

        e.printStackTrace();

    } catch (XmlPullParserException e) {
        // NEED THE CORRECT CODE HERE
        Toast.makeText(this, "ERROR", Toast.LENGTH_LONG).show();
        e.printStackTrace();

    }

    String strJSON = resultsRequestSOAP.toString();
    return strJSON;

}
    }

我也在EXCEPTION中尝试了这段代码,但仍无法正常工作

    activity.runOnUiThread(new Runnable() {
    public void run() {
    Toast.makeText(activity, "ERROR", Toast.LENGTH_SHORT).show();
    }
    });

1 个答案:

答案 0 :(得分:0)

runOnUiThread应该完成任务。

你还可以尝试一下。

在您的活动中声明一个处理程序并将其传递给此函数。

public String requestSession(final Handler handler) {

...
} catch (IOException e) {
    // NEED THE CORRECT CODE HERE'
    handler.post(new Runnable(){
         Toast.makeText(this, "ERROR", Toast.LENGTH_LONG).show();
    });

    e.printStackTrace();

} catch (XmlPullParserException e) {
    // NEED THE CORRECT CODE HERE
    handler.post(new Runnable(){
         Toast.makeText(this, "ERROR", Toast.LENGTH_LONG).show();
    });
    e.printStackTrace();

}

...

}