gpt4 book ai didi

java - 从类(xml解析类)检索String到当前类

转载 作者:行者123 更新时间:2023-12-01 09:43:39 25 4
gpt4 key购买 nike

我编写了两个类,一个用于从 XML feed 中检索字段并将它们作为字符串存储在类中,另一个类应该检索这些字段以在 xml 布局文件中设置各种 TextView 的文本。

使用日志,我设法看到 xml 解析器类确实从 xml feed 中获取字段,因为它们打印在 android 监视器中,但当我尝试传递它们时,它们不会打印在其他类中。我只获得初始化这些字符串时为它们设置的默认值。

我可能做错了什么,但我不知道,从昨天下午开始我就一直被这个问题困扰!

这是存储检索到的值的 xml 解析器类:

public class StackAgenceXml {

/**
* fields
*/
private String urlAgenceStringXml = null;
private String nom = "nom";
private String adresse = "adresse";
private String codepostal = "codepostal";
private String ville = "ville";
private String pays = "pays";
private String lat = "lat";
private String lng = "lng";
private String tel = "tel";
private String fax = "fax";
private String email = "email";
private String web = "url";
private String logo = "logo";
private XmlPullParserFactory xmlFactoryObject;
public volatile boolean parsingComplete = false;

/**
* Constructor.
*
* @param url
*/
public StackAgenceXml(String url) {
this.urlAgenceStringXml = url;
}

/**
* @param agenceParser
*/
public void parseXmlAgenceAndStore(XmlPullParser agenceParser) {
int event;
String text = null;

try {
event = agenceParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String name = agenceParser.getName();

switch (event) {
case XmlPullParser.START_TAG:
break;

case XmlPullParser.TEXT:
text = agenceParser.getText();
break;

case XmlPullParser.END_TAG:
if (name.equals("nom")) {
nom = text;
} else if (name.equals("adresse")) {
//adresse = agenceParser.getAttributeValue(null, "value");
adresse = text;
} else if (name.equals("codepostal")) {
codepostal = text;
} else if (name.equals("ville")) {
ville = text;
} else if (name.equals("pays")) {
pays = text;
} else if (name.equals("lat")) {
lat = text;
} else if (name.equals("lng")) {
lng = text;
} else if (name.equals("tel")) {
tel = text;
} else if (name.equals("fax")) {
fax = text;
} else if (name.equals("email")) {
email = text;
} else if (name.equals("url")) {
web = text;
} else if (name.equals("logo")) {
logo = text;
} else {
}
break;
}
event = agenceParser.next();
}
parsingComplete = false;
} catch (Exception e) {
e.printStackTrace();
}
Log.i("ex of agency info = ", nom + adresse + codepostal + ville + pays + lat + lng + tel +
fax + email + web + logo);
}

/**
* fetch data from xml url
*/
public void fetchTypesXml() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(urlAgenceStringXml);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setReadTimeout(10000 /*ms*/);
conn.setConnectTimeout(15000 /*ms*/);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();

InputStream stream = conn.getInputStream();
xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlFactoryObject.newPullParser();

myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
myparser.setInput(stream, null);

parseXmlAgenceAndStore(myparser);
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}

/**
* GETTERS*
*/
public String getAdresse() {
return adresse;
}
public String getNom() {
return nom;
}

public String getCodepostal() {
return codepostal;
}

public String getVille() {
return ville;
}

public String getPays() {
return pays;
}

public String getLat() {
return lat;
}

public String getLng() {
return lng;
}

public String getTel() {
return tel;
}

public String getFax() {
return fax;
}

public String getEmail() {
return email;
}

public String getWeb() {
return web;
}

public String getLogo() {
return logo;
}
}

这是我尝试从 xml 解析器获取字符串值的类:

public class Contact extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

/**
* fields
*/
private StackAgenceXml stackAgence;
private TextView txtAdresse, txtTel, txtFax, txtWeb;
private String adresse, telephone, fax, website;
//to change agency go to string values and change the id at the end of xml feed url
private String urlAgencesXml = "http://api.immotoolbox.com/xml/2.0/AGENCE/agences/?id=115";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);

/**fetch xml data*/
stackAgence = new StackAgenceXml(urlAgencesXml);
stackAgence.fetchTypesXml();

/**get references*/
txtAdresse = (TextView) findViewById(R.id.txt_contact_adresse);
txtTel = (TextView) findViewById(R.id.txt_contact_tel);
txtFax = (TextView) findViewById(R.id.txt_contact_fax);
txtWeb = (TextView) findViewById(R.id.txt_contact_web);

/**assign text to textviews*/
adresse = stackAgence.getAdresse() + stackAgence.getCodepostal() + stackAgence.getPays();
telephone = stackAgence.getTel();
fax = stackAgence.getFax();
website = stackAgence.getWeb();

Log.i("contact fields retrieved", adresse + telephone + fax + website);

txtAdresse.setText(adresse);
txtTel.setText(telephone);
txtFax.setText(fax);
txtWeb.setText(website);
}

