解析XML始终返回Null

时间:2013-08-13 18:05:02

标签: java android xml xml-parsing inputstream

我有一个XML文件,其中包含我需要在我的应用程序中解析的数据,但是当我尝试调试时,这些值会一直显示为null,尽管我已经验证我可以访问服务器上的XML文件。

我不断在我的logcat中得到这个,这会导致我的代码中出现其他问题:

08-13 17:56:39.452: I/System.out(1424): STREAM DEBUG
08-13 17:56:39.452: I/System.out(1424): null

非常感谢任何建议。

相关来源代码段:

public class ConfigFinalActivity extends Activity implements OnClickListener {
    private static final String TAG = "ConfigActivity";
    TelephonyManager tm;
    AlertDialog mErrorAlert = null;
    private Notification mNotification = null;
    private Button assist_update_btn = null;
    // Progress
    private ImageView mProgressImageview1;
    private ImageView mProgressImageview2;
    private ImageView mProgressImageview3;
    private ImageView mProgressImageview4;
    private ImageView mProgressImageview5;

    private int mInstructionNumber = 0;
    UpdateActivity update;
    public ArrayList<String> ValueArr = new ArrayList<String>();
    public ArrayList<String> nameArr = new ArrayList<String>(5);
    public ArrayList<String> ApnArr = new ArrayList<String>();
    public ArrayList<String> mmscArr = new ArrayList<String>();
    public ArrayList<String> mmsportArr = new ArrayList<String>();
    public ArrayList<String> mmsproxyArr = new ArrayList<String>();
    public ArrayList<String> portArr = new ArrayList<String>();
    public ArrayList<String> proxyArr = new ArrayList<String>();
    private Button mAssistInstrButton = null;
    private TextView mReadAgainButton = null;
    public static int TotalSteps = 8;
    public static int count;
    int i, g = 0;
    Context ctx;
    public static ContentValues Values = new ContentValues();
    XmlParserHandlerFinal handler;
    public static BigInteger id1, id2;
    BigInteger[] id;
    public static Integer mdn1, mdn2;
    public static String car;
    public static final Uri APN_TABLE_URI = Uri
            .parse("content://telephony/carriers");
    public static String Base_URL = "https://www.mysettings.com/";
    //NetworkTask task = new NetworkTask();
    NetworkTask tasks = new NetworkTask();
    InputStream stream = null;
    private AnimationDrawable loadingAnimation;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
            int version = android.os.Build.VERSION.SDK_INT;
            tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
            int networkType = tm.getNetworkType();
            int phoneType = tm.getPhoneType();
            tasks = new NetworkTask();
            handler = new XmlParserHandlerFinal();

            handler.setContext(ctx);
            getImpVariablesForQuery();

            if (version < VERSION_CODES.ICE_CREAM_SANDWICH) {

            try {
                // updating layout initially has updating text with 1 dot in the
                // xml
                setContentView(R.layout.updating);
                // This image view has the updating text to be progressively
                // updated
                // with dots addition
                ImageView loading = (ImageView) findViewById(R.id.loading_empty1);

                // Set updating button to drawable animation
                loading.setBackgroundResource(R.drawable.updating1);
                loadingAnimation = (AnimationDrawable) loading.getBackground();

                ImageView loading2 = (ImageView) findViewById(R.id.loading_empty2);

                // Set updating button to drawable animation
                loading2.setBackgroundResource(R.drawable.updating2);
                loadingAnimation = (AnimationDrawable) loading2.getBackground();

                ImageView loading3 = (ImageView) findViewById(R.id.loading_empty3);

                // Set updating button to drawable animation
                loading.setBackgroundResource(R.drawable.updating3);
                loadingAnimation = (AnimationDrawable) loading.getBackground();

                ImageView loading4 = (ImageView) findViewById(R.id.loading_empty4);

                // Set updating button to drawable animation
                loading.setBackgroundResource(R.drawable.updating4);
                loadingAnimation = (AnimationDrawable) loading.getBackground();

                ImageView loading5 = (ImageView) findViewById(R.id.loading_empty5);

                // Set updating button to drawable animation
                loading.setBackgroundResource(R.drawable.updating5);
                loadingAnimation = (AnimationDrawable) loading.getBackground();

                tasks.execute("http://datasettings.site90.net/GetPhoneSettings-ST-rsp-eng.xml");
                if (tasks.get() != null) {
                    stream = tasks.getInputStream();
                    Log.v("CfA", "here");

                } else if (tasks.get() == null) {
                    setContentView(R.layout.error);

                    // finish();
                    // finish();

                }

                handler.getQueryResponse(stream);

                Values = getContentValues();

            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            // Update APN table in separate thread
            new TableUpdateRequestTask().execute("");

        } else {// ICS and later versions

            tasks.execute("http://datasettings.site90.net/GetPhoneSettings-ST-rsp-eng.xml");
            System.out.println("STREAM DEBUG");
            System.out.println(stream);
            try {
                if (tasks.get() != null) {
                    stream = tasks.getInputStream();
                    Log.v("CfA", "here");

                } else if (tasks.get() == null) {
                    setContentView(R.layout.error);
                }
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (ExecutionException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            // startActivity(new Intent(Settings.ACTION_APN_SETTINGS));
            try {

                System.out.println("STREAM DEBUG2");
                System.out.println(stream);

                handler.getQueryResponse(stream);
            //  System.out.println("STREAM DEBUG");
                //System.out.println(stream);
                System.out.println(nameArr);
                System.out.println(ApnArr);
                System.out.println(mmscArr);
                System.out.println(mmsproxyArr);
                System.out.println(mmsportArr);
                System.out.println(count);
                System.out.println(proxyArr);

                System.out.println(portArr);

                Values = getContentValues();
                showNotification();
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            new TableUpdateRequestTask().execute("");
        }
    }

1 个答案:

答案 0 :(得分:0)

看起来你正在启动一个线程,然后立即使用要在线程上创建的数据?这是真的吗?

如果是这样,您需要创建一个处理程序并将消息从您的线程发送到处理程序以指示进度或线程何时完成。

因此,所有这些代码都将更改为tasks.execute(...),然后您的线程将向您的处理程序发送一条消息,指示线程何时执行完毕。当您的处理程序收到消息时,您可以使用该线程中创建的信息。 Here's a good article on this subject.

另外,是的,不要在Android中使用System.out.println。请改用Log课程。