我编写了两个类,一个用于从XML提要中检索字段并将它们作为字符串存储在类中,而另一个类应该检索这些字段以在xml布局文件中设置文本各种TextView。
使用Logs,我设法看到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解析器获取String值的类:
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
我希望这对任何人来说都足够帮助我,如果你还需要其他任何问题,可以在问题下发表评论,谢谢! :)
答案 0 :(得分:1)
正在发生的是您正在使用新线程运行XML解析器(HttpURLConnection是一个异步操作)。因此,mian线程调用fetchTypesXml()函数并在实际接收数据之前检索信息。
事实上,在你的logcat中,你得到的是活动的日志而不是类的日志。
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
要解决此问题,您可以在信息准备就绪时实现一个用于调用活动内部函数的接口。我会播种一些伪代码..
接口(伪代码):
public interface XMLResponse {
public void sendXMLData(String adresse, String telephone ,String fax, String website, ...);
}
你的函数需要接收监听器(pseucode):
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(伪代码)调用您的方法:
public class Contact extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, XMLResponse {
...
stackAgence.fetchTypesXml(this);
...
最后,在您的活动中,实现接口方法(伪代码):
@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);
}