最后,这是日志,您可以清楚地看到它们在 xml 解析器类中检索,但在其他类中未检索,因为仅打印默认值:

07-07 09:00:49.830 21234-21234/com.example.adam_jaamour.cfimmobilier W/System: ClassLoader referenced unknown path: /data/app/com.example.adam_jaamour.cfimmobilier-2/lib/x86
07-07 09:00:49.941 21234-21234/com.example.adam_jaamour.cfimmobilier W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
07-07 09:00:50.115 21234-21369/com.example.adam_jaamour.cfimmobilier D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

[ 07-07 09:00:50.119 21234:21234 D/ ]
HostConnection::get() New Host Connection established 0xaff50460, tid 21234


[ 07-07 09:00:50.149 21234:21369 D/ ]
HostConnection::get() New Host Connection established 0xae4473f0, tid 21369
07-07 09:00:50.150 21234-21369/com.example.adam_jaamour.cfimmobilier I/OpenGLRenderer: Initialized EGL, version 1.4
07-07 09:00:52.271 21234-21234/com.example.adam_jaamour.cfimmobilier I/contact fields retrieved: adressecodepostalpaystelfaxurl
07-07 09:00:52.518 21234-21369/com.example.adam_jaamour.cfimmobilier E/Surface: getSlotFromBufferLocked: unknown buffer: 0xae479a00
07-07 09:00:52.600 21234-21405/com.example.adam_jaamour.cfimmobilier I/ex of agency info =: Cristea-Flandrin Immobilier21, boulevard des Moulins98000MonacoMonaco43.742968007.42821350+377 93 30 75 61+377 92 16 71 64cristea@monte-carlo.mchttp://www.agencecristea.comhttp://api.immotoolbox.com/media/cache/xml2_logo/uploads/agences/115/logo-medium.png
07-07 09:03:35.351 21234-21238/com.example.adam_jaamour.cfimmobilier W/art: Suspending all threads took: 8.151ms
07-07 09:05:25.616 21234-21238/com.example.adam_jaamour.cfimmobilier W/art: Suspending all threads took: 8.484ms
07-07 09:05:47.661 21234-21238/com.example.adam_jaamour.cfimmobilier W/art: Suspending all threads took: 5.969ms
07-07 09:06:00.189 21234-21238/com.example.adam_jaamour.cfimmobilier W/art: Suspending all threads took: 5.851ms
07-07 09:06:43.804 21234-21238/com.example.adam_jaamour.cfimmobilier W/art: Suspending all threads took: 16.978ms
07-07 09:07:31.407 21234-21238/com.example.adam_jaamour.cfimmobilier W/art: Suspending all threads took: 7.489ms

我希望这足以让任何人在这里帮助我,如果您需要其他任何内容,请在问题下评论,谢谢! :)

最佳答案

发生的情况是您正在使用新线程运行 XML 解析器(HttpURLConnection 是一个异步操作)。因此,主线程正在调用 fetchTypesXml() 函数并在实际接收数据之前检索信息。

事实上,在 logcat 中,您先获取 Activity 的日志,然后再获取类的日志。

07-07 09:00:52.271 21234-21234/com.example.adam_jaamour.cfimmobilier I/contact fields retrieved: adressecodepostalpaystelfaxurl
07-07 09:00:52.518 21234-21369/com.example.adam_jaamour.cfimmobilier E/Surface: getSlotFromBufferLocked: unknown buffer: 0xae479a00
07-07 09:00:52.600 21234-21405/com.example.adam_jaamour.cfimmobilier I/ex of agency info =: Cristea-Flandrin Immobilier21, boulevard des Moulins98000MonacoMonaco43.742968007.42821350+377 93 30 75 61+377 92 16 71 64cristea@monte-carlo.mchttp://www.agencecristea.comhttp://api.immotoolbox.com/media/cache/xml2_logo/uploads/agences/115/logo-medium.png

要解决这个问题,您可以实现一个接口(interface),以便在信息准备好时调用 Activity 内的函数。我将向您展示一些伪代码..

接口(interface)(伪代码):

public interface XMLResponse {
public void sendXMLData(String adresse, String telephone ,String fax, String website, ...);
}

您的函数需要接收监听器(伪代码):

public void fetchTypesXml(XMLResponse listener) {
...

// when you finished to request and parse all the data, call the function inside the listener
listener.sendXMLData(adresse, telephone, fax, website ...);

}

在您的 Activity 中声明接口(interface)并使用 Activity 调用您的方法(伪代码):

public class Contact extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, XMLResponse {

...

stackAgence.fetchTypesXml(this);

...

最后,在您的 Activity 中,实现接口(interface)方法(伪代码):

@Override 
public void sendXMLData(String adrs, String cdpostal, String pays, String tel, String fx, String web) {

adresse = adrs + cdpostal + pays;
telephone = tel;
fax = fx;
website = web;
txtAdresse.setText(adresse);
txtTel.setText(telephone);
txtFax.setText(fax);
txtWeb.setText(website);
}

关于java - 从类(xml解析类)检索String到当前类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38246299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com