HttpUrlConnection_GET
URL url=new URL(“http://goo.gl/bL8Cyr”);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod(“GET”);
conn.connect();
InputStream inputstream=conn.getInputStream();
double fullSize=conn.getContentLength()
ByteArrayOutputStream outputstream=new ByteArrayOutputStream();
outputStream.write(buffer, 0, readSize);
注意,以上需在new Thread中執行,將執行結果利用handler 及post傳回並設定
HttpClient_POST
String user_agent = System.getProperty(“http_agent”);
HttpClient httpclient = AndroidHttpClient.newInstance(user_agent);
HttpPost httppost = new HttpPost(“上傳網址 .jsp”);
httppost.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = httpclient.execute(httppost);
此例需Tomcat server,並撰寫JSP網頁,此範例將不作說明
Http
http1.0 : 開關一個tcp通道,只能服務一次,也就是要一直重覆開關動作
http1.1 : 多次請求,只需在第一次開,最後一次關,可平行發送多組請求,但會造成線頭阻塞
http 2.0 : SPDY, 平行請求都會有id,所以不會有阻塞
SPDY-OkHttp 1.1
針對SPDY之解決方案,此為舊版本,可先不講解
http://java2s.com/Code/Jar/o/Downloadokhttp110jar.htm 下載 okhttp-1.1.0.jar.zip
解開後置於專案libs
Open Module Settings/Dependencies/+/File Dependency/選okthhp-1.1.0.jar
//HttpURLConnection conn=(HttpURLConnection)url.openConnection();
HttpURLConnection conn=new OkHttpClient().open(url);
SPDY-OkHttp 2.0
okio : https://search.maven.org/remote_content?g=com.squareup.okio&a=okio&v=LATEST
okhttp http://repo1.maven.org/maven2/com/squareup/okhttp3/okhttp/3.3.1/okhttp-3.3.1.jar
程式碼更變如下
URL url=new URL(“http://goo.gl/bL8Cyr”);
OkHttpClient client=new OkHttpClient();
Request request=new Request.Builder().url(url).build();
Response response=client.newCall(request).execute();
InputStream inputStream=response.body().byteStream();
double fullSize=response.body().contentLength();
while ((readSize=inputStream.read(buffer))!=-1){
outputStream.write(buffer,0,readSize);
percent+=(readSize/fullSize)*100;
publishProgress((int)percent);
}
img=outputStream.toByteArray();
Volley
使用git下載source code並編譯成volley.jar(請於範例中copy, 會ubuntu的人自會編譯)
RequestQueue queue= Volley.newRequestQueue(context);//最好設為全域,節省資源
ImageRequest imageRequest=new ImageRequest(//Request, 傳入網址,匿名反應,匿名錯誤
“http://i135.photobucket.com/albums/q123/ccsalan92002/scenic_photo/D1.jpg”,
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}
},0,0,Bitmap.Config.RGB_565,
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) { }
});
queue.add(imageRequest);//加入Queue中執行
Volley ImageLoader
創建一個RequestQueue物件。
創建一個ImageLoader物件。
獲取一個ImageListener物件。
調用ImageLoader的get()方法載入網路上的圖片。
創建ImageCache
NetworkImageView使用
詳細說明請見Android_Volley_ImageLoader.doc
AsyncTask
UI更新只能在主執行緒中執行, 花費較長時間之作業, 如網路, 需開立另一執行緒執行
子執行緒執行完畢,通常會用handler之post, postDelay()來更新UI
也常用handler.sendMessage(), 再由handler之handleMessage()函數接收
另一簡化方法為runOnUiThread(Runnable r)
最懶的方法是使用Timer timer=new Timer(), 再將要處理的UI寫在TimerTask之run()中, 再由timer.schedule(task, ms)觸發(不建議)
AsyncTask是Google強化Thread之類別,蠻好用的
private class MyAsyncTask extends AsyncTask<String, Integer, Bitmap> {
protected void onPreExecute() {}
protected void onProgressUpdate(Integer… values) {}
protected Bitmap doInBackground(String… params) {}
protected void onPostExecute(Bitmap result) {}
}
主Activity中, new AsyncTask後,執行myAsyncTask.execute();即可啟動