-3

catch ブロックの値を別のアクティビティに渡す方法は? URL にアクセスし、サーバーがビジー状態または応答していないときに XML 応答を取得すると、私のアクティビティは正常に動作します。catch ブロック ステートメントを別のアクティビティに渡すにはどうすればよいですか?

       public class AgAppHelperMethods
       {
         private static final String LOG_TAG = null;
         public static String varMobileNo;
         public static String varPinNo;
         String[][] xmlRespone = null;
         public static String[][] AgAppXMLParser( String parUrl)
         {
    String _node,_element;
            String[][] xmlRespone = null;
            HttpURLConnection urlConnection = null;
            try
               {        
                String url = parUrl;
                URL finalUrl = new URL(url);    
            urlConnection = (HttpURLConnection) finalUrl.openConnection();
                urlConnection.connect();
            DocumentBuilderFactory dbf =  
    DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new  
                    InputSource(urlConnection.getInputStream()));
        doc.getDocumentElement().normalize();
                NodeList list=doc.getElementsByTagName("*");
            _node=new String();
            _element = new String();
            xmlRespone = new String[list.getLength()][2];
            for (int i=0;i<list.getLength();i++)
                {
                    Node value=list.item(i).getChildNodes().item(0);
                    _node=list.item(i).getNodeName();
                    _element=value.getNodeValue();
                    xmlRespone[i][0] = _node;
                    xmlRespone[i][1] = _element;

                }//end for

            urlConnection.disconnect();

        }//end try

    catch (Exception e)
    {
        Log.e(LOG_TAG, "CONNECTION ERROR O SERVER NOT RESPONDING", e);
    } 
    return xmlRespone;         

}
4

3 に答える 3

2

あなたが言ったように、あなたはインテントを呼び出したくないし、あなたはこの例外を特定のクラスに渡さないので、Activityを使うことはできません...

では、これはどうですか?

Public Class Messages{

static String message = "";

public static void setMessage(String msg){
        message= msg;

    }
public static String getMessage(){
   return message;

}



}

...............ここで設定します

    catch(Exception e)

{
  Messages.setmessage("CONNECTION ERROR O SERVER NOT RESPONDING");
}

そして、あなたはそれをどこでも手に入れることができます、

文字列メッセージ=Messages.getMessage();

最新の更新されたメッセージまたは空の文字列を返します。すべてのメッセージを保持したい場合は、配列を使用できます。それはあなたのために機能しますか?

于 2012-10-09T07:01:47.043 に答える
0

メソッドを呼び出している他のクラスで例外を受け取りたい場合は、次の方法でそれを行うことができます。

  1. 別のクラスで受け取りたい catch ブロックを削除します
  2. メソッドの最後にthrows Exception/IoExceptionを追加して、メソッドから例外をスローします。
  3. メソッドの呼び出しクラスから、try および catch ブロックを配置し、そこで例外を処理します。

    編集

メソッドクラスの呼び出し以外の他のクラスに例外を渡したい場合は、例外用のクラスを作成し、ゲッターメソッドとセッターメソッドを使用して、どこからでも例外を設定および取得できます。そのクラス オブジェクトを受信例外クラスに渡します。

于 2012-10-09T07:16:37.563 に答える