2016-03-06 99 views
-2

花了幾個小時試圖弄清楚爲什麼在「魂」的方法不能由「下載類閱讀「finalURL」變量?會很感激任何指針..如何從另一個類訪問變量? Android的Java的

這是一個詞彙改良中的應用,所以它從字典API獲取XML數據。

public class DefineWord extends Activity { 

    static final String head = new String("http://www.dictionaryapi.com/api/v1/references/collegiate/xml/"); 
    static final String apikey = new String("?key=xxxxxx-xxx-xxx-xxxx-xxxxx"); 

    TextView src; 
    EditText searchBar; 

OnCreate中:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.defineword); 

     src=(TextView)findViewById(R.id.textFromWeb); 
     searchBar = (EditText)findViewById(R.id.searchBar); 
    } 

fetch方法:

public void fetch(View v){            //onClick in xml starts this "fetch" method 

     String w = searchBar.getText().toString();       // get the text the user enters into searchbar 
     String finalURL = head.trim() + w.trim() + apikey.trim();   //concatenate api url 

     Downloader d = new Downloader(); 
     d.execute(finalURL);            // Calls AsyncTask to connect in the background.  
} 

的AsyncTask:

class Downloader extends AsyncTask<String,Void,String>{ 

     ArrayList<String> information = new ArrayList<String>(); // Store fetched XML data in ArrayList 

     public String doInBackground(String... urls) { 
      String result = null; 

      try { 

       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
       DocumentBuilder builder = factory.newDocumentBuilder(); 

的問題是在這裏。 「finalURL」不能被解析爲一個變量。

   Document doc = builder.parse(finalURL); //finalURL cannot be resolved to a variable ??? 

       doc.getDocumentElement().normalize(); 

       NodeList nList = doc.getElementsByTagName("entry"); // Make a list of all elements in XML file with tag name "entry" 

       for (int temp = 0; temp < nList.getLength(); temp++) { //Iterate over elements and get their attributes (Definition, etymology...) 

        Node nNode = nList.item(temp); 

        if (nNode.getNodeType() == Node.ELEMENT_NODE){ 


         Element eElement = (Element) nNode; 

         String element = ("\nCurrent Element : " + eElement.getAttribute("id")); 
         information.add(element); // 

         String definitions = ("\nDefinition : \n" + eElement.getElementsByTagName("dt").item(0).getTextContent()); 
         information.add(definitions); 
        } 

       } 

      }catch (ParserConfigurationException e){ 
       e.printStackTrace(); 

      }catch(SAXException e){ 
       e.printStackTrace(); 
      }catch(IOException e){ 
       e.printStackTrace(); 
      } 

      return result; 
     } 

     protected void onPostExecute (String result){ 

      String finalresult = information.toString(); 
      src.setText(finalresult); 
     } 

    } 
} 

感謝您的時間。

回答

3

這就是所謂的參數。通過使用asyncTask.execute(parameter)你的對象傳遞給doInBackground(String... arrayOfParameters)

所以再次訪問該值,你應該使用builder.parse(urls[0]);

+0

哇!非常感謝。我不敢相信這很容易。大聲笑。希望你有美好的一天;希望你今天過得很開心。榮譽。 –

1

finalURL聲明的fetch方法內,因此只能在這個函數中進行訪問。

如果您希望它在整個程序中都可以訪問,請在函數之外聲明它,如下所示。

class MyClass{ 
    public static String finalURL; // variable declaration 

    public void fetch(View v){ 
     finalURL = head.trim() + w.trim() + apikey.trim(); // variable assignment 
    }  

    public void otherFunction(){ 
     Document doc = builder.parse(finalURL); // retrieves static class variable 
    } 
} 

我希望這能回答你的問題。如果沒有,請讓我知道混淆是什麼。

相關問題