I'm a beginner in android
I'm a beginner in java
then ... i had no problem developing since I needed an app to download and parse xml from url
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 ...
update : 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 ?
If you want to execute the DownloadXml1()
then you have to write it in your onCreate()
method. And also initialize all your views in onCreate()
method not in AsyncTask as below:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.