我是安卓初学者
我是java初学者
然后......我开发没有问题,因为我需要一个应用程序来从 url 下载和解析 xml
然后我写了以下代码:
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) {
}
}
}
}
我在没有按钮和文本框的情况下尝试了它我的意思是删除授权行+预执行功能+获取编辑文本值行...然后在应用程序启动时运行它..但仍然无法正常工作:|
代码有什么问题... ??? 或者如果您知道另一种下载和解析 xml 的方法,请告诉我...
更新: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