1, after the android4.0 version, the main thread (UI thread) is not supporting the network request, the reason is probably the impact of the main thread, the speed is too slow, easy card machine, so need to open a new thread request data;
Copy Code code as follows:
Thread1 = new Thread () {
@Override
public void Run () {
try {
URL url = new URL (weburlmanager.carsever_getcarsservlet);
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
Bufferedinputstream bis = new Bufferedinputstream (Conn.getinputstream ());
Buffered reads
byte[] data = new byte[1024];
int len = 0;
String bufferstring = "";
while (len = bis.read (data))!=-1) {
Bufferstring+=new String (data, 0, Len);
}
Carlist = new Jsonarray (Bufferstring.trim ());
System.out.println (Carlist);
/*
for (int i=0;i
2, the new thread is completed after a start, found an error, null pointer nullpointerexception, to wait for the thread to complete before the data, the following are two solutions:
1) either determine whether the thread is still alive;
2) either set a flag in the thread, and change its status after the end
Copy Code code as follows:
/*
Wait for thread Thread1 execution complete
while (true) {
if (thread1.isalive ()) {
try {
Thread.Sleep (500);
catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}else{
Break
}
}
*/
When the thread is not finished, sleep 500 ms
while (!flag) {
try {
Thread.Sleep (500);
catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}}}
3, processing the returned JSON data
1 request JSON data to server, save in Carlist
Copy Code code as follows:
URL url = new URL (weburlmanager.carsever_getcarsservlet);
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
Bufferedinputstream bis = new Bufferedinputstream (Conn.getinputstream ());
Buffered reads
byte[] data = new byte[1024];
int len = 0;
String bufferstring = "";
while (len = bis.read (data))!=-1) {
Bufferstring+=new String (data, 0, Len);
}
Carlist = new Jsonarray (Bufferstring.trim ());
2) parsing JSON data
Copy Code code as follows:
Jsonobject car = (jsonobject) getitem (position);
try {
This.pic.setImageBitmap (Carimagearray.get (position));
This.title.setText (car.getstring ("title"));
This.describe.setText (car.getstring ("describe"));
This.updateTime.setText (car.getstring ("UpdateTime"));
This.price.setText (String.Format ("%.1f", car.getdouble ("price")) + "Million");
This.pic.setTag (weburlmanager.carserver_car_image+car.getstring ("IMAGE"));
New Asyncviewtask (). Execute (this.pic);
catch (Jsonexception E1) {
E1.printstacktrace ();
}
4, the picture loading is usually very slow, the best asynchronous request
Asynchronous request class source code
Copy Code code as follows:
Import Java.io.InputStream;
Import java.lang.ref.SoftReference;
Import java.net.HttpURLConnection;
Import Java.net.URL;
Import Java.util.HashMap;
Import android.graphics.drawable.Drawable;
Import Android.os.AsyncTask;
Import Android.util.Log;
Import Android.view.View;
Import Android.webkit.URLUtil;
Import Android.widget.ImageView;
public class Asyncviewtask extends Asynctask {
Private View Mview;
Private hashmap> Imagecache;
Public Asyncviewtask () {
Imagecache = new hashmap> ();
}
Protected drawable doinbackground (View ... views) {
Drawable drawable = null;
View view = Views[0];
if (View.gettag ()!= null) {
if (Imagecache.containskey (View.gettag ())) {
SoftReference cache = Imagecache.get (View.gettag (). toString ());
drawable = Cache.get ();
if (drawable!= null) {
return drawable;
}
}
try {
if (Urlutil.ishttpurl (View.gettag (). toString ())) {//If it is a network address. The connection URL downloads the picture
URL url = new URL (View.gettag (). toString ());
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
Conn.setdoinput (TRUE);
Conn.connect ();
InputStream stream = Conn.getinputstream ();
drawable = Drawable.createfromstream (Stream, "src");
Stream.Close ();
else {//if local data, directly parse
drawable = Drawable.createfrompath (View.gettag (). toString ());
}
catch (Exception e) {
LOG.V ("img", E.getmessage ());
return null;
}
}
This.mview = view;
return drawable;
}
protected void OnPostExecute (drawable drawable) {
if (drawable!= null) {
ImageView view = (ImageView) This.mview;
View.setimagedrawable (drawable);
This.mview = null;
}}}