1. In versions after Android 4.0, the main thread (UI thread) does not support network requests. The reason is probably because it affects the main thread, the speed is too slow and it is easy to get stuck, so it is necessary to enable a new thread to request data;
thread1 = new Thread(){
@Override
public void run() {
try {
URL url = new URL(WebUrlManager.CARSEVER_GetCarsServlet);
HttpURLConnection conn = (HttpURLConnection) ();
BufferedInputStream bis = new BufferedInputStream(());
//Buffered read
byte[] data = new byte[1024];
int len = 0;
String bufferString = "";
while((len = (data)) != -1){
bufferString+=new String(data, 0, len);
}
carList = new JSONArray(());
//(carList);
/*
for(int i=0;i
2. Once the new thread is completed, an error is reported and a null pointer is nullpointerexception is found. You have to wait for the thread to complete before you can get the data. The following are two solutions:
1) Either determine whether the thread is still alive;
2) Or set a flag in the thread, and after the end, change its status
/*
//Waiting for thread thread1 to be executed
while(true){
if(()){
try {
(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
();
}
}else{
break;
}
}
*/
//When the thread is not finished, sleep for 500 ms
while(!flag){
try {
(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
();
}}}
3. Process the returned json data
1) Request Json data from the server and save it in carList
URL url = new URL(WebUrlManager.CARSEVER_GetCarsServlet);
HttpURLConnection conn = (HttpURLConnection) ();
BufferedInputStream bis = new BufferedInputStream(());
//Buffer read
byte[] data = new byte[1024];
int len = 0;
String bufferString = "";
while((len = (data)) != -1){
bufferString+=new String(data, 0, len);
}
carList = new JSONArray(());
2) Analyze Json data
JSONObject car = (JSONObject) getItem(position);
try {
//((position));
(("title"));
(("describe"));
(("updateTime"));
(("%.1f", ("price"))+"10,000");
(WebUrlManager.CARSERVER_CAR_IMAGE+("image"));
new AsyncViewTask().execute();
} catch (JSONException e1) {
();
}
4. Image loading is usually very slow, it is best to request asynchronously
Asynchronous request class source code
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
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 (() != null) {
if ((())) {
SoftReference cache = (().toString());
drawable = ();
if (drawable != null) {
return drawable;
}
}
try {
if ((().toString())) {// If it is a network address. Then connect to url to download pictures
URL url = new URL(().toString());
HttpURLConnection conn = (HttpURLConnection) ();
(true);
();
InputStream stream = ();
drawable = (stream, "src");
();
} else {// If it is local data, parse it directly
drawable = (().toString());
}
} catch (Exception e) {
("img", ());
return null;
}
}
= view;
return drawable;
}
protected void onPostExecute(Drawable drawable) {
if (drawable != null) {
ImageView view = (ImageView) ;
(drawable);
= null;
}}}