編輯:關於Android編程
下面就android向服務器請求數據的問題分析如下:
1、在android4.0以後的版本,主線程(UI線程)不在支持網絡請求,原因大概是影響主線程,速度太慢,容易卡機,所以需要開啟新的線程請求數據;
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()); //緩沖讀取 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、新線程完成後一啟動,發現報錯,空指針nullpointerexception,要等待線程完畢後才能得到數據,下面是兩種解決方法:
1)要麼判斷線程是否還活著;
2)要麼在線程中設置一flag,結束後,更改其狀態
/* //等待線程thread1執行完畢 while(true){ if(thread1.isAlive()){ try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ break; } } */ //當線程還沒結束,就睡500毫秒ms while(!flag){ try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
3、處理返回的json數據1)向服務器請求Json數據,保存在carList
URL url = new URL(WebUrlManager.CARSEVER_GetCarsServlet); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); //緩沖讀取 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)解析Json數據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"))+"萬"); this.pic.setTag(WebUrlManager.CARSERVER_CAR_IMAGE+car.getString("image")); new AsyncViewTask().execute(this.pic); } catch (JSONException e1) { e1.printStackTrace(); }
4、圖片加載通常很慢,最好異步請求1)先貼出異步請求的類源代碼
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; /** * @author wzy qq:290581825 http://blog.csdn.net/wzygis */ 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())) {// 如果為網絡地址。則連接url下載圖片 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 {// 如果為本地數據,直接解析 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; } } }
結果如下:
上一篇面試總結,主要講了Activity 和Intent ,這篇主要講剩下的四大組件,面試中基礎較多的就是四大組件,需要了解的知識點也很多。上一篇文章 &nb
大多數場景下,會單單使用viewPager+Fragment來完成項目需求,但也有一些特殊情況需要用到viewPager的多重嵌套。一、例如:我的想法是:①用viewPa
概述Android Settings模塊說簡單也簡單,說難也難,裡面涉及到的知識點也挺多的。我們知道Settings主要是用於配置一些系統選項或屬性值,通過修改設置項就能
如下58同城快捷方式的效果: /** * 啟動某個activity是需要在manifest裡面定義 */ private void addShortCut