简体   繁体   English

将xml从url下载到inputstream

[英]download xml from url to inputstream

I'm a beginner in android 我是android的初学者

I'm a beginner in java 我是Java的初学者

then ... i had no problem developing since I needed an app to download and parse xml from url 然后...开发我没有问题,因为我需要一个应用程序来从url下载和解析xml

then i wrote the following code : 然后我写了下面的代码:

    package com.aaa.bbb;


import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Base64;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;

public class Login extends Activity {

    String VA_URL = "MYURL";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    }

    public void onClick(View v) {
        if ("loginbu".equals(v.getId())){
            new DownloadXml1().execute(VA_URL);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.login, menu);
        return true;
    }
    private class DownloadXml1 extends AsyncTask<String, Void, InputStream> {

        protected void onPreExecute() {
                EditText ET1 = (EditText) findViewById(R.id.editText1);
                EditText ET2 = (EditText) findViewById(R.id.editText2);
                ProgressBar PB1 = (ProgressBar) findViewById(R.id.progressBar1);
                ET1.setVisibility(View.GONE);
                ET2.setVisibility(View.GONE);
                PB1.setVisibility(View.VISIBLE);
            }


            protected InputStream doInBackground(String...f_url) {
                EditText ET1 = (EditText) findViewById(R.id.editText1);
                EditText ET2 = (EditText) findViewById(R.id.editText2);
                String user = ET1.getText().toString();
                String pass = ET2.getText().toString();
                InputStream stream = null;
                try {
                    URL url = new URL(f_url[0]);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    String encoded;
                    encoded = "Basic " + Base64.encodeToString((user + ":" + pass).getBytes(), 0);
                    conn.setRequestProperty("Authorization", encoded);
                conn.setReadTimeout(10000 /* milliseconds */);
                conn.setConnectTimeout(15000 /* milliseconds */);
                conn.setRequestMethod("GET");
                conn.setDoInput(true);
                conn.connect();
                stream = conn.getInputStream();
            } catch (Exception ee) {}
            return stream;
        }

        protected void onPostExecute(InputStream inpStream) {
            // Parse Xml
            try {
                inpStream.close();
            } catch (IOException e) {
            }
        }

    }
}

I tried it without having button and text boxes I mean deleting the Authorization line + preexecute function + getting edit text value lines ... then running it when the application starts .. but still not working :| 我没有按钮和文本框就尝试了它,我的意思是删除授权行+预执行功能+获取编辑文本值行...然后在应用程序启动时运行它。

is there any problem with the code ... ??? 代码有什么问题吗? or if you know another method for downloading and parsing xmls please let me know ... 或者,如果您知道下载和解析xmls的另一种方法,请告诉我...

update : LogCat 更新 :LogCat

    07-26 14:00:33.647: D/gralloc_goldfish(1092): Emulator without GPU emulation detected.
07-26 14:00:34.559: I/Choreographer(1092): Skipped 50 frames!  The application may be doing too much work on its main thread.
07-26 14:00:35.338: I/Choreographer(1092): Skipped 84 frames!  The application may be doing too much work on its main thread.
07-26 14:00:38.008: I/Choreographer(1092): Skipped 37 frames!  The application may be doing too much work on its main thread.
07-26 14:00:43.498: I/Choreographer(1092): Skipped 78 frames!  The application may be doing too much work on its main thread.
07-26 14:00:43.498: D/AndroidRuntime(1092): Shutting down VM
07-26 14:00:43.608: W/dalvikvm(1092): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-26 14:00:44.218: E/AndroidRuntime(1092): FATAL EXCEPTION: main
07-26 14:00:44.218: E/AndroidRuntime(1092): java.lang.NullPointerException
07-26 14:00:44.218: E/AndroidRuntime(1092):     at com.aaa.bbb.Login$DownloadXml1.onPostExecute(Login.java:73)
07-26 14:00:44.218: E/AndroidRuntime(1092):     at com.aaa.bbb.Login$DownloadXml1.onPostExecute(Login.java:1)
07-26 14:00:44.218: E/AndroidRuntime(1092):     at android.os.AsyncTask.finish(AsyncTask.java:631)
07-26 14:00:44.218: E/AndroidRuntime(1092):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-26 14:00:44.218: E/AndroidRuntime(1092):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
07-26 14:00:44.218: E/AndroidRuntime(1092):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 14:00:44.218: E/AndroidRuntime(1092):     at android.os.Looper.loop(Looper.java:137)
07-26 14:00:44.218: E/AndroidRuntime(1092):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-26 14:00:44.218: E/AndroidRuntime(1092):     at java.lang.reflect.Method.invokeNative(Native Method)
07-26 14:00:44.218: E/AndroidRuntime(1092):     at java.lang.reflect.Method.invoke(Method.java:511)
07-26 14:00:44.218: E/AndroidRuntime(1092):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-26 14:00:44.218: E/AndroidRuntime(1092):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-26 14:00:44.218: E/AndroidRuntime(1092):     at dalvik.system.NativeStart.main(Native Method)
07-26 14:01:07.968: I/Process(1092): Sending signal. PID: 1092 SIG: 9

You are trying to execute your Asynctask code in onClick method but where have you registered your view for clicklistener ? 您正在尝试以onClick方法执行Asynctask代码,但您在哪里注册了clicklistener的视图?

If you want to execute the DownloadXml1() then you have to write it in your onCreate() method. 如果要执行DownloadXml1()则必须在onCreate()方法中编写它。 And also initialize all your views in onCreate() method not in AsyncTask as below: 并且还可以在onCreate()方法中而不是在AsyncTask中初始化所有视图,如下所示:

  EditText ET1; EditText ET2; ProgressBar PB1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); ET1 = (EditText) findViewById(R.id.editText1); ET2 = (EditText) findViewById(R.id.editText2); PB1 = (ProgressBar) findViewById(R.id.progressBar1); if ("loginbu".equals(v.getId())){ new DownloadXml1().execute(VA_URL); } } 

Hope you will get idea from this. 希望您能从中得到启发。 Try out and let me know if any issues. 试试看,让我知道是否有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM