问题描述
我有两个文件XMLParser的,从URL和活动解析XML。似乎getter方法不起作用。我可输出XMLParser的文件ArrayList的值。但不是在活动文件。
公共XMLParser类扩展的AsyncTask<无效,无效的ArrayList<对象>> { ArrayList的<串GT; groupItem =新的ArrayList<串GT;();
ArrayList的<对象> childItem =新的ArrayList<对象>();
ArrayList的<串GT;孩子=新的ArrayList<串GT;();
的TrustManager [] = trustAllCerts新的TrustManager [] {新X509TrustManager(){
公共java.security.cert.X509Certificate [] getAcceptedIssuers()为{
返回null;
} 公共无效checkClientTrusted(
java.security.cert.X509Certificate []证书,字符串的authType){
} 公共无效checkServerTrusted(
java.security.cert.X509Certificate []证书,字符串的authType){
的System.out.println(authType的是+的authType);
的System.out.println(发行人证书);
的for(int i = 0; I< certs.length;我++){
的System.out.println(\\ t的
+证书[I] .getIssuerX500Principal()的getName())。
的System.out.println(\\ t+证书[I] .getIssuerDN()的getName());
}
}
}}; @覆盖
保护的ArrayList<对象> doInBackground(虚空...... PARAMS){ 尝试{
的SSLContext SC = SSLContext.getInstance(SSL);
sc.init(NULL,trustAllCerts,新java.security.SecureRandom中的());
HttpsURLConnection的
.setDefaultSSLSocketFactory(sc.getSocketFactory());
}赶上(例外五){
e.printStackTrace();
System.exit(1);
} 尝试{
网址URL =新的URL();
DBF的DocumentBuilderFactory = DocumentBuilderFactory.newInstance();
的DocumentBuilder分贝= dbf.newDocumentBuilder();
文档的文档= db.parse(新的InputSource(url.openStream()));
doc.getDocumentElement()正常化()。
节点列表节点列表= doc.getElementsByTagName(设备);
的for(int i = 0; I< nodeList.getLength();我++){
元件deviceElement =(元件)nodeList.item(ⅰ);
groupItem.add(deviceElement.getAttribute(串行));
孩子=新的ArrayList<串GT;();
child.add(deviceElement.getAttribute(模特));
child.add(deviceElement.getAttribute(财富));
child.add(deviceElement.getAttribute(位置));
child.add(deviceElement.getAttribute(lastConnected));
childItem.add(小孩);
}
;
}赶上(例外五){
的System.out.println(XML帕兴错误时抛出=+ e)条;
}
返回childItem;
} 保护无效onPostExecute(){
//在这里你会得到的结果
} 公众的ArrayList<串GT; getGroupItem(){ 返回groupItem;
} 公众的ArrayList<对象> getChildItem(){
返回childItem;
}
下面是活动的文件:
公共类DevicesActivity扩展ExpandableListActivity工具
OnChildClickListener {
ArrayList的<串GT; groupItem =新的ArrayList<串GT;();
ArrayList的<对象> childItem =新的ArrayList<对象>(); @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
/ **创建一个新的布局,以显示视图* /
//setContentView(R.layout.activity_devices); // ExpandableListView expandbleLis = getExpandableListView();
//expandbleLis.setDividerHeight(2);
//expandbleLis.setGroupIndicator(null);
//expandbleLis.setClickable(true);
startXMLParser(); 的System.out.println(大小在设备+ groupItem.size());
的System.out.println(大小在设备+ childItem.size()); }
私人无效startXMLParser(){
XMLParser的XMLTask =新XMLParser的();
XMLTask.execute(NULL,NULL,NULL);
groupItem = XMLTask.getGroupItem();
childItem = XMLTask.getChildItem();
}
获取的ArrayList
公共无效getArrayList(ArrayList的< DataInterface> LST){
finalList =善堂; }
由于 AyncTask
是异步的,没有保证结果将在那里当你打电话给你的方法来检索数据,事实上,它可能不会。您需要确保该任务已试图访问这些变量前完成。
一个解决办法是通过这些数据到活动
在 onPostExecute()
。如果这个类是一个内部类的活动的
,那么你可以有成员变量和 onPostExecute为它们分配()
或另一个的AsyncTask
方法。
此外,你应该有参数
键入您的 onPostExecute()
声明,否则将不接受结果。你应该添加 @覆盖
注释
@覆盖
保护无效onPostExecute(ArrayList的<对象>的结果){
//在这里你会得到的结果
}
在AsyncTask的创建一个构造函数接收上下文
活动MACT;
公共CALSS MyTask延伸的AsyncTask< ...> //你知道在这里补充
{
公共MyTask(活动法)
{
MACT =行为;
}
然后在 onPostExecute使用
打电话给你的 mContext
()活动
函数
在创建任务
MyTask任务=新MyTask(本); //通过上下文来构造
taske.execute(//添加PARAMS);公共无效onPostExecute(ArrayList的<对象>的结果){
mAct.yourMethod(结果);
I have two files XMLParser that parse XML from URL and the Activity. Seems the getter methods don't work. I can output the values of the arrayList in XMLParser files. but not in Activity file.
public class XMLParser extends AsyncTask<Void, Void, ArrayList<Object>> {
ArrayList<String> groupItem = new ArrayList<String>();
ArrayList<Object> childItem = new ArrayList<Object>();
ArrayList<String> child = new ArrayList<String>();
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
System.out.println("authType is " + authType);
System.out.println("cert issuers");
for (int i = 0; i < certs.length; i++) {
System.out.println("\t"
+ certs[i].getIssuerX500Principal().getName());
System.out.println("\t" + certs[i].getIssuerDN().getName());
}
}
} };
@Override
protected ArrayList<Object> doInBackground(Void... params) {
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection
.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
try {
URL url = new URL();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("Device");
for (int i = 0; i < nodeList.getLength(); i++) {
Element deviceElement = (Element) nodeList.item(i);
groupItem.add(deviceElement.getAttribute("serial"));
child = new ArrayList<String>();
child.add(deviceElement.getAttribute("model"));
child.add(deviceElement.getAttribute("asset"));
child.add(deviceElement.getAttribute("location"));
child.add(deviceElement.getAttribute("lastConnected"));
childItem.add(child);
}
;
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
return childItem;
}
protected void onPostExecute() {
// here you will get the result
}
public ArrayList<String> getGroupItem() {
return groupItem;
}
public ArrayList<Object> getChildItem() {
return childItem;
}
Here is Activity file:
public class DevicesActivity extends ExpandableListActivity implements
OnChildClickListener {
ArrayList<String> groupItem = new ArrayList<String>();
ArrayList<Object> childItem = new ArrayList<Object>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Create a new layout to display the view */
//setContentView(R.layout.activity_devices);
//ExpandableListView expandbleLis = getExpandableListView();
//expandbleLis.setDividerHeight(2);
//expandbleLis.setGroupIndicator(null);
//expandbleLis.setClickable(true);
startXMLParser();
System.out.println("size in Device "+groupItem.size());
System.out.println("size in Device "+childItem.size());
}
private void startXMLParser() {
XMLParser XMLTask = new XMLParser();
XMLTask.execute(null,null,null);
groupItem=XMLTask.getGroupItem();
childItem=XMLTask.getChildItem();
}
Get ArrayList:
public void getArrayList(ArrayList<DataInterface> lst) {
finalList=lst;
}
Since AyncTask
is asynchronous there is no guarantee that the results will be there when you call your methods to retrieve the data, in fact, it probably won't be. You need to make sure that the task has completed before trying to access those variables.
One solution would be to pass that data on to the Activity
in onPostExecute()
. If this class is an inner class of the Activity
then you can have member variables and assign them in onPostExecute()
or another AsyncTask
method.
Also, you should have the parameter
type in your onPostExecute()
declaration or it won't receive the results. And you should add the @Override
annotation
@Override
protected void onPostExecute(ArrayList<Object> result) {
// here you will get the result
}
In AsyncTask create a constructor to receive Context
Activity mAct;
public calss MyTask extends AsyncTask<...> // you know what to add here
{
public MyTask(Activity act)
{
mAct = act;
}
then use mContext
in onPostExecute()
to call your Activity
function
When you create the task
MyTask task = new MyTask(this); // pass context to constructor
taske.execute(// add params);
public void onPostExecute(ArrayList<Object> result){
mAct.yourMethod(result);
这篇关于的AsyncTask的getter方法,从其他文件中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!