Java 如何从另一个类访问变量?索爱手机应用开发职位
花了几个小时试图弄清楚为什么“下载程序”“类无法读取获取”方法中的“finalURL”变量?非常感谢任何提示 这是一个词汇表改进应用程序,因此它从字典api获取XML数据Java 如何从另一个类访问变量?索爱手机应用开发职位,java,android,android-asynctask,domparser,Java,Android,Android Asynctask,Domparser,花了几个小时试图弄清楚为什么“下载程序”“类无法读取获取”方法中的“finalURL”变量?非常感谢任何提示 这是一个词汇表改进应用程序,因此它从字典api获取XML数据 public class DefineWord extends Activity { static final String head = new String("http://www.dictionaryapi.com/api/v1/references/collegiate/xml/"); static
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);
}
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.
}
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();
获取方法:
@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);
}
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.
}
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();
异步任务:
@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);
}
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.
}
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();
类下载程序扩展异步任务{
ArrayList information=new ArrayList();//将获取的XML数据存储在ArrayList中
公共字符串doInBackground(字符串…URL){
字符串结果=null;
试一试{
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=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);
}
}
}
Document doc=builder.parse(finalURL);//finalURL无法解析为变量???
doc.getDocumentElement().normalize();
NodeList nList=doc.getElementsByTagName(“entry”);//列出XML文件中标记名为“entry”的所有元素
对于(int-temp=0;temp
谢谢你抽出时间 这称为参数。通过使用
asyncTask.execute(参数)
将对象传递到doInBackground(String…arrayOfParameters)
因此,要再次访问该值,应该使用
builder.parse(URL[0])代码>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
}
}
我希望这能回答你的问题。如果没有,请告诉我混淆的是什么。哇!非常感谢你。我真不敢相信会那么容易。哈哈,我希望你有一个伟大的一天。荣誉